true ), 'names' ); foreach ( $post_types as $post_type ) { register_post_meta( $post_type, '_og_title', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_text_field', 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); register_post_meta( $post_type, '_og_description', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_textarea_field', 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); register_post_meta( $post_type, '_og_image_alt', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_text_field', 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); register_post_meta( $post_type, '_og_type', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => array( __CLASS__, 'sanitize_og_type' ), 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); register_post_meta( $post_type, '_twitter_creator', array( 'type' => 'string', 'single' => true, 'sanitize_callback' => 'sanitize_text_field', 'auth_callback' => static function () { return current_user_can( 'edit_posts' ); }, 'show_in_rest' => true, ) ); } } /** * Register the meta box for all public post types. * * @since 1.2.0 * * @return void */ public function register_meta_box(): void { $post_types = get_post_types( array( 'public' => true ), 'names' ); foreach ( $post_types as $post_type ) { add_meta_box( 'robotstxt-og-meta-box', __( 'Open Graph / Social Media', 'robotstxt-og' ), array( $this, 'render_meta_box' ), $post_type, 'normal', 'default' ); } } /** * Render the meta box HTML. * * @since 1.2.0 * * @param WP_Post $post The current post object. * @return void */ public function render_meta_box( WP_Post $post ): void { wp_nonce_field( 'robotstxt_og_meta_box', 'robotstxt_og_meta_box_nonce' ); $raw_title = get_post_meta( $post->ID, '_og_title', true ); $og_title = is_string( $raw_title ) ? $raw_title : ''; $raw_desc = get_post_meta( $post->ID, '_og_description', true ); $og_description = is_string( $raw_desc ) ? $raw_desc : ''; $raw_alt = get_post_meta( $post->ID, '_og_image_alt', true ); $og_image_alt = is_string( $raw_alt ) ? $raw_alt : ''; $raw_type = get_post_meta( $post->ID, '_og_type', true ); $og_type = self::sanitize_og_type( $raw_type ); $raw_creator = get_post_meta( $post->ID, '_twitter_creator', true ); $twitter_creator = is_string( $raw_creator ) ? $raw_creator : ''; ?> post_type ); if ( ! $post_type_obj || ! current_user_can( $post_type_obj->cap->edit_post, $post_id ) ) { return; } // Save og:title. $og_title_raw = filter_input( INPUT_POST, 'robotstxt_og_title', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $og_title = $og_title_raw ? sanitize_text_field( wp_unslash( $og_title_raw ) ) : ''; if ( empty( $og_title ) ) { delete_post_meta( $post_id, '_og_title' ); } else { update_post_meta( $post_id, '_og_title', $og_title ); } // Save og:description. $og_desc_raw = filter_input( INPUT_POST, 'robotstxt_og_description', FILTER_UNSAFE_RAW ); $og_desc = $og_desc_raw ? sanitize_textarea_field( wp_unslash( $og_desc_raw ) ) : ''; if ( empty( $og_desc ) ) { delete_post_meta( $post_id, '_og_description' ); } else { update_post_meta( $post_id, '_og_description', $og_desc ); } // Save og:image:alt. $og_alt_raw = filter_input( INPUT_POST, 'robotstxt_og_image_alt', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $og_alt = $og_alt_raw ? sanitize_text_field( wp_unslash( $og_alt_raw ) ) : ''; if ( empty( $og_alt ) ) { delete_post_meta( $post_id, '_og_image_alt' ); } else { update_post_meta( $post_id, '_og_image_alt', $og_alt ); } // Save og:type override (whitelisted). $og_type_raw = filter_input( INPUT_POST, 'robotstxt_og_type', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $og_type = self::sanitize_og_type( $og_type_raw ? wp_unslash( $og_type_raw ) : '' ); if ( '' === $og_type ) { delete_post_meta( $post_id, '_og_type' ); } else { update_post_meta( $post_id, '_og_type', $og_type ); } // Save twitter:creator handle. $creator_raw = filter_input( INPUT_POST, 'robotstxt_og_twitter_creator', FILTER_SANITIZE_FULL_SPECIAL_CHARS ); $creator = $creator_raw ? sanitize_text_field( wp_unslash( $creator_raw ) ) : ''; if ( '' === $creator ) { delete_post_meta( $post_id, '_twitter_creator' ); } else { update_post_meta( $post_id, '_twitter_creator', $creator ); } } }