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 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' ); $og_title = (string) get_post_meta( $post->ID, '_og_title', true ); $og_description = (string) get_post_meta( $post->ID, '_og_description', true ); ?> 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_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 ); } } }