options->get( 'show_meta_box', true ); $types = array_values( array_filter( $this->post_types->enabled(), static fn( string $type ): bool => (bool) apply_filters( 'agentwelcome_show_meta_box', $setting, $type ) ) ); if ( empty( $types ) ) { return; } add_meta_box( 'agentwelcome', __( 'AgentWelcome', 'agentwelcome' ), array( $this, 'render' ), $types, 'side' ); } /** * Loads the box script on the edit screen. * * @param string $hook Admin page hook. * @return void */ public function enqueue( string $hook ): void { if ( 'post.php' !== $hook ) { return; } Assets::style(); wp_enqueue_script( 'agentwelcome-editor', AGENTWELCOME_URL . 'assets/js/editor.js', array( 'wp-api-fetch' ), Assets::version( 'assets/js/editor.js' ), true ); } /** * Prints the box. * * @param WP_Post $post The page being edited. * @return void */ public function render( WP_Post $post ): void { wp_nonce_field( self::NONCE_ACTION, self::NONCE_NAME ); View::render( 'metabox', array( 'post' => $post, 'excluded' => (bool) get_post_meta( $post->ID, PostMeta::EXCLUDE, true ), 'description' => (string) get_post_meta( $post->ID, PostMeta::DESCRIPTION, true ), 'status' => $this->status->for_post( $post ), 'verify_url' => current_user_can( 'manage_options' ) ? VerifyPage::url( array( 'url' => (string) get_permalink( $post ) ) ) : '', ) ); } /** * Saves the hide and description choices when the page is saved. * * @param int $post_id Page ID. * @return void */ public function save( int $post_id ): void { if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } $nonce = isset( $_POST[ self::NONCE_NAME ] ) ? sanitize_text_field( wp_unslash( $_POST[ self::NONCE_NAME ] ) ) : ''; if ( ! wp_verify_nonce( $nonce, self::NONCE_ACTION ) || ! current_user_can( 'edit_post', $post_id ) ) { return; } if ( empty( $_POST['agentwelcome_exclude'] ) ) { delete_post_meta( $post_id, PostMeta::EXCLUDE ); } else { update_post_meta( $post_id, PostMeta::EXCLUDE, 1 ); } $description = isset( $_POST['agentwelcome_description'] ) ? sanitize_textarea_field( wp_unslash( $_POST['agentwelcome_description'] ) ) : ''; if ( '' === $description ) { delete_post_meta( $post_id, PostMeta::DESCRIPTION ); } else { update_post_meta( $post_id, PostMeta::DESCRIPTION, $description ); } } }