*/ function apev_get_element_labels() { return array( 'title' => __( 'Title', 'adrefill-post-element-visibility' ), 'featured_image' => __( 'Featured image', 'adrefill-post-element-visibility' ), 'author' => __( 'Author', 'adrefill-post-element-visibility' ), 'date' => __( 'Date', 'adrefill-post-element-visibility' ), 'terms' => __( 'Categories and tags', 'adrefill-post-element-visibility' ), ); } /** * Post meta key that stores the per-post toggle for an element. * * @param string $element Element key. * @return string */ function apev_meta_key( $element ) { return '_apev_hide_' . $element; } /** * Normalise a tri-state toggle value. * * '' = inherit the default, '1' = hide, '0' = show. * * @param mixed $value Raw value. * @return string */ function apev_sanitize_toggle( $value ) { if ( is_bool( $value ) ) { return $value ? '1' : '0'; } $value = is_scalar( $value ) ? trim( (string) $value ) : ''; return in_array( $value, array( '0', '1' ), true ) ? $value : ''; } /** * Post types that get the per-post toggles. * * @return string[] */ function apev_get_supported_post_types() { $types = get_post_types( array( 'public' => true, 'show_ui' => true, ), 'names' ); unset( $types['attachment'] ); /** * Filters the post types that Adrefill Visibility supports. * * @param string[] $types Post type names. */ $types = apply_filters( 'apev_supported_post_types', array_values( $types ) ); return array_values( array_unique( array_filter( array_map( 'sanitize_key', (array) $types ) ) ) ); } /** * Whether the block editor can read and write our meta through the REST API * for a post type. When it cannot, the classic meta box is shown instead. * * @param string $post_type Post type name. * @return bool */ function apev_post_type_has_rest_meta( $post_type ) { $object = get_post_type_object( $post_type ); return $object && ! empty( $object->show_in_rest ) && post_type_supports( $post_type, 'custom-fields' ); } /** * Default plugin settings. * * @return array */ function apev_default_settings() { return array( 'scope' => 'singular', 'css_fallback' => 1, 'block_meta_cleanup' => 1, 'global' => array_fill_keys( apev_get_element_keys(), 0 ), 'post_types' => array(), ); } /** * Saved settings merged with defaults. * * @return array */ function apev_get_settings() { $defaults = apev_default_settings(); $saved = get_option( 'apev_settings', array() ); if ( ! is_array( $saved ) ) { $saved = array(); } $settings = wp_parse_args( $saved, $defaults ); $settings['global'] = wp_parse_args( is_array( $settings['global'] ) ? $settings['global'] : array(), $defaults['global'] ); if ( ! is_array( $settings['post_types'] ) ) { $settings['post_types'] = array(); } return $settings; } /** * Effective default (before any per-post override) for each element of a post type. * * @param string $post_type Post type name. * @return array Element key => hidden by default. */ function apev_get_type_defaults( $post_type ) { $settings = apev_get_settings(); $type_settings = array(); if ( isset( $settings['post_types'][ $post_type ] ) && is_array( $settings['post_types'][ $post_type ] ) ) { $type_settings = $settings['post_types'][ $post_type ]; } $result = array(); foreach ( apev_get_element_keys() as $element ) { $value = isset( $type_settings[ $element ] ) ? apev_sanitize_toggle( $type_settings[ $element ] ) : ''; if ( '' === $value ) { $value = empty( $settings['global'][ $element ] ) ? '0' : '1'; } $result[ $element ] = ( '1' === $value ); } return $result; } /** * Resolve which elements are hidden for a post. * * Order of precedence: per-post toggle > post type default > global default. * * @param int $post_id Post ID. * @return array Element key => hidden. */ function apev_get_hidden_elements( $post_id ) { static $cache = array(); $post_id = (int) $post_id; if ( isset( $cache[ $post_id ] ) ) { return $cache[ $post_id ]; } $result = array_fill_keys( apev_get_element_keys(), false ); $post_type = $post_id > 0 ? get_post_type( $post_id ) : false; if ( $post_type && in_array( $post_type, apev_get_supported_post_types(), true ) ) { $defaults = apev_get_type_defaults( $post_type ); foreach ( apev_get_element_keys() as $element ) { $value = apev_sanitize_toggle( get_post_meta( $post_id, apev_meta_key( $element ), true ) ); if ( '' === $value ) { $result[ $element ] = $defaults[ $element ]; } else { $result[ $element ] = ( '1' === $value ); } } } $cache[ $post_id ] = $result; return $result; } /** * Whether we are rendering a front-end request where hiding should apply * to the given post. * * @param int $post_id Post ID. * @return bool */ function apev_is_front_context( $post_id ) { if ( is_admin() || ! did_action( 'wp' ) ) { return false; } if ( defined( 'REST_REQUEST' ) && REST_REQUEST ) { return false; } if ( is_feed() || is_embed() ) { return false; } $settings = apev_get_settings(); if ( 'everywhere' === $settings['scope'] ) { return true; } return is_singular() && (int) get_queried_object_id() === (int) $post_id; } /** * Whether an element of a post should be hidden in the current request. * * @param string $element Element key. * @param int $post_id Post ID. * @return bool */ function apev_should_hide( $element, $post_id ) { $post_id = (int) $post_id; $hide = false; if ( $post_id > 0 && apev_is_front_context( $post_id ) ) { $hidden = apev_get_hidden_elements( $post_id ); $hide = ! empty( $hidden[ $element ] ); } /** * Filters whether an element is hidden for a post. * * @param bool $hide Whether to hide the element. * @param string $element Element key (title, featured_image, author, date, terms). * @param int $post_id Post ID. */ return (bool) apply_filters( 'apev_should_hide', $hide, $element, $post_id ); } /** * Whether any element of a post is hidden in the current request. * * @param int $post_id Post ID. * @return bool */ function apev_post_has_hidden_elements( $post_id ) { foreach ( apev_get_element_keys() as $element ) { if ( apev_should_hide( $element, $post_id ) ) { return true; } } return false; }