'post_type', 'operator' => '==', 'value' => 'post' ), * ), * array( // Group 2 * array( 'param' => 'post_type', 'operator' => '==', 'value' => 'page' ), * ), * ) * * Supported params, by the screen they apply to: * - post screens: post_type, post_id, post_status, page_template, * post_category, post_taxonomy * - user profiles: user_role (the role of the user being edited) * - term screens: taxonomy * - options pages: options_page * - custom blocks: block * - any screen: current_user_role (the role of whoever is editing) * * A group whose rules mix params of different screens never matches * anywhere: a screen is exactly one kind. A group made only of * current_user_role rules has no screen to anchor to, so it never matches. * * @package Fieldloom */ if ( ! defined( 'ABSPATH' ) ) { exit; } class CFStudio_Location_Rules { /** * Params available in the location rule builder. Keyed by param key, * value is the label. Extend this array in later phases. * * @return array */ public static function get_params() { return array( 'post_type' => __( 'Post Type', 'fieldloom' ), 'post_id' => __( 'Post', 'fieldloom' ), 'post_status' => __( 'Post Status', 'fieldloom' ), 'page_template' => __( 'Page Template', 'fieldloom' ), 'post_category' => __( 'Post Category', 'fieldloom' ), 'post_taxonomy' => __( 'Post Taxonomy Term', 'fieldloom' ), 'current_user_role' => __( 'Current User Role', 'fieldloom' ), 'user_role' => __( 'User Form (edited user role)', 'fieldloom' ), 'taxonomy' => __( 'Taxonomy Term Screen', 'fieldloom' ), 'options_page' => __( 'Options Page', 'fieldloom' ), 'block' => __( 'Block', 'fieldloom' ), ); } /** Params that belong to each kind of admin screen. */ const SCREEN_PARAMS = array( 'post' => array( 'post_type', 'post_id', 'post_status', 'page_template', 'post_category', 'post_taxonomy' ), 'user' => array( 'user_role' ), 'term' => array( 'taxonomy' ), 'options' => array( 'options_page' ), 'block' => array( 'block' ), ); /** Params that apply on any screen. */ const NEUTRAL_PARAMS = array( 'current_user_role' ); /** * Values available for a given param, e.g. all public post types for * the `post_type` param. * * @param string $param Param key. * @return array value => label */ public static function get_values_for_param( $param ) { if ( 'post_type' === $param ) { $post_types = get_post_types( array( 'public' => true ), 'objects' ); $values = array(); foreach ( $post_types as $post_type ) { if ( 'attachment' === $post_type->name ) { continue; } $values[ $post_type->name ] = $post_type->labels->singular_name; } return $values; } if ( 'options_page' === $param ) { $values = array(); foreach ( CFStudio_Options_Page::get_pages() as $page ) { $values[ $page['menu_slug'] ] = $page['menu_title']; } return $values; } if ( 'post_id' === $param ) { $values = array(); $posts = get_posts( array( 'post_type' => array_keys( self::get_values_for_param( 'post_type' ) ), 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'numberposts' => 200, 'orderby' => 'title', 'order' => 'ASC', ) ); foreach ( $posts as $one ) { $values[ (string) $one->ID ] = ( '' !== $one->post_title ? $one->post_title : __( '(no title)', 'fieldloom' ) ) . ' (#' . $one->ID . ')'; } return $values; } if ( 'post_status' === $param ) { return array( 'publish' => __( 'Published', 'fieldloom' ), 'draft' => __( 'Draft', 'fieldloom' ), 'pending' => __( 'Pending Review', 'fieldloom' ), 'private' => __( 'Private', 'fieldloom' ), 'future' => __( 'Scheduled', 'fieldloom' ), ); } if ( 'page_template' === $param ) { $values = array( 'default' => __( 'Default Template', 'fieldloom' ) ); if ( function_exists( 'wp_get_theme' ) ) { foreach ( wp_get_theme()->get_page_templates( null, 'page' ) as $file => $name ) { $values[ $file ] = $name; } } return $values; } if ( 'post_category' === $param ) { $values = array(); $terms = get_terms( array( 'taxonomy' => 'category', 'hide_empty' => false, 'number' => 200, ) ); if ( ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $values[ (string) $term->term_id ] = $term->name; } } return $values; } if ( 'post_taxonomy' === $param ) { $values = array(); foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $tax ) { $terms = get_terms( array( 'taxonomy' => $tax->name, 'hide_empty' => false, 'number' => 100, ) ); if ( is_wp_error( $terms ) ) { continue; } foreach ( $terms as $term ) { $values[ $tax->name . ':' . $term->slug ] = $tax->labels->singular_name . ': ' . $term->name; } } return $values; } if ( 'current_user_role' === $param || 'user_role' === $param ) { $values = array(); if ( 'current_user_role' === $param ) { $values['super_admin'] = __( 'Super Admin', 'fieldloom' ); } else { $values['all'] = __( 'All roles', 'fieldloom' ); } foreach ( wp_roles()->get_names() as $slug => $name ) { $values[ $slug ] = translate_user_role( $name ); } return $values; } if ( 'block' === $param ) { $values = array(); foreach ( CFStudio_Blocks::get_blocks() as $name => $block ) { $values[ $name ] = $block['title']; } return $values; } if ( 'taxonomy' === $param ) { $values = array( 'all' => __( 'All taxonomies', 'fieldloom' ) ); foreach ( get_taxonomies( array( 'public' => true ), 'objects' ) as $tax ) { $values[ $tax->name ] = $tax->labels->singular_name; } return $values; } return array(); } /** * Every param's values, for the builder's JS (to refill the value * dropdown when a rule's param changes). * * @return array param => ( value => label ) */ public static function get_all_values() { $all = array(); foreach ( array_keys( self::get_params() ) as $param ) { $all[ $param ] = self::get_values_for_param( $param ); } return $all; } /** * Render the location rules builder UI (groups of AND rules, OR'd * together) for the field group edit screen. * * @param array $groups Saved rule groups. */ public static function render_builder( $groups ) { if ( empty( $groups ) ) { $groups = array( array( array( 'param' => 'post_type', 'operator' => '==', 'value' => 'post', ), ), ); } ?>