options = $options; $this->init(); } /** * Start the engine running * * @return void */ public function init() { add_action( 'add_meta_boxes', [ $this, 'add_xml_exclude_metaboxes' ] ); // Pro extends this with News Sitemap-only post types via the filter below. $post_types = apply_filters( 'adminoptim_xml_sitemap_metabox_post_types', $this->options['post_types'] ?? [ 'post' ] ); foreach ( $post_types as $post_type ) { add_action( "save_post_{$post_type}", [ $this, 'save_xml_settings' ], 10, 2 ); } } /** * Add the meta box to the supported post types. * * @return void */ public function add_xml_exclude_metaboxes() { $screens = get_post_types( '', 'names' ); $supported_post_types = apply_filters( 'adminoptim_xml_sitemap_metabox_post_types', $this->options['post_types'] ?? [ 'post' ] ); foreach ( $screens as $screen ) { if ( in_array( $screen, $supported_post_types, true ) ) { if ( current_user_can( 'publish_posts' ) || current_user_can( 'manage_options' ) ) { add_meta_box( 'adminoptim_xml_exclude_div', __( 'XML Sitemap', 'admin-optimizer' ), [ $this, 'render_metabox' ], $screen, 'side' ); } } } } /** * Render the meta box content. * * @param \WP_Post $post The post object. * @return void */ public function render_metabox( $post ) { // Use nonce for verification. wp_nonce_field( 'adminoptim_xml_post', 'adminoptim_xml_nonce', false ); // Use get_post_meta to retrieve an existing value from the database and use the value for the form. $exclude = get_post_meta( $post->ID, '_adminoptim_xml_exclude', true ); ?>
post_type ) ) { return; } $user_exclude = '0'; if ( isset( $_POST['adminoptim_xml_exclude'] ) ) { $user_exclude = sanitize_text_field( wp_unslash( $_POST['adminoptim_xml_exclude'] ) ); } $current_exclude = get_post_meta( $post->ID, '_adminoptim_xml_exclude', true ); $current_exclude = ! empty( $current_exclude ) ? $current_exclude : '0'; if ( $user_exclude !== $current_exclude ) { if ( '1' === $user_exclude ) { update_post_meta( $post_id, '_adminoptim_xml_exclude', '1' ); } else { delete_post_meta( $post_id, '_adminoptim_xml_exclude' ); } } // Pro hooks in here to save the "Exclude from News Sitemap" checkbox. do_action( 'adminoptim_xml_sitemap_save_metabox', $post_id, $post ); } }