'string', 'single' => true, 'default' => '', 'show_in_rest' => true, 'sanitize_callback' => 'apev_sanitize_toggle', 'auth_callback' => 'apev_meta_auth_callback', ) ); } } } /** * Only users who can edit the post may change its toggles. * * @param bool $allowed Whether the user can add the meta. * @param string $meta_key Meta key. * @param int $object_id Post ID. * @return bool */ function apev_meta_auth_callback( $allowed, $meta_key, $object_id ) { return current_user_can( 'edit_post', (int) $object_id ); } /** * Choices for a per-post toggle, with the current default spelled out. * * @param bool $default_hidden Whether the element is hidden by default. * @return array Value => label. */ function apev_get_toggle_choices( $default_hidden ) { return array( '' => $default_hidden ? __( 'Default (hidden)', 'adrefill-post-element-visibility' ) : __( 'Default (shown)', 'adrefill-post-element-visibility' ), '1' => __( 'Hide', 'adrefill-post-element-visibility' ), '0' => __( 'Show', 'adrefill-post-element-visibility' ), ); } /** * Add the classic meta box. * * When the block editor can edit the meta via REST, the meta box is kept for * the classic editor only and the sidebar panel is used instead. * * @param string $post_type Post type of the current screen. * @param mixed $post Current object (WP_Post for posts). */ function apev_add_meta_box( $post_type, $post ) { if ( ! $post instanceof WP_Post || ! in_array( $post_type, apev_get_supported_post_types(), true ) ) { return; } $args = null; if ( apev_post_type_has_rest_meta( $post_type ) ) { $args = array( '__back_compat_meta_box' => true ); } add_meta_box( 'apev-visibility', __( 'Adrefill Visibility', 'adrefill-post-element-visibility' ), 'apev_render_meta_box', $post_type, 'side', 'default', $args ); } /** * Render the classic meta box. * * @param WP_Post $post Current post. */ function apev_render_meta_box( $post ) { wp_nonce_field( 'apev_save_post_meta', 'apev_meta_nonce' ); $defaults = apev_get_type_defaults( $post->post_type ); echo '

' . esc_html__( 'Choose what to hide on this item. "Default" follows Settings > Adrefill Visibility.', 'adrefill-post-element-visibility' ) . '

'; foreach ( apev_get_element_labels() as $element => $label ) { $value = apev_sanitize_toggle( get_post_meta( $post->ID, apev_meta_key( $element ), true ) ); $field_id = 'apev-hide-' . str_replace( '_', '-', $element ); printf( '


'; } } /** * Save the classic meta box. * * @param int $post_id Post ID. * @param WP_Post $post Post object. */ function apev_save_meta_box( $post_id, $post ) { if ( ! isset( $_POST['apev_meta_nonce'] ) ) { return; } $nonce = sanitize_text_field( wp_unslash( $_POST['apev_meta_nonce'] ) ); if ( ! wp_verify_nonce( $nonce, 'apev_save_post_meta' ) ) { return; } if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) { return; } if ( wp_is_post_revision( $post_id ) || wp_is_post_autosave( $post_id ) ) { return; } if ( ! current_user_can( 'edit_post', $post_id ) ) { return; } if ( ! $post instanceof WP_Post || ! in_array( $post->post_type, apev_get_supported_post_types(), true ) ) { return; } $input = array(); if ( isset( $_POST['apev_hide'] ) && is_array( $_POST['apev_hide'] ) ) { $input = map_deep( wp_unslash( $_POST['apev_hide'] ), 'sanitize_text_field' ); } foreach ( apev_get_element_keys() as $element ) { $value = isset( $input[ $element ] ) ? apev_sanitize_toggle( $input[ $element ] ) : ''; $key = apev_meta_key( $element ); if ( '' === $value ) { delete_post_meta( $post_id, $key ); } else { update_post_meta( $post_id, $key, $value ); } } } /** * Load the block editor sidebar panel. */ function apev_enqueue_editor_assets() { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( ! $screen || 'post' !== $screen->base ) { return; } $post_type = $screen->post_type; if ( ! in_array( $post_type, apev_get_supported_post_types(), true ) || ! apev_post_type_has_rest_meta( $post_type ) ) { return; } wp_enqueue_script( 'apev-editor-panel', APEV_PLUGIN_URL . 'assets/js/editor-panel.js', array( 'wp-plugins', 'wp-edit-post', 'wp-editor', 'wp-element', 'wp-components', 'wp-data' ), APEV_VERSION, true ); $defaults = apev_get_type_defaults( $post_type ); $elements = array(); foreach ( apev_get_element_labels() as $element => $label ) { $elements[] = array( 'key' => $element, 'metaKey' => apev_meta_key( $element ), 'label' => $label, 'options' => array( array( 'value' => '', 'label' => $defaults[ $element ] ? __( 'Default (hidden)', 'adrefill-post-element-visibility' ) : __( 'Default (shown)', 'adrefill-post-element-visibility' ), ), array( 'value' => '1', 'label' => __( 'Hide', 'adrefill-post-element-visibility' ), ), array( 'value' => '0', 'label' => __( 'Show', 'adrefill-post-element-visibility' ), ), ), ); } $data = array( 'elements' => $elements, 'i18n' => array( 'panelTitle' => __( 'Adrefill Visibility', 'adrefill-post-element-visibility' ), 'help' => __( 'Choose what to hide on this item. "Default" follows Settings > Adrefill Visibility.', 'adrefill-post-element-visibility' ), ), ); wp_add_inline_script( 'apev-editor-panel', 'window.apevEditorData = ' . wp_json_encode( $data, JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_APOS | JSON_HEX_QUOT ) . ';', 'before' ); }