$message
1006 | Leave a review 1007 | 1008 | 1009 | 1033 | HTML; 1034 | 1035 | echo $html; 1036 | } 1037 | } 1038 | 1039 | add_action('admin_notices', 'cf_image_resizing_admin_notice'); 1040 | 1041 | function dismiss_cf_image_resizing_admin_notice() 1042 | { 1043 | update_option('cf_image_resizing_admin_notice_dismissed', 'yes'); 1044 | wp_die(); 1045 | } 1046 | 1047 | add_action('wp_ajax_dismiss_cf_image_resizing_admin_notice', 'dismiss_cf_image_resizing_admin_notice'); 1048 | 1049 | // Add Feedback system 1050 | function cf_image_resizing_admin_feedback() 1051 | { 1052 | $screen = get_current_screen(); 1053 | 1054 | if (null === $screen) { 1055 | return; 1056 | } 1057 | 1058 | if ($screen->id !== "plugins") { 1059 | return; 1060 | } 1061 | 1062 | $html = << 1064 | 1105 | 1106 | HTML; 1107 | 1108 | echo $html; 1109 | } 1110 | 1111 | add_action('admin_footer', 'cf_image_resizing_admin_feedback'); 1112 | 1113 | function send_cf_image_resizing_admin_feedback() 1114 | { 1115 | if (!wp_verify_nonce($_POST['nonce'], 'cf_image_resizing_admin_feedback_nonce')) { 1116 | wp_send_json_error('Request is invalid. Please refresh the page and try again.', 400, 0); 1117 | exit(); 1118 | } 1119 | 1120 | $account = "af87c346-67e8-4c94-a4d2-c5a5db65b4c7"; 1121 | $token = "lu-3VkkHTqTOnQ8JDRuadMAJErJQvBSQXaZL04igSTs"; 1122 | $api_url = "https://api.mecanik.dev/v1/developer/$account/wp-feedback"; 1123 | 1124 | $region = Utils::get_region($account, $token); 1125 | 1126 | $response = wp_remote_post($api_url, [ 1127 | 'headers' => [ 1128 | 'Authorization' => 'Bearer '.$token, 1129 | 'Content-Type' => 'application/json; charset=utf-8', 1130 | ], 1131 | 'body' => json_encode([ 1132 | 'reason' => sanitize_text_field($_POST['reason']), 1133 | 'comments' => sanitize_textarea_field($_POST['comments']), 1134 | 'wp_plugin_name' => 'cf-image-resizing', 1135 | 'wp_plugin_version' => CF_IMAGE_RESIZING_VERSION, 1136 | 'wp_site_url' => get_bloginfo('url'), 1137 | 'wp_version' => Utils::get_wp_version(), 1138 | 'wp_locale' => get_locale(), 1139 | 'wp_multisite' => is_multisite(), 1140 | 'php_version' => Utils::get_php_version(), 1141 | 'db_type' => Utils::get_db_type(), 1142 | 'db_version' => Utils::get_db_version(), 1143 | 'server_type' => Utils::get_server_type(), 1144 | 'server_version' => Utils::get_server_version(), 1145 | 'date_created' => current_time('mysql'), 1146 | 'region' => isset($region['recommended-storage-region']) ? $region['recommended-storage-region'] : "weur", 1147 | ]), 1148 | 'method' => 'PUT', 1149 | 'data_format' => 'body' 1150 | ]); 1151 | 1152 | // Check for errors in the response body 1153 | $response_body = wp_remote_retrieve_body($response); 1154 | $response_data = json_decode($response_body); 1155 | 1156 | if ($response_data && isset($response_data->success) && $response_data->success === false) { 1157 | $error_message = isset($response_data->errors[0]->message) ? esc_html($response_data->errors[0]->message) : 'Unknown error'; 1158 | wp_send_json_error($error_message, 400); 1159 | exit(); 1160 | } 1161 | else if ($response_data && isset($response_data->success) && $response_data->success === true && isset($response_data->error)) { 1162 | wp_send_json_error($response_data->error, 400); 1163 | exit(); 1164 | } 1165 | 1166 | wp_send_json_success($response_data, 200, 0); 1167 | } 1168 | 1169 | add_action('wp_ajax_send_cf_image_resizing_admin_feedback', 'send_cf_image_resizing_admin_feedback'); 1170 | 1171 | function enqueue_feedback_scripts($hook_suffix) 1172 | { 1173 | if (!isset($hook_suffix)) { 1174 | return; 1175 | } 1176 | 1177 | if ('plugins.php' !== $hook_suffix) { 1178 | return; 1179 | } 1180 | 1181 | wp_enqueue_style('cf-image-resizing-feedback-style', plugins_url('/', __FILE__) . 'assets/feedback.css?nocache='.substr(uniqid(),-10), [], CF_IMAGE_RESIZING_VERSION, 'all'); 1182 | wp_enqueue_script('cf-image-resizing-feedback-script', plugins_url('/', __FILE__) . 'assets/feedback.js?nocache='.substr(uniqid(),-10), [], CF_IMAGE_RESIZING_VERSION, false); 1183 | 1184 | wp_localize_script('cf-image-resizing-feedback-script', 'params', [ 1185 | 'ajax_url' => admin_url('admin-ajax.php'), 1186 | 'ajax_send_cf_image_resizing_admin_feedback_nonce' => wp_create_nonce('cf_image_resizing_admin_feedback_nonce'), 1187 | ]); 1188 | } 1189 | 1190 | add_action('admin_enqueue_scripts', 'enqueue_feedback_scripts'); 1191 | 1192 | /** 1193 | * Activation hook. 1194 | */ 1195 | function cf_image_resizing_activate() 1196 | { 1197 | // Piece of code taken from the original Cloudflare plugin 1198 | if (version_compare(PHP_VERSION, '7.0', '<')) 1199 | { 1200 | // We need to load "plugin.php" manually to call "deactivate_plugins" 1201 | require_once ABSPATH . 'wp-admin/includes/plugin.php'; 1202 | 1203 | deactivate_plugins(plugin_basename(__FILE__), true); 1204 | wp_die('The CF Image Resizing plugin requires a PHP version of at least 7.0; you have '. PHP_VERSION .'.
', 'Plugin Activation Error', array('response' => 200, 'back_link' => true)); 1205 | } 1206 | 1207 | // Set default options 1208 | update_option('cf_image_resizing_siteurl', rtrim(home_url(), '/')); 1209 | update_option('cf_image_resizing_sitefolder', ''); 1210 | update_option('cf_image_resizing_homedir', ABSPATH); 1211 | update_option('cf_image_resizing_hook_1', true); 1212 | update_option('cf_image_resizing_hook_2', true); 1213 | update_option('cf_image_resizing_hook_3', false); 1214 | update_option('cf_image_resizing_hook_4', false); 1215 | update_option('cf_image_resizing_hook_5', false); 1216 | update_option('cf_image_resizing_hook_6', true); 1217 | update_option('cf_image_resizing_fit', 'crop'); 1218 | update_option('cf_image_resizing_quality', 80); 1219 | update_option('cf_image_resizing_format', 'auto'); 1220 | update_option('cf_image_resizing_metadata', 'none'); 1221 | update_option('cf_image_resizing_onerror', 'redirect'); 1222 | update_option('cf_image_resizing_strip_img_sizes', true); 1223 | update_option('cf_image_resizing_add_img_sizes', true); 1224 | update_option('cf_image_resizing_fix_vc_composer', false); 1225 | 1226 | return true; 1227 | } 1228 | 1229 | register_activation_hook(__FILE__, 'cf_image_resizing_activate'); 1230 | 1231 | /** 1232 | * Deactivation hook. 1233 | */ 1234 | function cf_image_resizing_deactivate() 1235 | { 1236 | // Remove plugin actions/filters 1237 | remove_action('admin_init', [ CFImageResizing::class, 'register_settings' ]); 1238 | remove_action('rest_api_init', [ CFImageResizing::class, 'register_settings' ]); 1239 | remove_filter('rest_pre_dispatch', [ CFImageResizing::class, 'hook_rest_pre_dispatch' ]); 1240 | 1241 | // Remove Settings Page 1242 | remove_filter('plugin_action_links_' . plugin_basename(__FILE__), 'cf_image_resizing_shortcut'); 1243 | 1244 | // Remove plugin filters 1245 | if (CF_IMAGE_RESIZING_HOOK_1 === TRUE) 1246 | remove_filter('wp_get_attachment_image_src', [ 1247 | CFImageResizingHooks::class, 'hook_single_img' 1248 | ], PHP_INT_MAX); 1249 | 1250 | if (CF_IMAGE_RESIZING_HOOK_2 === TRUE) 1251 | remove_filter('wp_calculate_image_srcset', [ 1252 | CFImageResizingHooks::class, 'hook_srcset' 1253 | ], PHP_INT_MAX); 1254 | 1255 | if (CF_IMAGE_RESIZING_HOOK_3 === TRUE) 1256 | remove_filter('wp_get_attachment_url', [ 1257 | CFImageResizingHooks::class, 'hook_get_attachment_url' 1258 | ], PHP_INT_MAX); 1259 | 1260 | if (CF_IMAGE_RESIZING_HOOK_4 === TRUE) 1261 | remove_filter('attribute_escape', [ 1262 | CFImageResizingHooks::class, 'hook_attribute_escape' 1263 | ], PHP_INT_MAX); 1264 | 1265 | if (CF_IMAGE_RESIZING_HOOK_5 === TRUE) 1266 | remove_filter('clean_url', [ 1267 | CFImageResizingHooks::class, 'hook_clean_url' 1268 | ], PHP_INT_MAX); 1269 | 1270 | if (CF_IMAGE_RESIZING_HOOK_6 === TRUE) 1271 | remove_filter('the_content', [ 1272 | CFImageResizingHooks::class, 'hook_content_filter' 1273 | ], PHP_INT_MAX); 1274 | } 1275 | 1276 | register_deactivation_hook(__FILE__, 'cf_image_resizing_deactivate'); 1277 | } -------------------------------------------------------------------------------- /cf-image-resizing/config.php: -------------------------------------------------------------------------------- 1 | Network). 39 | 40 | // Hook -> wp_get_attachment_image_src 41 | define('CF_IMAGE_RESIZING_HOOK_1', TRUE); 42 | 43 | // Hook -> wp_calculate_image_srcset 44 | define('CF_IMAGE_RESIZING_HOOK_2', TRUE); 45 | 46 | // Hook -> wp_get_attachment_url 47 | define('CF_IMAGE_RESIZING_HOOK_3', FALSE); 48 | 49 | // Hook -> attribute_escape 50 | define('CF_IMAGE_RESIZING_HOOK_4', FALSE); 51 | 52 | // Hook -> clean_url 53 | define('CF_IMAGE_RESIZING_HOOK_5', FALSE); 54 | 55 | // Hook -> the_content 56 | define('CF_IMAGE_RESIZING_HOOK_6', TRUE); 57 | 58 | /******************************************************************************/ 59 | // Configure default Cloudflare Image Resizing options 60 | // https://developers.cloudflare.com/images/image-resizing/url-format#options 61 | 62 | // Recommended: 'crop' 63 | define('CF_IMAGE_RESIZING_FIT', 'crop'); 64 | 65 | // Recommended: 80 66 | define('CF_IMAGE_RESIZING_QUALITY', 80); 67 | 68 | // Recommended: 'auto' 69 | define('CF_IMAGE_RESIZING_FORMAT', 'auto'); 70 | 71 | // Recommended: 'redirect' 72 | define('CF_IMAGE_RESIZING_ONERROR', 'redirect'); 73 | 74 | // Recommended: 'none' 75 | define('CF_IMAGE_RESIZING_METADATA', 'none'); 76 | 77 | /******************************************************************************/ 78 | // Extra options - Enable (TRUE) or Disable (FALSE) any of them. 79 | 80 | // If enabled this will remove the size specification from the image URL, which 81 | // in result will give Cloudflare the original image for resizing. 82 | // Example: /wp-content/uploads/2020/07/project-9-1200x848.jpg 83 | // Becomes: /wp-content/uploads/2020/07/project-9.jpg 84 | // NOTE: Only takes effect when the plugin can get the width + height properly. 85 | 86 | define('CF_IMAGE_RESIZING_STRIP_SIZES', TRUE); 87 | 88 | // If enabled this will check all your