true ] ); foreach ( $post_types as $pt ) { add_meta_box( 'awb-seo-meta', __( 'CitedSEO', 'adworksbid-seo' ), [ __CLASS__, 'render_meta_box' ], $pt, 'normal', 'low' ); } add_action( 'admin_enqueue_scripts', [ __CLASS__, 'enqueue_meta_box_assets' ] ); } /** * Enqueue the React meta box bundle on post edit screens. * Only loads if the compiled build exists (i.e. after `npm run build`). */ public static function enqueue_meta_box_assets( $hook ) { if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) { return; } $asset_file = AWBSEO_PLUGIN_DIR . 'build/meta-box.asset.php'; if ( ! file_exists( $asset_file ) ) { return; // Build hasn't run yet — fall back to the legacy PHP meta box. } $asset = require $asset_file; wp_enqueue_script( 'awbseo-meta-box', AWBSEO_PLUGIN_URL . 'build/meta-box.js', $asset['dependencies'], $asset['version'], true // in footer ); if ( file_exists( AWBSEO_PLUGIN_DIR . 'build/meta-box.css' ) ) { wp_enqueue_style( 'awbseo-meta-box', AWBSEO_PLUGIN_URL . 'build/meta-box.css', [], $asset['version'] ); } wp_enqueue_media(); // needed for OG / Twitter image picker } public static function render_meta_box( $post ) { wp_nonce_field( 'awbseo_save_meta', 'awbseo_nonce' ); $meta = self::get_post_meta( $post->ID ); $analysis = AWBSEO_On_Page_Analyzer::analyze( $post->ID ); // Pass data to the React component — wp_localize_script() works here even // though the script is in_footer because render_meta_box() runs during the // page body, before wp_footer() outputs the script tag. if ( wp_script_is( 'awbseo-meta-box', 'enqueued' ) ) { $permalink = get_permalink( $post->ID ); wp_localize_script( 'awbseo-meta-box', 'AWBSEOMeta', [ 'postId' => $post->ID, 'nonce' => wp_create_nonce( 'awbseo_admin' ), 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'meta' => $meta, 'analysis' => $analysis, 'schemaTypes' => AWBSEO_Schema_Manager::get_schema_types(), 'autoTitle' => self::get_title( $post->ID ), 'autoDesc' => self::get_description( $post->ID ), 'permalink' => $permalink ?: '', ] ); } require AWBSEO_PLUGIN_DIR . 'admin/views/meta-box.php'; } public static function save_post_meta( $post_id, $post ) { if ( ! isset( $_POST['awbseo_nonce'] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST['awbseo_nonce'] ) ), 'awbseo_save_meta' ) || defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE || ! current_user_can( 'edit_post', $post_id ) ) { return; } $fields = [ 'title' => 'sanitize_text_field', 'description' => 'sanitize_textarea_field', 'focus_keyword' => 'sanitize_text_field', 'robots_index' => 'sanitize_text_field', 'robots_follow' => 'sanitize_text_field', 'canonical' => 'esc_url_raw', 'og_title' => 'sanitize_text_field', 'og_description' => 'sanitize_textarea_field', 'og_image' => 'esc_url_raw', 'twitter_title' => 'sanitize_text_field', 'twitter_desc' => 'sanitize_textarea_field', 'twitter_image' => 'esc_url_raw', 'schema_type' => 'sanitize_text_field', 'schema_data' => [ __CLASS__, 'sanitize_schema_data' ], ]; $post_title = get_the_title( $post_id ); foreach ( $fields as $key => $sanitizer ) { $raw_key = 'awbseo_' . $key; if ( isset( $_POST[ $raw_key ] ) ) { $value = is_array( $sanitizer ) ? call_user_func( $sanitizer, wp_unslash( $_POST[ $raw_key ] ) ) : $sanitizer( wp_unslash( $_POST[ $raw_key ] ) ); $old_val = get_post_meta( $post_id, self::META_PREFIX . $key, true ); update_post_meta( $post_id, self::META_PREFIX . $key, $value ); // Log to activity log only when the value actually changed. if ( (string) $old_val !== (string) $value && '' !== $value ) { switch ( $key ) { case 'description': do_action( 'awbseo_activity', 'on-page', 'Updated meta description', $post_title ); break; case 'focus_keyword': do_action( 'awbseo_activity', 'on-page', 'Set focus keyword', $post_title . ': "' . $value . '"' ); break; case 'title': do_action( 'awbseo_activity', 'on-page', 'Updated SEO title', $post_title ); break; case 'schema_type': case 'schema_data': do_action( 'awbseo_activity', 'on-page', 'Updated schema markup', $post_title ); break; case 'robots_index': do_action( 'awbseo_activity', 'on-page', 'Changed indexing setting', $post_title . ': ' . $value ); break; case 'og_image': case 'og_title': case 'og_description': do_action( 'awbseo_activity', 'on-page', 'Updated social sharing data', $post_title ); break; } } } } } public static function sanitize_schema_data( $data ) { if ( is_array( $data ) ) { $decoded = $data; } else { $data = wp_unslash( $data ); // Allow pasting a full // block copied straight from a schema generator/testing tool - strip // the wrapper tags so the JSON inside can still be decoded and saved. $data = trim( preg_replace( '#]*>#i', '', $data ) ); $decoded = json_decode( $data, true ); } // Not valid JSON (or empty) - nothing to store. if ( ! is_array( $decoded ) ) { return ''; } return wp_json_encode( self::sanitize_schema_value( $decoded ) ); } /** * Recursively sanitize a decoded schema_data structure. * Schema JSON is often nested (FAQ pairs, HowTo steps, Recipe ingredients), * so we walk every level instead of only sanitizing the top-level values - * otherwise nested arrays get cast to the literal string "Array" and the * real content is lost on save. */ private static function sanitize_schema_value( $value ) { if ( is_array( $value ) ) { return array_map( [ __CLASS__, 'sanitize_schema_value' ], $value ); } if ( is_string( $value ) ) { // textarea variant: strips tags/unsafe input but keeps line breaks, // which matters for multi-line answers/instructions/descriptions. return sanitize_textarea_field( $value ); } return $value; // numbers, booleans, null pass through untouched } // ── Getters ─────────────────────────────────────────────────────────────── public static function get( $post_id, $key, $default = '' ) { $value = get_post_meta( $post_id, self::META_PREFIX . $key, true ); return $value !== '' ? $value : $default; } public static function get_post_meta( $post_id ) { $keys = [ 'title', 'description', 'focus_keyword', 'robots_index', 'robots_follow', 'canonical', 'og_title', 'og_description', 'og_image', 'twitter_title', 'twitter_desc', 'twitter_image', 'schema_type', 'schema_data', ]; $meta = []; foreach ( $keys as $key ) { $meta[ $key ] = self::get( $post_id, $key ); } return $meta; } public static function get_title( $post_id ) { $custom = self::get( $post_id, 'title' ); if ( $custom ) { return $custom; } $settings = get_option( 'awbseo_settings', AWBSEO_Core::default_settings() ); $format = $settings['title_format'] ?? '%title% %sep% %sitename%'; return str_replace( [ '%title%', '%sep%', '%sitename%' ], [ get_the_title( $post_id ), $settings['separator'], get_bloginfo( 'name' ) ], $format ); } public static function get_description( $post_id ) { $custom = self::get( $post_id, 'description' ); if ( $custom ) { return $custom; } $post = get_post( $post_id ); if ( ! $post ) { return ''; } return wp_trim_words( wp_strip_all_tags( $post->post_excerpt ?: $post->post_content ), 30 ); } public static function get_robots( $post_id ) { $index = self::get( $post_id, 'robots_index', 'index' ); $follow = self::get( $post_id, 'robots_follow', 'follow' ); return implode( ', ', array_filter( [ $index, $follow ] ) ); } }