' . esc_html__( 'No display rule. The template never shows.', 'headfoot' ) . '';
} else {
echo esc_html( implode( ', ', array_map( [ Rules::class, 'get_label' ], $include ) ) );
}
if ( ! empty( $exclude ) ) {
/* translators: %s: Comma separated rule labels. */
echo '
' . esc_html( sprintf( __( 'Except: %s', 'headfoot' ), implode( ', ', array_map( [ Rules::class, 'get_label' ], $exclude ) ) ) ) . '';
}
if ( ! empty( $roles ) && ! in_array( 'all', $roles, true ) ) {
$options = Rules::get_role_options();
$labels = [];
foreach ( $roles as $role ) {
$labels[] = isset( $options[ $role ] ) ? $options[ $role ] : $role;
}
/* translators: %s: Comma separated role labels. */
echo '
' . esc_html( sprintf( __( 'Users: %s', 'headfoot' ), implode( ', ', $labels ) ) ) . '';
}
break;
case 'headfoot_shortcode':
printf(
'',
esc_attr( '[' . Shortcode::TAG . ' id="' . (int) $post_id . '"]' )
);
break;
}
}
/**
* Prints the type dropdown above the list table.
*
* @param string $post_type Post type of the list table.
* @return void
*/
public function type_filter( $post_type ) {
if ( Post_Type::POST_TYPE !== $post_type ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read only filter of the list table.
$current = isset( $_GET['headfoot_type'] ) ? sanitize_key( wp_unslash( $_GET['headfoot_type'] ) ) : '';
?>
is_main_query() || Post_Type::POST_TYPE !== $query->get( 'post_type' ) ) {
return;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read only filter of the list table.
$type = isset( $_GET['headfoot_type'] ) ? sanitize_key( wp_unslash( $_GET['headfoot_type'] ) ) : '';
if ( ! isset( Post_Type::get_types()[ $type ] ) ) {
return;
}
$meta_query = (array) $query->get( 'meta_query' );
$meta_query[] = [
'key' => Post_Type::META_TYPE,
'value' => $type,
];
$query->set( 'meta_query', $meta_query ); // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query
}
/**
* Tells whether the current screen belongs to the plugin.
*
* @return bool
*/
private function is_plugin_screen() {
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null;
return $screen && Post_Type::POST_TYPE === $screen->post_type;
}
/**
* Registers the meta box.
*
* @param string $post_type Post type of the edit screen.
* @return void
*/
public function add_meta_box( $post_type ) {
if ( Post_Type::POST_TYPE !== $post_type ) {
return;
}
add_meta_box(
'headfoot-template-settings',
esc_html__( 'Template Settings', 'headfoot' ),
[ $this, 'render_meta_box' ],
Post_Type::POST_TYPE,
'normal',
'high'
);
}
/**
* Changes the title placeholder on the edit screen.
*
* @param string $placeholder Placeholder text.
* @param \WP_Post $post Post object.
* @return string
*/
public function title_placeholder( $placeholder, $post ) {
if ( $post && Post_Type::POST_TYPE === $post->post_type ) {
return esc_html__( 'Template name, for example Main Header', 'headfoot' );
}
return $placeholder;
}
/**
* Loads the meta box assets on the edit screen.
*
* @param string $hook Current admin page.
* @return void
*/
public function enqueue( $hook ) {
if ( ! $this->is_plugin_screen() ) {
return;
}
if ( 'edit.php' === $hook ) {
wp_enqueue_style( 'headfoot-list-table', HEADFOOT_URL . 'assets/admin/list-table.css', [], HEADFOOT_VERSION );
return;
}
if ( ! in_array( $hook, [ 'post.php', 'post-new.php' ], true ) ) {
return;
}
wp_enqueue_style( 'headfoot-meta-box', HEADFOOT_URL . 'assets/admin/meta-box.css', [], HEADFOOT_VERSION );
wp_enqueue_script( 'headfoot-meta-box', HEADFOOT_URL . 'assets/admin/meta-box.js', [ 'jquery', 'jquery-ui-autocomplete' ], HEADFOOT_VERSION, true );
wp_localize_script(
'headfoot-meta-box',
'headfootMetaBox',
[
'ajaxUrl' => admin_url( 'admin-ajax.php' ),
'nonce' => wp_create_nonce( self::SEARCH_NONCE ),
'action' => self::SEARCH_NONCE,
'specific' => Rules::SPECIFIC,
'noResults' => esc_html__( 'No results.', 'headfoot' ),
]
);
}
/**
* Prints the meta box.
*
* @param \WP_Post $post Current post.
* @return void
*/
public function render_meta_box( $post ) {
wp_nonce_field( self::NONCE_ACTION, self::NONCE_FIELD );
$type = Post_Type::sanitize_type( get_post_meta( $post->ID, Post_Type::META_TYPE, true ) );
$include = get_post_meta( $post->ID, Post_Type::META_INCLUDE, true );
$exclude = get_post_meta( $post->ID, Post_Type::META_EXCLUDE, true );
$roles = get_post_meta( $post->ID, Post_Type::META_ROLES, true );
$canvas = get_post_meta( $post->ID, Post_Type::META_CANVAS, true );
$include = is_array( $include ) ? $include : [];
$exclude = is_array( $exclude ) ? $exclude : [];
$roles = is_array( $roles ) ? $roles : [];
// A new template starts with the rule Entire Website.
if ( 'auto-draft' === $post->post_status && empty( $include ) ) {
$include = [ 'global' ];
}
?>
| render_rule_list( 'headfoot_include_rules', $include, 'headfoot-include-rules' ); ?> | |
| render_rule_list( 'headfoot_exclude_rules', $exclude, 'headfoot-exclude-rules' ); ?> | |
%s
%s