| @@ -75,16 +75,10 @@ | ||
| 75 | 75 | ); |
| 76 | 76 | $this->common = new Metasync_Common(); |
| 77 | 77 | // get all options |
| 78 | 78 | $this->metasync_option_data = Metasync::get_option('general'); |
| 79 | - $this->init_ajax_hooks(); | |
| 80 | 79 | } |
| 81 | 80 | |
| 82 | - public function init_ajax_hooks() | |
| 83 | - { | |
| 84 | - add_action('wp_ajax_metasync_otto_ajax_action', array($this,'metasyn_otto_ajax')); | |
| 85 | - } | |
| 86 | - | |
| 87 | 81 | /** |
| 88 | 82 | * Encode non-ASCII characters in a UTF-8 string as numeric HTML entities. |
| 89 | 83 | * |
| 90 | 84 | * Uses mb_encode_numericentity when the mbstring extension is loaded; |
| @@ -170,86 +164,9 @@ | ||
| 170 | 164 | } |
| 171 | 165 | return $posts; |
| 172 | 166 | } |
| 173 | 167 | |
| 174 | - public function metasyn_otto_ajax() { | |
| 175 | - // Check nonce for security | |
| 176 | - if (!isset($_POST['nonce']) || !wp_verify_nonce($_POST['nonce'], 'otto_nonce')) { | |
| 177 | - wp_send_json_error('Invalid nonce'); | |
| 178 | - wp_die(); | |
| 179 | - } | |
| 180 | - $post_id = sanitize_text_field($_POST['post_id']); | |
| 181 | - $current_url = get_permalink($post_id); | |
| 182 | - | |
| 183 | - $header_html = get_post_meta($post_id, '_otto_header_html_json', true); | |
| 184 | 168 | |
| 185 | - // Example API call using wp_remote_get() | |
| 186 | - | |
| 187 | - | |
| 188 | - if (is_wp_error($header_html)) { | |
| 189 | - wp_send_json_error('API call failed'); | |
| 190 | - } else { | |
| 191 | - wp_send_json_success(json_decode($header_html, true)); | |
| 192 | - } | |
| 193 | - | |
| 194 | - wp_die(); // Always terminate after an AJAX call | |
| 195 | - } | |
| 196 | - | |
| 197 | - public function otto_header_data() { | |
| 198 | - global $post; | |
| 199 | - | |
| 200 | - // Get the current post ID | |
| 201 | - $post_id = $post->ID; | |
| 202 | - | |
| 203 | - // Get the current time and the last update time from post meta | |
| 204 | - $current_time = current_time('timestamp'); | |
| 205 | - $last_update_time = get_post_meta($post_id, '_otto_last_update_time', true); | |
| 206 | - | |
| 207 | - // Set the interval for 24 hours (in seconds) | |
| 208 | - $interval = 24 * 60 * 60; | |
| 209 | - | |
| 210 | - // Check if the last update time is set or if 24 hours have passed | |
| 211 | - if (!$last_update_time || ($current_time - $last_update_time) >= $interval) { | |
| 212 | - // Get the current URL | |
| 213 | - $current_url = get_permalink($post_id); | |
| 214 | - | |
| 215 | - // Use endpoint manager to get the correct API URL | |
| 216 | - $api_endpoint = class_exists('Metasync_Endpoint_Manager') | |
| 217 | - ? Metasync_Endpoint_Manager::get_endpoint('OTTO_URL_DETAILS') | |
| 218 | - : 'https://sa.searchatlas.com/api/v2/otto-url-details'; | |
| 219 | - | |
| 220 | - // Call the API | |
| 221 | - $response = wp_remote_get($api_endpoint . '/?url=' . urlencode($current_url)); | |
| 222 | - | |
| 223 | - // Check if the API call was successful | |
| 224 | - if (!is_wp_error($response)) { | |
| 225 | - $body = wp_remote_retrieve_body($response); | |
| 226 | - $data = json_decode($body, true); // Decode JSON into an associative array | |
| 227 | - | |
| 228 | - // Update the post meta with the new data and timestamp | |
| 229 | - update_post_meta($post_id, '_otto_header_html', $data['header_html_insertion']); | |
| 230 | - update_post_meta($post_id, '_otto_last_update_time', $current_time); | |
| 231 | - } | |
| 232 | - } | |
| 233 | - | |
| 234 | - // Get the saved HTML from post meta | |
| 235 | - $header_html = get_post_meta($post_id, '_otto_header_html', true); | |
| 236 | - | |
| 237 | - // Display the HTML with security measures | |
| 238 | - if ($header_html) { | |
| 239 | - echo "<!-- Otto Start -->"; | |
| 240 | - // SECURITY FIX: Sanitize HTML to prevent XSS while allowing safe HTML | |
| 241 | - echo wp_kses($header_html, array( | |
| 242 | - 'style' => array(), | |
| 243 | - 'link' => array('rel' => array(), 'href' => array(), 'type' => array()), | |
| 244 | - 'meta' => array('name' => array(), 'content' => array(), 'property' => array()), | |
| 245 | - 'script' => array('type' => array(), 'src' => array()), | |
| 246 | - // Add other safe tags as needed | |
| 247 | - )); | |
| 248 | - echo "<!-- Otto End -->"; | |
| 249 | - } | |
| 250 | - } | |
| 251 | - | |
| 252 | 169 | public function rest_authorization_middleware($request = null) |
| 253 | 170 | { |
| 254 | 171 | $api_key = ''; |
| 255 | 172 | |
| @@ -266,10 +183,10 @@ | ||
| 266 | 183 | $api_key = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_API_KEY'])); |
| 267 | 184 | } |
| 268 | 185 | |
| 269 | 186 | // Fallback: ?apikey= query param (deprecated) |
| 270 | - if (empty($api_key) && isset($_GET['apikey'])) { | |
| 271 | - $api_key = sanitize_text_field($_GET['apikey']); | |
| 187 | + if (empty($api_key) && isset($_GET['apikey'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- API key credential in REST auth, not cookie auth | |
| 188 | + $api_key = sanitize_text_field(wp_unslash($_GET['apikey'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- API key credential in REST auth, not cookie auth | |
| 272 | 189 | } |
| 273 | 190 | |
| 274 | 191 | if (empty($api_key)) { |
| 275 | 192 | return false; |
| @@ -423,10 +340,10 @@ | ||
| 423 | 340 | return '/' . ltrim((string) $GLOBALS['wp']->query_vars['rest_route'], '/'); |
| 424 | 341 | } |
| 425 | 342 | |
| 426 | 343 | // ?rest_route= form, used when pretty permalinks are disabled. |
| 427 | - if (!empty($_GET['rest_route'])) { | |
| 428 | - return '/' . ltrim(sanitize_text_field(wp_unslash($_GET['rest_route'])), '/'); | |
| 344 | + if (!empty($_GET['rest_route'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only route check for REST auth | |
| 345 | + return '/' . ltrim(sanitize_text_field(wp_unslash($_GET['rest_route'])), '/'); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only route check for REST auth | |
| 429 | 346 | } |
| 430 | 347 | |
| 431 | 348 | // Last resort: the request path with the /wp-json/ prefix stripped. |
| 432 | 349 | $uri = isset($_SERVER['REQUEST_URI']) ? (string) wp_unslash($_SERVER['REQUEST_URI']) : ''; |
| @@ -433,9 +350,9 @@ | ||
| 433 | 350 | if ($uri === '') { |
| 434 | 351 | return ''; |
| 435 | 352 | } |
| 436 | 353 | |
| 437 | - $path = (string) parse_url($uri, PHP_URL_PATH); | |
| 354 | + $path = (string) wp_parse_url($uri, PHP_URL_PATH); | |
| 438 | 355 | $url_prefix = function_exists('rest_get_url_prefix') ? rest_get_url_prefix() : 'wp-json'; |
| 439 | 356 | $needle = '/' . trim((string) $url_prefix, '/') . '/'; |
| 440 | 357 | |
| 441 | 358 | $pos = strpos($path, $needle); |
| @@ -476,10 +393,10 @@ | ||
| 476 | 393 | $api_key = sanitize_text_field(wp_unslash($_SERVER['HTTP_X_API_KEY'])); |
| 477 | 394 | } |
| 478 | 395 | |
| 479 | 396 | // Fallback: ?apikey= query param (deprecated). |
| 480 | - if ($api_key === '' && !empty($_GET['apikey'])) { | |
| 481 | - $api_key = sanitize_text_field(wp_unslash($_GET['apikey'])); | |
| 397 | + if ($api_key === '' && !empty($_GET['apikey'])) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- API key credential in REST auth, not cookie auth | |
| 398 | + $api_key = sanitize_text_field(wp_unslash($_GET['apikey'])); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- API key credential in REST auth, not cookie auth | |
| 482 | 399 | } |
| 483 | 400 | |
| 484 | 401 | if ($api_key === '') { |
| 485 | 402 | return false; |
| @@ -633,9 +550,9 @@ | ||
| 633 | 550 | array( |
| 634 | 551 | array( |
| 635 | 552 | 'methods' => 'GET', |
| 636 | 553 | 'callback' => function () { |
| 637 | - $getPostID = url_to_postid(sanitize_url($_GET['url'])); | |
| 554 | + $getPostID = url_to_postid(sanitize_url(wp_unslash($_GET['url']))); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST route authenticated by API key, not cookies | |
| 638 | 555 | |
| 639 | 556 | if ($getPostID==0) { |
| 640 | 557 | $response = false; |
| 641 | 558 | }else{ |
| @@ -1164,9 +1081,9 @@ | ||
| 1164 | 1081 | } |
| 1165 | 1082 | |
| 1166 | 1083 | public function get_errorlogs() |
| 1167 | 1084 | { |
| 1168 | - $get_data = metasync_sanitize_input_array($_GET); | |
| 1085 | + $get_data = metasync_sanitize_input_array($_GET); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST route authenticated by API key, not cookies | |
| 1169 | 1086 | if (!isset($get_data['limit'])) |
| 1170 | 1087 | return false; |
| 1171 | 1088 | $limit = sanitize_text_field($get_data['limit']) ?? null; |
| 1172 | 1089 | |
| @@ -1354,9 +1271,9 @@ | ||
| 1354 | 1271 | } |
| 1355 | 1272 | } catch (Throwable $e) { |
| 1356 | 1273 | // Fallback to DOMDocument if Dom\HTMLDocument fails |
| 1357 | 1274 | if (defined('WP_DEBUG_LOG') && WP_DEBUG_LOG) { |
| 1358 | - error_log('MetaSync: Dom\HTMLDocument error, falling back to DOMDocument: ' . $e->getMessage()); | |
| 1275 | + error_log('MetaSync: Dom\HTMLDocument error, falling back to DOMDocument: ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- debug-gated, no secrets | |
| 1359 | 1276 | } |
| 1360 | 1277 | $dom = new DOMDocument(); |
| 1361 | 1278 | libxml_use_internal_errors(true); |
| 1362 | 1279 | $encoded_content = $this->encode_numeric_entities($content); |
| @@ -1556,9 +1473,9 @@ | ||
| 1556 | 1473 | $image_id = attachment_url_to_postid($node->getAttribute('src') ); |
| 1557 | 1474 | $image_alt = get_post_meta($image_id, '_wp_attachment_image_alt', TRUE); |
| 1558 | 1475 | $result ='[et_pb_image src="'.$node->getAttribute('src') .'" url="'.$node->getAttribute('src'). '" _builder_version="'.ET_BUILDER_VERSION.'" _module_preset="default" hover_enabled="0" global_colors_info="{}" sticky_enabled="0"][/et_pb_image]'; |
| 1559 | 1476 | |
| 1560 | - error_log(json_encode($e)); | |
| 1477 | + error_log(json_encode($e)); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 1561 | 1478 | |
| 1562 | 1479 | } |
| 1563 | 1480 | }elseif($node->nodeName === 'iframe'){ |
| 1564 | 1481 | $result= '[et_pb_code _builder_version="'.ET_BUILDER_VERSION.'" _module_preset="default" global_colors_info="{}"]'.$node->ownerDocument->saveHTML($node).'[/et_pb_code]' ; |
| @@ -1708,9 +1625,9 @@ | ||
| 1708 | 1625 | return $content; |
| 1709 | 1626 | } |
| 1710 | 1627 | |
| 1711 | 1628 | # Entity-decode both sides so "Mobility & Recovery" matches "Mobility & Recovery" |
| 1712 | - $h1_text = trim(html_entity_decode(strip_tags($h1_match[1]), ENT_QUOTES | ENT_HTML5)); | |
| 1629 | + $h1_text = trim(html_entity_decode(wp_strip_all_tags($h1_match[1]), ENT_QUOTES | ENT_HTML5)); | |
| 1713 | 1630 | $title_text = trim(html_entity_decode((string) $title, ENT_QUOTES | ENT_HTML5)); |
| 1714 | 1631 | if ($h1_text === '' || strcasecmp($h1_text, $title_text) !== 0) { |
| 1715 | 1632 | return $content; |
| 1716 | 1633 | } |
| @@ -1979,13 +1896,13 @@ | ||
| 1979 | 1896 | $new_post['post_name'] = sanitize_text_field($item['permalink']); |
| 1980 | 1897 | } |
| 1981 | 1898 | |
| 1982 | 1899 | if (isset($item['post_date']) && !empty($item['post_date'])) { |
| 1983 | - $is_valid_date = date('Y-m-d', strtotime($item['post_date'])) === $item['post_date']; | |
| 1900 | + $is_valid_date = gmdate('Y-m-d', strtotime($item['post_date'])) === $item['post_date']; | |
| 1984 | 1901 | if (!$is_valid_date) { |
| 1985 | 1902 | return new WP_Error( |
| 1986 | 1903 | 'rest_post_invalid_date', |
| 1987 | - esc_html__('Post date is not valid'), | |
| 1904 | + esc_html__('Post date is not valid', 'metasync'), | |
| 1988 | 1905 | array('status' => 400) |
| 1989 | 1906 | ); |
| 1990 | 1907 | } |
| 1991 | 1908 | |
| @@ -2197,9 +2114,9 @@ | ||
| 2197 | 2114 | # Track Content Genius event in GA4 |
| 2198 | 2115 | try { |
| 2199 | 2116 | Metasync_GA4::get_instance()->track_content_genius_event($post_id, strtolower($action)); |
| 2200 | 2117 | } catch (Exception $e) { |
| 2201 | - error_log('MetaSync: Analytics tracking failed for Content Genius - ' . $e->getMessage()); | |
| 2118 | + error_log('MetaSync: Analytics tracking failed for Content Genius - ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 2202 | 2119 | } |
| 2203 | 2120 | } |
| 2204 | 2121 | |
| 2205 | 2122 | # For standard synced posts, learn (once per post-type) whether the |
| @@ -2209,9 +2126,9 @@ | ||
| 2209 | 2126 | && $new_post['post_status'] === 'publish') { |
| 2210 | 2127 | try { |
| 2211 | 2128 | $this->record_theme_title_verdict($post_id, $permalink, $new_post['post_title'], $new_post['post_type']); |
| 2212 | 2129 | } catch (Exception $e) { |
| 2213 | - error_log('MetaSync title verdict failed: ' . $e->getMessage()); | |
| 2130 | + error_log('MetaSync title verdict failed: ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 2214 | 2131 | } |
| 2215 | 2132 | } |
| 2216 | 2133 | |
| 2217 | 2134 | $respCreatePosts[$index] = array_merge($new_post, $post_meta); |
| @@ -2256,9 +2173,9 @@ | ||
| 2256 | 2173 | } |
| 2257 | 2174 | |
| 2258 | 2175 | public function delete_item() |
| 2259 | 2176 | { |
| 2260 | - $get_data = metasync_sanitize_input_array($_GET); | |
| 2177 | + $get_data = metasync_sanitize_input_array($_GET); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST route authenticated by API key, not cookies | |
| 2261 | 2178 | if (!isset($get_data['ID'])) |
| 2262 | 2179 | return false; |
| 2263 | 2180 | |
| 2264 | 2181 | $post_id = sanitize_text_field($get_data['ID']) ?? null; |
| @@ -2264,18 +2181,18 @@ | ||
| 2264 | 2181 | $post_id = sanitize_text_field($get_data['ID']) ?? null; |
| 2265 | 2182 | $post = get_post($post_id); |
| 2266 | 2183 | if ($post) { |
| 2267 | 2184 | wp_delete_post($post_id); |
| 2185 | + // HTTP 204 requires no body for response, so no message is set. | |
| 2268 | 2186 | return new WP_Error( |
| 2269 | 2187 | 'rest_post_delete_success', |
| 2270 | - esc_html__(''), | |
| 2271 | - // HTTP 204 requires no body for response | |
| 2188 | + '', | |
| 2272 | 2189 | array('status' => 204) |
| 2273 | 2190 | ); |
| 2274 | 2191 | } |
| 2275 | 2192 | return new WP_Error( |
| 2276 | 2193 | 'rest_post_delete_fail', |
| 2277 | - esc_html__('No post found in the database with requested ID.'), | |
| 2194 | + esc_html__('No post found in the database with requested ID.', 'metasync'), | |
| 2278 | 2195 | array('status' => 400) |
| 2279 | 2196 | ); |
| 2280 | 2197 | } |
| 2281 | 2198 | |
| @@ -2280,9 +2197,9 @@ | ||
| 2280 | 2197 | } |
| 2281 | 2198 | |
| 2282 | 2199 | public function get_items($request) |
| 2283 | 2200 | { |
| 2284 | - $get_data = metasync_sanitize_input_array($_GET); | |
| 2201 | + $get_data = metasync_sanitize_input_array($_GET); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST route authenticated by API key, not cookies | |
| 2285 | 2202 | |
| 2286 | 2203 | # let us check if request send post id and is valid int |
| 2287 | 2204 | if(isset($get_data['post_id']) AND intval($get_data['post_id']) > 0){ |
| 2288 | 2205 | |
| @@ -2622,9 +2539,9 @@ | ||
| 2622 | 2539 | $post_data = get_post($post_id); |
| 2623 | 2540 | if (!$post_data) { |
| 2624 | 2541 | return new WP_Error( |
| 2625 | 2542 | 'rest_post_update_fail', |
| 2626 | - esc_html__('No post found in the database with requested ID.'), | |
| 2543 | + esc_html__('No post found in the database with requested ID.', 'metasync'), | |
| 2627 | 2544 | array('status' => 400) |
| 2628 | 2545 | ); |
| 2629 | 2546 | } |
| 2630 | 2547 | |
| @@ -2762,40 +2679,8 @@ | ||
| 2762 | 2679 | ); |
| 2763 | 2680 | } |
| 2764 | 2681 | |
| 2765 | 2682 | |
| 2766 | - if (isset($post['post_date']) && !empty($post['post_date']) && false) { | |
| 2767 | - $is_valid_date = date('Y-m-d', strtotime($post['post_date'])) == $post['post_date']; | |
| 2768 | - if (!$is_valid_date) { | |
| 2769 | - return new WP_Error( | |
| 2770 | - 'rest_post_invalid_date', | |
| 2771 | - esc_html__('Post date is not valid'), | |
| 2772 | - array('status' => 400) | |
| 2773 | - ); | |
| 2774 | - } | |
| 2775 | - | |
| 2776 | - $date_limit_str = strtotime(date('Y-m-d') . '-2 month'); | |
| 2777 | - $post_date_str = strtotime($post['post_date']); | |
| 2778 | - | |
| 2779 | - if ($date_limit_str >= $post_date_str) { | |
| 2780 | - $newDate = date('Y-m-d', strtotime('-2 month')); | |
| 2781 | - return new WP_Error( | |
| 2782 | - 'rest_post_greater_date', | |
| 2783 | - esc_html__("Post date should be greater then " . $newDate), | |
| 2784 | - array('status' => 400) | |
| 2785 | - ); | |
| 2786 | - } | |
| 2787 | - | |
| 2788 | - if ($post_date_str > strtotime(date('Y-m-d'))) { | |
| 2789 | - return new WP_Error( | |
| 2790 | - 'rest_post_greater_date', | |
| 2791 | - esc_html__('Post date should be less then Today'), | |
| 2792 | - array('status' => 400) | |
| 2793 | - ); | |
| 2794 | - } | |
| 2795 | - $update_params['post_date'] = sanitize_text_field($post['post_date'] . date(' h:i:s')); | |
| 2796 | - } | |
| 2797 | - | |
| 2798 | 2683 | $post_cattegories = []; |
| 2799 | 2684 | # if ($post_data && $post_data->post_type === 'post' && is_array(@$post['post_categories'])) { |
| 2800 | 2685 | |
| 2801 | 2686 | # fixed Undefined array key issue |
| @@ -2939,9 +2824,9 @@ | ||
| 2939 | 2824 | # Track Content Genius event in GA4 |
| 2940 | 2825 | try { |
| 2941 | 2826 | Metasync_GA4::get_instance()->track_content_genius_event($post_id, 'updated'); |
| 2942 | 2827 | } catch (Exception $e) { |
| 2943 | - error_log('MetaSync: Analytics tracking failed for Content Genius - ' . $e->getMessage()); | |
| 2828 | + error_log('MetaSync: Analytics tracking failed for Content Genius - ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 2944 | 2829 | } |
| 2945 | 2830 | |
| 2946 | 2831 | # Learn (once per post-type) whether the theme renders the title, |
| 2947 | 2832 | # so future syncs prepend only when the theme genuinely omits it. |
| @@ -2949,9 +2834,9 @@ | ||
| 2949 | 2834 | && $sync_status === 'published') { |
| 2950 | 2835 | try { |
| 2951 | 2836 | $this->record_theme_title_verdict($post_id, ($permalink ?? get_permalink($post_id)), $post_title, $post_type); |
| 2952 | 2837 | } catch (Exception $e) { |
| 2953 | - error_log('MetaSync title verdict failed: ' . $e->getMessage()); | |
| 2838 | + error_log('MetaSync title verdict failed: ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 2954 | 2839 | } |
| 2955 | 2840 | } |
| 2956 | 2841 | } |
| 2957 | 2842 | |
| @@ -3073,9 +2958,9 @@ | ||
| 3073 | 2958 | $post_data = get_post($payload['post_id']); |
| 3074 | 2959 | if(!isset($post_data->post_type)){ |
| 3075 | 2960 | return new WP_Error( |
| 3076 | 2961 | 'rest_page_type_fail', |
| 3077 | - esc_html__('No page found in the database with requested ID.'), | |
| 2962 | + esc_html__('No page found in the database with requested ID.', 'metasync'), | |
| 3078 | 2963 | array('status' => 400) |
| 3079 | 2964 | ); |
| 3080 | 2965 | } |
| 3081 | 2966 | if ($post_data->post_type !== 'page') { |
| @@ -3168,20 +3053,19 @@ | ||
| 3168 | 3053 | |
| 3169 | 3054 | global $wpdb; |
| 3170 | 3055 | |
| 3171 | 3056 | // Never propagate a template this plugin wrote itself. |
| 3172 | - $owned = $this->metasync_plugin_owned_templates(); | |
| 3173 | - $placeholders = implode(',', array_fill(0, count($owned), '%s')); | |
| 3057 | + $owned = $this->metasync_plugin_owned_templates(); | |
| 3174 | 3058 | |
| 3175 | - $rows = $wpdb->get_results( | |
| 3059 | + $rows = $wpdb->get_results( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint over the plugin custom table — no WordPress API exists | |
| 3176 | 3060 | $wpdb->prepare( |
| 3177 | - "SELECT pm.meta_value AS tpl, COUNT(*) AS hits | |
| 3178 | - FROM {$wpdb->postmeta} pm | |
| 3179 | - INNER JOIN {$wpdb->posts} p ON p.ID = pm.post_id | |
| 3061 | + 'SELECT pm.meta_value AS tpl, COUNT(*) AS hits | |
| 3062 | + FROM ' . $wpdb->postmeta . ' pm | |
| 3063 | + INNER JOIN ' . $wpdb->posts . " p ON p.ID = pm.post_id | |
| 3180 | 3064 | WHERE pm.meta_key = '_wp_page_template' |
| 3181 | 3065 | AND pm.meta_value <> '' |
| 3182 | 3066 | AND pm.meta_value <> 'default' |
| 3183 | - AND pm.meta_value NOT IN ($placeholders) | |
| 3067 | + AND pm.meta_value NOT IN (" . implode(',', array_fill(0, count($owned), '%s')) . ") | |
| 3184 | 3068 | AND p.post_type = %s |
| 3185 | 3069 | AND p.post_status = 'publish' |
| 3186 | 3070 | GROUP BY pm.meta_value |
| 3187 | 3071 | ORDER BY hits DESC", |
| @@ -3196,9 +3080,9 @@ | ||
| 3196 | 3080 | // not just the ones that set a template. Counting only the latter |
| 3197 | 3081 | // would let a handful of outliers on a special template read as a |
| 3198 | 3082 | // site-wide convention on a site whose posts otherwise use the |
| 3199 | 3083 | // theme default. |
| 3200 | - $total = (int) $wpdb->get_var( | |
| 3084 | + $total = (int) $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint over the plugin custom table — no WordPress API exists | |
| 3201 | 3085 | $wpdb->prepare( |
| 3202 | 3086 | "SELECT COUNT(*) FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'publish'", |
| 3203 | 3087 | $post_type |
| 3204 | 3088 | ) |
| @@ -3375,8 +3259,17 @@ | ||
| 3375 | 3259 | } |
| 3376 | 3260 | |
| 3377 | 3261 | public function linkgraph_login() |
| 3378 | 3262 | { |
| 3263 | + # Relays caller-supplied credentials to the token API, so verify the | |
| 3264 | + # nonce sent by admin/js (metasyncLGLogin) and confirm plugin access | |
| 3265 | + # first; otherwise any logged-in user could use the site as a proxy. | |
| 3266 | + check_ajax_referer('metasync_lglogin', 'nonce'); | |
| 3267 | + | |
| 3268 | + if (!Metasync::current_user_has_plugin_access()) { | |
| 3269 | + wp_send_json_error(['message' => 'Not authorized']); | |
| 3270 | + } | |
| 3271 | + | |
| 3379 | 3272 | $post_data = metasync_sanitize_input_array($_POST); |
| 3380 | 3273 | $payload = array( |
| 3381 | 3274 | 'username' => wp_unslash(sanitize_email($post_data['username'])), |
| 3382 | 3275 | 'password' => wp_unslash(sanitize_text_field($post_data['password'])) |
| @@ -3393,9 +3286,9 @@ | ||
| 3393 | 3286 | )); |
| 3394 | 3287 | |
| 3395 | 3288 | # Error handling for timeout or connection failures |
| 3396 | 3289 | if (is_wp_error($response)) { |
| 3397 | - error_log('MetaSync: Token API failed: ' . $response->get_error_message()); | |
| 3290 | + error_log('MetaSync: Token API failed: ' . $response->get_error_message()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 3398 | 3291 | wp_send_json_error(array('message' => 'Token API request failed')); |
| 3399 | 3292 | wp_die(); |
| 3400 | 3293 | } |
| 3401 | 3294 | |
| @@ -3529,9 +3422,9 @@ | ||
| 3529 | 3422 | $option_name = '_transient_metasync_sa_connect_active_' . $nonce_token; |
| 3530 | 3423 | $timeout_name = '_transient_timeout_metasync_sa_connect_active_' . $nonce_token; |
| 3531 | 3424 | |
| 3532 | 3425 | // Check expiry first |
| 3533 | - $timeout = $wpdb->get_var( | |
| 3426 | + $timeout = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3534 | 3427 | $wpdb->prepare( |
| 3535 | 3428 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
| 3536 | 3429 | $timeout_name |
| 3537 | 3430 | ) |
| @@ -3538,14 +3431,14 @@ | ||
| 3538 | 3431 | ); |
| 3539 | 3432 | |
| 3540 | 3433 | if ($timeout && (int) $timeout < time()) { |
| 3541 | 3434 | // Expired — clean up orphan rows |
| 3542 | - $wpdb->delete($wpdb->options, array('option_name' => $option_name)); | |
| 3543 | - $wpdb->delete($wpdb->options, array('option_name' => $timeout_name)); | |
| 3435 | + $wpdb->delete($wpdb->options, array('option_name' => $option_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3436 | + $wpdb->delete($wpdb->options, array('option_name' => $timeout_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3544 | 3437 | return false; |
| 3545 | 3438 | } |
| 3546 | 3439 | |
| 3547 | - $value = $wpdb->get_var( | |
| 3440 | + $value = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3548 | 3441 | $wpdb->prepare( |
| 3549 | 3442 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
| 3550 | 3443 | $option_name |
| 3551 | 3444 | ) |
| @@ -3569,9 +3462,9 @@ | ||
| 3569 | 3462 | $option_name = '_transient_' . $transient_key; |
| 3570 | 3463 | $timeout_name = '_transient_timeout_' . $transient_key; |
| 3571 | 3464 | |
| 3572 | 3465 | // Check expiry first |
| 3573 | - $timeout = $wpdb->get_var( | |
| 3466 | + $timeout = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3574 | 3467 | $wpdb->prepare( |
| 3575 | 3468 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
| 3576 | 3469 | $timeout_name |
| 3577 | 3470 | ) |
| @@ -3577,14 +3470,14 @@ | ||
| 3577 | 3470 | ) |
| 3578 | 3471 | ); |
| 3579 | 3472 | |
| 3580 | 3473 | if ($timeout && (int) $timeout < time()) { |
| 3581 | - $wpdb->delete($wpdb->options, array('option_name' => $option_name)); | |
| 3582 | - $wpdb->delete($wpdb->options, array('option_name' => $timeout_name)); | |
| 3474 | + $wpdb->delete($wpdb->options, array('option_name' => $option_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3475 | + $wpdb->delete($wpdb->options, array('option_name' => $timeout_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3583 | 3476 | return false; |
| 3584 | 3477 | } |
| 3585 | 3478 | |
| 3586 | - $value = $wpdb->get_var( | |
| 3479 | + $value = $wpdb->get_var( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3587 | 3480 | $wpdb->prepare( |
| 3588 | 3481 | "SELECT option_value FROM {$wpdb->options} WHERE option_name = %s LIMIT 1", |
| 3589 | 3482 | $option_name |
| 3590 | 3483 | ) |
| @@ -3611,10 +3504,10 @@ | ||
| 3611 | 3504 | { |
| 3612 | 3505 | delete_transient($transient_name); |
| 3613 | 3506 | if (wp_using_ext_object_cache()) { |
| 3614 | 3507 | global $wpdb; |
| 3615 | - $wpdb->delete($wpdb->options, array('option_name' => '_transient_' . $transient_name)); | |
| 3616 | - $wpdb->delete($wpdb->options, array('option_name' => '_transient_timeout_' . $transient_name)); | |
| 3508 | + $wpdb->delete($wpdb->options, array('option_name' => '_transient_' . $transient_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3509 | + $wpdb->delete($wpdb->options, array('option_name' => '_transient_timeout_' . $transient_name)); // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3617 | 3510 | } |
| 3618 | 3511 | } |
| 3619 | 3512 | |
| 3620 | 3513 | /** |
| @@ -3631,14 +3524,14 @@ | ||
| 3631 | 3524 | { |
| 3632 | 3525 | set_transient($transient_name, $value, $expiration); |
| 3633 | 3526 | if (wp_using_ext_object_cache()) { |
| 3634 | 3527 | global $wpdb; |
| 3635 | - $wpdb->replace($wpdb->options, array( | |
| 3528 | + $wpdb->replace($wpdb->options, array( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3636 | 3529 | 'option_name' => '_transient_' . $transient_name, |
| 3637 | 3530 | 'option_value' => maybe_serialize($value), |
| 3638 | 3531 | 'autoload' => 'no', |
| 3639 | 3532 | )); |
| 3640 | - $wpdb->replace($wpdb->options, array( | |
| 3533 | + $wpdb->replace($wpdb->options, array( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 3641 | 3534 | 'option_name' => '_transient_timeout_' . $transient_name, |
| 3642 | 3535 | 'option_value' => time() + $expiration, |
| 3643 | 3536 | 'autoload' => 'no', |
| 3644 | 3537 | )); |
| @@ -4075,9 +3968,9 @@ | ||
| 4075 | 3968 | } elseif (strlen($whitelabel_domain) > 255) { |
| 4076 | 3969 | $validation_errors['whitelabel_domain'] = 'Whitelabel domain must not exceed 255 characters'; |
| 4077 | 3970 | } elseif (!filter_var($whitelabel_domain, FILTER_VALIDATE_URL)) { |
| 4078 | 3971 | $validation_errors['whitelabel_domain'] = 'Whitelabel domain must be a valid URL'; |
| 4079 | - } elseif (!in_array(parse_url($whitelabel_domain, PHP_URL_SCHEME), ['http', 'https'])) { | |
| 3972 | + } elseif (!in_array(wp_parse_url($whitelabel_domain, PHP_URL_SCHEME), ['http', 'https'])) { | |
| 4080 | 3973 | $validation_errors['whitelabel_domain'] = 'Whitelabel domain must use http or https protocol'; |
| 4081 | 3974 | } |
| 4082 | 3975 | } |
| 4083 | 3976 | |
| @@ -4090,9 +3983,9 @@ | ||
| 4090 | 3983 | $validation_errors['whitelabel_logo'] = 'Whitelabel logo URL must not exceed 500 characters'; |
| 4091 | 3984 | } else { |
| 4092 | 3985 | // If URL is invalid, we'll clear it later but not fail the POST |
| 4093 | 3986 | if (!filter_var($whitelabel_logo, FILTER_VALIDATE_URL) || |
| 4094 | - !in_array(parse_url($whitelabel_logo, PHP_URL_SCHEME), ['http', 'https'])) { | |
| 3987 | + !in_array(wp_parse_url($whitelabel_logo, PHP_URL_SCHEME), ['http', 'https'])) { | |
| 4095 | 3988 | // Don't add to validation_errors - let POST succeed but clear the field |
| 4096 | 3989 | } |
| 4097 | 3990 | } |
| 4098 | 3991 | } |
| @@ -4218,11 +4111,20 @@ | ||
| 4218 | 4111 | if (!empty($whitelabel_otto)) { |
| 4219 | 4112 | $options['general']['whitelabel_otto_name'] = $whitelabel_otto; |
| 4220 | 4113 | } |
| 4221 | 4114 | } else { |
| 4222 | - // Clear whitelabel fields when not whitelabel | |
| 4223 | - unset($options['general']['white_label_plugin_name']); | |
| 4224 | - unset($options['general']['whitelabel_otto_name']); | |
| 4115 | + // Clear whitelabel fields when not whitelabel. | |
| 4116 | + // | |
| 4117 | + // Blanked rather than unset. This array is saved through | |
| 4118 | + // Metasync::set_option(), and the settings sanitizer merges it | |
| 4119 | + // over the stored option, so a key the array simply omits keeps | |
| 4120 | + // whatever was stored — an unset here left the previous | |
| 4121 | + // agency's plugin name and OTTO name in place forever. An empty | |
| 4122 | + // string is a value the merge carries, and both readers | |
| 4123 | + // (get_effective_plugin_name(), get_whitelabel_otto_name()) | |
| 4124 | + // test with !empty(), so it reads exactly like an absent key. | |
| 4125 | + $options['general']['white_label_plugin_name'] = ''; | |
| 4126 | + $options['general']['whitelabel_otto_name'] = ''; | |
| 4225 | 4127 | } |
| 4226 | 4128 | |
| 4227 | 4129 | // Store whitelabel settings (hidden from UI but accessible to plugin logic) |
| 4228 | 4130 | if (!isset($options['whitelabel'])) { |
| @@ -4255,9 +4157,9 @@ | ||
| 4255 | 4157 | $save_result = Metasync::set_option($options); |
| 4256 | 4158 | Metasync::invalidate_api_key_cache(); |
| 4257 | 4159 | |
| 4258 | 4160 | if (!$save_result) { |
| 4259 | - error_log('MetaSync SA Connect: mark_searchatlas_nonce_used - Failed to save plugin options'); | |
| 4161 | + error_log('MetaSync SA Connect: mark_searchatlas_nonce_used - Failed to save plugin options'); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 4260 | 4162 | } else { |
| 4261 | 4163 | if ($status_code === 200) { |
| 4262 | 4164 | // Option 1: Set heartbeat cache and last-known to CONNECTED so dashboard shows iframe immediately. |
| 4263 | 4165 | // If the immediate heartbeat then fails (e.g. backoff), Option 2 prevents overwriting this to DISCONNECTED. |
| @@ -4276,9 +4178,9 @@ | ||
| 4276 | 4178 | |
| 4277 | 4179 | return true; |
| 4278 | 4180 | |
| 4279 | 4181 | } catch (Exception $e) { |
| 4280 | - error_log('MetaSync SA Connect: mark_searchatlas_nonce_used Error - ' . $e->getMessage()); | |
| 4182 | + error_log('MetaSync SA Connect: mark_searchatlas_nonce_used Error - ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 4281 | 4183 | return false; |
| 4282 | 4184 | } |
| 4283 | 4185 | } |
| 4284 | 4186 | |
| @@ -4290,9 +4192,9 @@ | ||
| 4290 | 4192 | { |
| 4291 | 4193 | global $wpdb; |
| 4292 | 4194 | |
| 4293 | 4195 | // Clear all JWT token transients |
| 4294 | - $deleted = $wpdb->query( | |
| 4196 | + $deleted = $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 4295 | 4197 | $wpdb->prepare( |
| 4296 | 4198 | "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 4297 | 4199 | '_transient_metasync_jwt_token_%' |
| 4298 | 4200 | ) |
| @@ -4298,9 +4200,9 @@ | ||
| 4298 | 4200 | ) |
| 4299 | 4201 | ); |
| 4300 | 4202 | |
| 4301 | 4203 | // Also clear timeout transients |
| 4302 | - $wpdb->query( | |
| 4204 | + $wpdb->query( // phpcs:ignore WordPress.DB.DirectDatabaseQuery -- REST admin endpoint — manual transient/option surgery with no API equivalent | |
| 4303 | 4205 | $wpdb->prepare( |
| 4304 | 4206 | "DELETE FROM {$wpdb->options} WHERE option_name LIKE %s", |
| 4305 | 4207 | '_transient_timeout_metasync_jwt_token_%' |
| 4306 | 4208 | ) |
| @@ -4323,9 +4225,9 @@ | ||
| 4323 | 4225 | // Also ensure heartbeat cron is scheduled now that we have an API key |
| 4324 | 4226 | do_action('metasync_ensure_heartbeat_cron_scheduled'); |
| 4325 | 4227 | |
| 4326 | 4228 | } catch (Exception $e) { |
| 4327 | - error_log('MetaSync SA Connect: Error triggering immediate heartbeat check - ' . $e->getMessage()); | |
| 4229 | + error_log('MetaSync SA Connect: Error triggering immediate heartbeat check - ' . $e->getMessage()); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log -- genuine failure path, bounded, no secrets | |
| 4328 | 4230 | } |
| 4329 | 4231 | } |
| 4330 | 4232 | |
| 4331 | 4233 | public function get_item_schema() |
| @@ -4343,15 +4245,15 @@ | ||
| 4343 | 4245 | 'type' => 'object', |
| 4344 | 4246 | // In JSON Schema you can specify object properties in the properties attribute. |
| 4345 | 4247 | 'properties' => array( |
| 4346 | 4248 | 'id' => array( |
| 4347 | - 'description' => esc_html__('Unique identifier for the object.', 'my-textdomain'), | |
| 4249 | + 'description' => esc_html__('Unique identifier for the object.', 'metasync'), | |
| 4348 | 4250 | 'type' => 'integer', |
| 4349 | 4251 | 'context' => array('view', 'edit', 'embed'), |
| 4350 | 4252 | 'readonly' => true, |
| 4351 | 4253 | ), |
| 4352 | 4254 | 'content' => array( |
| 4353 | - 'description' => esc_html__('The content for the object.', 'my-textdomain'), | |
| 4255 | + 'description' => esc_html__('The content for the object.', 'metasync'), | |
| 4354 | 4256 | 'type' => 'string', |
| 4355 | 4257 | ), |
| 4356 | 4258 | ), |
| 4357 | 4259 | ); |
| @@ -4442,11 +4344,11 @@ | ||
| 4442 | 4344 | |
| 4443 | 4345 | # Try alternative parameter methods |
| 4444 | 4346 | $body_params = $request->get_body_params(); |
| 4445 | 4347 | $key_param = $request->get_param('key'); |
| 4446 | - $post_key = $_POST['key'] ?? null; | |
| 4447 | - $request_key = $_REQUEST['key'] ?? null; | |
| 4448 | - $get_key = $_GET['key'] ?? null; | |
| 4348 | + $post_key = $_POST['key'] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- REST route authenticated by API key, not cookies | |
| 4349 | + $request_key = $_REQUEST['key'] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST route authenticated by API key, not cookies | |
| 4350 | + $get_key = $_GET['key'] ?? null; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- REST route authenticated by API key, not cookies | |
| 4449 | 4351 | |
| 4450 | 4352 | # Try to get key from multiple sources |
| 4451 | 4353 | $key_value = null; |
| 4452 | 4354 | |
| @@ -4538,9 +4440,15 @@ | ||
| 4538 | 4440 | # delete it so it stops shadowing the virtual route and let WordPress serve |
| 4539 | 4441 | # the key from PHP instead. |
| 4540 | 4442 | $serving = 'virtual'; |
| 4541 | 4443 | |
| 4542 | - if (is_writable($wp_root) && @file_put_contents($file_path, $safe_key) !== false) { | |
| 4444 | + # ABSPATH is intentional and verified: the Bing site-verification key | |
| 4445 | + # must be served from the site's web root exactly as Bing fetches it | |
| 4446 | + # (/{key}.txt). $safe_key is basename()+sanitize_file_name()'d and the | |
| 4447 | + # realpath check above confines $file_path to the root, so this cannot | |
| 4448 | + # escape it. Plugin Check's ABSPATHDetected warning is a false | |
| 4449 | + # positive — a verification file cannot live in the uploads directory. | |
| 4450 | + if (wp_is_writable($wp_root) && @file_put_contents($file_path, $safe_key) !== false) { | |
| 4543 | 4451 | # Cache-buster avoids a stale 404 cached by a CDN/nginx for this path. |
| 4544 | 4452 | $probe = wp_remote_get( |
| 4545 | 4453 | home_url('/' . $safe_key . '.txt?_msverify=' . time()), |
| 4546 | 4454 | array('timeout' => 10, 'sslverify' => false) |
| @@ -4552,9 +4460,9 @@ | ||
| 4552 | 4460 | $serving = 'physical'; |
| 4553 | 4461 | } else { |
| 4554 | 4462 | # 403 / 404 / wrong body — a present file shadows the virtual route, |
| 4555 | 4463 | # so remove it and let WordPress serve the key instead. |
| 4556 | - @unlink($file_path); | |
| 4464 | + @wp_delete_file($file_path); | |
| 4557 | 4465 | $serving = 'virtual'; |
| 4558 | 4466 | } |
| 4559 | 4467 | } |
| 4560 | 4468 | |