settings = Plugin::get_settings( 'posts' ); add_filter( 'adminease_settings_fields', [ $this, 'adminease_settings_fields' ] ); if( !current_user_can( 'edit_posts' ) ) { return; } if( empty( $this->settings['enable_posts_metadata_box'] ) ) { return; } add_action( 'init', [ $this, 'init' ] ); add_action( 'admin_enqueue_scripts', [ $this, 'admin_enqueue_scripts' ] ); add_action( 'admin_footer', [ $this, 'render_modal' ] ); add_action( 'wp_ajax_adminease_get_post_metadata', [ $this, 'ajax_get_post_metadata' ] ); add_action( 'wp_ajax_adminease_update_post_metadata', [ $this, 'ajax_update_post_metadata' ] ); add_action( 'wp_ajax_adminease_delete_post_metadata', [ $this, 'ajax_delete_post_metadata' ] ); } /** * Initialize post types on init hook to ensure all custom post types are registered. */ public function init() { $this->post_types = apply_filters( 'adminease_posts_metadata_box_allowed_post_types', Utils::get_post_types( [ 'base', 'media' ] ) ); add_action( 'add_meta_boxes', [ $this, 'add_meta_boxes' ] ); } /** * Modifies and returns the settings fields for admin security features. * * @param array $fields The existing configuration fields to be updated with additional options. * * @return array The updated configuration fields, including the option to add metadata boxes to posts. */ public function adminease_settings_fields( array $fields ): array { $fields['posts']['fields'][] = [ 'type' => 'switch', 'id' => 'enable-posts-metadata-box', 'name' => 'adminease[posts][enable_posts_metadata_box]', 'value' => $this->settings['enable_posts_metadata_box'] ?? false, 'label_class' => 'pwp-switch', 'input_class' => 'pwp-field-input toggle-field', 'label' => __( 'Enable Posts Metadata Box', 'adminease' ), 'description' => __( 'Enable a metadata box on post edit screens to view and manage custom fields easily. In addition, it adds image sizes to images in the media library.', 'adminease' ), ]; return $fields; } /** * Registers the meta box for allowed post types. * * @return void */ public function add_meta_boxes(): void { foreach( $this->post_types as $post_type_key => $post_type ) { if( 'shop_order' === $post_type_key ) { $post_type_key = 'woocommerce_page_wc-orders'; } elseif( 'shop_subscription' === $post_type_key ) { $post_type_key = 'woocommerce_page_wc-orders--shop_subscription'; } add_meta_box( 'adminease-posts-metadata-box', __( 'Post Metadata', 'adminease' ), [ $this, 'render_metabox' ], $post_type_key, 'normal', ); } } /** * Renders the meta box content. * * @param $post_or_order * * @return void */ public function render_metabox( $post_or_order ): void { // Get the post ID from either WP_Post object or WooCommerce order object if( $post_or_order instanceof WP_Post ) { $post_id = $post_or_order->ID; } elseif( is_object( $post_or_order ) && method_exists( $post_or_order, 'get_id' ) ) { $post_id = $post_or_order->get_id(); } else { $post_id = 0; } // Don't render if no post ID (shouldn't happen, but safety check) if( !$post_id ) { echo '
'; esc_html_e( 'Please save this post first to manage metadata.', 'adminease' ); echo '
'; return; } // Remembered so render_modal() knows the box is on screen and which post it // belongs to. The modal itself is printed in the footer, not here: a