'string', 'single' => true, 'show_in_rest' => true, 'sanitize_callback' => array(__CLASS__, 'sanitize'), 'auth_callback' => function () { return current_user_can('edit_posts'); }, )); foreach (array(self::TITLE_KEY, self::DESCRIPTION_KEY, self::CANONICAL_KEY, self::ROBOTS_KEY, self::OG_TITLE_KEY, self::OG_DESCRIPTION_KEY, self::OG_IMAGE_KEY, self::TWITTER_TITLE_KEY, self::TWITTER_DESCRIPTION_KEY, self::TWITTER_IMAGE_KEY, self::FOCUS_KEYPHRASE_KEY) as $key) { register_post_meta($post_type, $key, array( 'type' => 'string', 'single' => true, 'show_in_rest' => true, 'sanitize_callback' => array(__CLASS__, 'sanitize_text'), 'auth_callback' => function () { return current_user_can('edit_posts'); }, )); } } } public static function register_routes() { register_rest_route('agents24x7/v2', '/pages/(?P\d+)/inspection', array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'inspect_page_for_builder_edit'), 'permission_callback' => array(__CLASS__, 'check_page_permissions'), )); register_rest_route('agents24x7/v1', '/pages/(?P\d+)/builder', array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'detect_page_builder'), 'permission_callback' => array(__CLASS__, 'check_route_permissions'), )); register_rest_route('agents24x7/v1', '/pages/(?P\d+)/seo', array( array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'read_page_seo'), 'permission_callback' => array(__CLASS__, 'check_page_permissions'), ), array( 'methods' => 'POST', 'callback' => array(__CLASS__, 'write_page_seo'), 'permission_callback' => array(__CLASS__, 'check_page_permissions'), ), )); register_rest_route('agents24x7/v1', '/posts/(?P\d+)/seo', array( array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'read_post_seo'), 'permission_callback' => array(__CLASS__, 'check_page_permissions'), ), array( 'methods' => 'POST', 'callback' => array(__CLASS__, 'write_post_seo'), 'permission_callback' => array(__CLASS__, 'check_page_permissions'), ), )); register_rest_route('agents24x7/v1', '/pages/(?P\d+)/cache-purge', array( 'methods' => 'POST', 'callback' => array(__CLASS__, 'purge_page_cache'), 'permission_callback' => array(__CLASS__, 'check_page_permissions'), )); register_rest_route('agents24x7/v1', '/pages/(?P\d+)/template-clone', array( array('methods' => 'POST', 'callback' => array(__CLASS__, 'create_template_clone'), 'permission_callback' => array(__CLASS__, 'check_page_permissions')), array('methods' => 'DELETE', 'callback' => array(__CLASS__, 'delete_template_clone'), 'permission_callback' => array(__CLASS__, 'check_page_permissions')), )); } /** Publish a stable public key file for IndexNow without editing the active theme. */ public static function register_indexnow_routes() { register_rest_route('agents24x7/v1', '/indexnow/details', array( 'methods' => 'GET', 'callback' => array(__CLASS__, 'indexnow_details'), 'permission_callback' => '__return_true', )); } public static function register_indexnow_rewrite() { add_rewrite_rule('^([a-f0-9]{32})\.txt$', 'index.php?agents24x7_indexnow_key=$matches[1]', 'top'); if (get_option('agents24x7_indexnow_rewrite_version') !== '1') { update_option('agents24x7_indexnow_rewrite_version', '1', false); flush_rewrite_rules(false); } } public static function indexnow_query_vars($vars) { $vars[] = 'agents24x7_indexnow_key'; return $vars; } private static function indexnow_key() { $key = get_option('agents24x7_indexnow_key'); if (!is_string($key) || !preg_match('/^[a-f0-9]{32}$/', $key)) { try { $key = bin2hex(random_bytes(16)); } catch (Exception $error) { return null; } update_option('agents24x7_indexnow_key', $key, false); } return $key; } public static function indexnow_details() { $key = self::indexnow_key(); if (!$key) { return new WP_Error('indexnow_unavailable', esc_html__('IndexNow key could not be created.', 'agents24x7'), array('status' => 503)); } $host = wp_parse_url(home_url('/'), PHP_URL_HOST); if (!is_string($host) || $host === '') { return new WP_Error('indexnow_unavailable', esc_html__('Site host is unavailable.', 'agents24x7'), array('status' => 503)); } return array('host' => strtolower($host), 'key' => $key, 'keyLocation' => 'https://' . strtolower($host) . '/' . $key . '.txt'); } public static function serve_root_indexnow_key_file($request) { $request_key = $request->query_vars['agents24x7_indexnow_key'] ?? ''; if (!is_string($request_key) || !preg_match('/^[a-f0-9]{32}$/', $request_key)) { $path = wp_parse_url(wp_unslash($_SERVER['REQUEST_URI'] ?? ''), PHP_URL_PATH); if (!is_string($path) || !preg_match('#^/([a-f0-9]{32})\.txt$#', $path, $matches)) { return; } $request_key = $matches[1]; } $stored_key = get_option('agents24x7_indexnow_key'); if (!is_string($stored_key) || !hash_equals($stored_key, $request_key)) { return; } if (!headers_sent()) { status_header(200); header('Content-Type: text/plain; charset=utf-8'); header('Cache-Control: public, max-age=3600'); header('X-Content-Type-Options: nosniff'); } echo $stored_key; exit; } public static function purge_page_cache($request) { $page_id = (int) $request->get_param('id'); $page = $page_id > 0 ? get_post($page_id) : null; if (!$page || !in_array($page->post_type, array('page', 'post'), true)) { return new WP_Error('page_not_found', esc_html__('Page not found.', 'agents24x7'), array('status' => 404)); } if (function_exists('sg_cachepress_purge_cache')) { sg_cachepress_purge_cache(); return array('purged' => true, 'provider' => 'siteground'); } clean_post_cache($page_id); wp_cache_flush(); return array('purged' => true, 'provider' => 'wordpress'); } public static function check_page_permissions($request) { $page_id = (int) $request->get_param('id'); return $page_id > 0 && current_user_can('edit_post', $page_id); } public static function create_template_clone($request) { $source_id = (int) $request->get_param('id'); $source = $source_id > 0 ? get_post($source_id) : null; if (!$source || $source->post_type !== 'page') { return new WP_Error('page_not_found', esc_html__('Source page not found.', 'agents24x7'), array('status' => 404)); } if (!current_user_can('edit_post', $source_id)) { return new WP_Error('forbidden', esc_html__('You are not allowed to edit this page.', 'agents24x7'), array('status' => 403)); } $template = (string) get_page_template_slug($source_id); $inspection_version = (int) $request->get_param('inspectionVersion'); $digest = (string) $request->get_param('sourceDigest'); $content = $request->get_param('content'); $title = sanitize_text_field((string) $request->get_param('title')); $slug = sanitize_title((string) $request->get_param('slug')); $source_builder = get_post_meta($source_id, '_elementor_edit_mode', true) === 'builder' ? 'elementor' : (get_post_meta($source_id, '_et_pb_use_builder', true) === 'on' || strpos((string) $source->post_content, '[et_pb_') !== false ? 'divi' : (preg_match('/\[(?:vc_|mkd_)[a-zA-Z0-9_-]+(?:\s|\])/', (string) $source->post_content) ? 'wpbakery' : 'classic')); if ($inspection_version !== 2 || !$digest || !hash_equals(hash('sha256', (string) $source->post_content), $digest) || !is_string($content) || $content === '' || $title === '' || $slug === '' || $template !== (string) $request->get_param('template') || preg_match('/\.php$/i', $template) || $source_builder !== 'wpbakery') { return new WP_Error('stale_or_unsupported_template', esc_html__('The source page is stale or uses an unsupported template.', 'agents24x7'), array('status' => 409)); } $clone_key = hash('sha256', $source_id . "\n" . $digest . "\n" . $slug . "\n" . hash('sha256', $content)); $existing = get_posts(array('post_type' => 'page', 'post_status' => array('draft', 'pending'), 'meta_key' => '_agents24x7_template_clone_key', 'meta_value' => $clone_key, 'posts_per_page' => 1)); if (is_array($existing) && !empty($existing)) { $existing_page = $existing[0]; if ($existing_page->post_status !== 'draft' || (string) $existing_page->post_content !== $content || (string) get_page_template_slug($existing_page->ID) !== $template) { return new WP_Error('clone_replay_conflict', esc_html__('A prior draft for this approved request has changed.', 'agents24x7'), array('status' => 409)); } return array('id' => (int) $existing_page->ID, 'status' => 'draft', 'template' => (string) get_page_template_slug($existing_page->ID), 'contentDigest' => hash('sha256', (string) $existing_page->post_content), 'url' => get_permalink($existing_page->ID)); } if (get_page_by_path($slug, 'OBJECT', 'page')) { return new WP_Error('page_slug_exists', esc_html__('The requested page slug is already in use.', 'agents24x7'), array('status' => 409)); } $id = wp_insert_post(array('post_type' => 'page', 'post_status' => 'draft', 'post_title' => $title, 'post_name' => $slug, 'post_content' => wp_slash($content), 'post_author' => get_current_user_id(), 'meta_input' => array('_agents24x7_template_clone_key' => $clone_key)), true); if (is_wp_error($id)) { return $id; } if ($template !== '') { update_post_meta($id, '_wp_page_template', $template); } return array('id' => (int) $id, 'status' => 'draft', 'template' => (string) get_page_template_slug($id), 'contentDigest' => hash('sha256', (string) get_post_field('post_content', $id)), 'url' => get_permalink($id)); } public static function delete_template_clone($request) { $id = (int) $request->get_param('id'); $page = $id > 0 ? get_post($id) : null; if (!$page || $page->post_type !== 'page') { return new WP_Error('page_not_found', esc_html__('Page not found.', 'agents24x7'), array('status' => 404)); } if (!current_user_can('edit_post', $id)) { return new WP_Error('forbidden', esc_html__('You are not allowed to edit this page.', 'agents24x7'), array('status' => 403)); } $expected_digest = (string) $request->get_param('contentDigest'); $expected_template = (string) $request->get_param('template'); if ($page->post_status !== 'draft' || !hash_equals(hash('sha256', (string) $page->post_content), $expected_digest) || (string) get_page_template_slug($id) !== $expected_template) { return new WP_Error('page_changed', esc_html__('The page has changed since creation.', 'agents24x7'), array('status' => 409)); } wp_delete_post($id, true); return array('deleted' => true, 'id' => $id); } public static function inspect_page_for_builder_edit($request) { $page_id = (int) $request->get_param('id'); $page = $page_id > 0 ? get_post($page_id) : null; if (!$page || $page->post_type !== 'page') { return new WP_Error('page_not_found', esc_html__('Page not found.', 'agents24x7'), array('status' => 404)); } if (!current_user_can('edit_post', $page_id)) { return new WP_Error('forbidden', esc_html__('You are not allowed to edit this page.', 'agents24x7'), array('status' => 403)); } $content = (string) $page->post_content; if (get_post_meta($page_id, '_elementor_edit_mode', true) === 'builder') { $builder = 'elementor'; } elseif (get_post_meta($page_id, '_et_pb_use_builder', true) === 'on' || strpos($content, '[et_pb_') !== false) { $builder = 'divi'; } elseif (preg_match('/\[(?:vc_|mkd_)[a-zA-Z0-9_-]+(?:\s|\])/', $content)) { $builder = 'wpbakery'; } else { $builder = has_blocks($content) ? 'gutenberg' : 'classic'; } $provider = self::seo_provider(get_permalink($page_id)); $meta_provider = in_array($provider['provider'], array('conflict', 'unsupported'), true) ? 'connector' : $provider['provider']; $provider_keys = self::provider_meta_keys($meta_provider); $registered = function_exists('get_registered_meta_keys') ? get_registered_meta_keys('post', 'page') : array(); $meta = array(); foreach (array_unique(array_values($provider_keys)) as $key) { if (isset($registered[$key])) { $meta[$key] = get_post_meta($page_id, $key, true); } } return array( 'version' => 2, 'context' => 'edit', 'id' => $page_id, 'post_type' => $page->post_type, 'status' => $page->post_status, 'template' => (string) get_page_template_slug($page_id), 'builder' => $builder, 'seoProvider' => $provider['provider'], 'seoConflicts' => $provider['conflicts'], 'seoIdleProviders' => isset($provider['idleProviders']) ? $provider['idleProviders'] : array(), 'modifiedAt' => isset($page->post_modified_gmt) && $page->post_modified_gmt !== '0000-00-00 00:00:00' ? $page->post_modified_gmt : null, 'post_content' => $content, 'registeredSeoMeta' => $meta, ); } /** * The SEO plugin whose values reach the page head. When several known SEO * plugins are active, $head_url is a page of this site whose rendered head * may show which one actually prints (codpal.com runs All in One SEO and * Rank Math, and only All in One SEO's tags are served). Without a URL, * as on the front end, only a choice an earlier REST call cached is used, * so rendering a page never fetches a page. The other active plugins go in * idleProviders; conflicts stays for clashes that block writes. */ private static function seo_provider($head_url = null) { $active = array(); $known = array( 'wordpress-seo/wp-seo.php' => 'yoast', 'seo-by-rank-math/rank-math.php' => 'rank_math', 'all-in-one-seo-pack/all_in_one_seo_pack.php' => 'aioseo', 'all-in-one-seo-pack-pro/all_in_one_seo_pack.php' => 'aioseo', 'wordpress-seo/seopress.php' => 'seopress', 'wp-seopress/seopress.php' => 'seopress', ); $plugins = get_option('active_plugins', array()); $network = get_site_option('active_sitewide_plugins', array()); $slugs = array_unique(array_merge(is_array($plugins) ? $plugins : array(), is_array($network) ? array_keys($network) : array())); foreach ($known as $slug => $provider) { if (in_array($slug, $slugs, true)) { $active[$provider] = true; } } if (defined('WPSEO_VERSION')) { $active['yoast'] = true; } if (defined('RANK_MATH_VERSION')) { $active['rank_math'] = true; } if (defined('AIOSEO_VERSION')) { $active['aioseo'] = true; } if (defined('SEOPRESS_VERSION')) { $active['seopress'] = true; } $unsupported = array(); foreach ($slugs as $slug) { // AIOSEO Pro addons (aioseo-image-seo and the like) extend AIOSEO rather than competing with it. if (stripos($slug, 'seo') !== false && !isset($known[$slug]) && strpos($slug, 'aioseo-') !== 0) { $unsupported[] = $slug; } } if ($unsupported) { return array('provider' => 'unsupported', 'conflicts' => array_values(array_unique(array_merge(array_keys($active), $unsupported)))); } if (count($active) > 1) { $candidates = array_keys($active); sort($candidates); $rendered = self::rendered_provider($candidates, $head_url); if ($rendered !== null) { return array('provider' => $rendered, 'conflicts' => array(), 'idleProviders' => array_values(array_diff($candidates, array($rendered)))); } return array('provider' => 'conflict', 'conflicts' => $candidates); } return array('provider' => $active ? (string) array_key_first($active) : 'connector', 'conflicts' => array()); } /** * Comments and schema classes each plugin prints in the head. SEOPress * prints no fixed marker, so with SEOPress among the candidates the head * cannot tell them apart and the clash stands. */ private static function provider_head_signatures() { return array( 'aioseo' => array('/