home
This commit is contained in:
+401
@@ -0,0 +1,401 @@
|
||||
<?php
|
||||
/**
|
||||
* Class that handles conditional logic related to pages.
|
||||
*
|
||||
* @package WPCode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* The WPCode_Conditional_Page class.
|
||||
*/
|
||||
class WPCode_Conditional_Page extends WPCode_Conditional_Type {
|
||||
|
||||
/**
|
||||
* The type unique name (slug).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'page';
|
||||
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'where';
|
||||
|
||||
/**
|
||||
* Set the translatable label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function set_label() {
|
||||
$this->label = __( 'Page', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load evaluation-only options (without translations) for frontend use.
|
||||
* This method is called when translations are not yet loaded to avoid WP 6.7 notices.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_evaluation_options() {
|
||||
$this->options = array(
|
||||
'type_of_page' => array(
|
||||
'callback' => array( $this, 'get_type_of_page' ),
|
||||
),
|
||||
'post_type' => array(
|
||||
'callback' => array( $this, 'get_current_post_type' ),
|
||||
),
|
||||
'referrer' => array(
|
||||
'callback' => array( $this, 'get_referer' ),
|
||||
),
|
||||
'taxonomy_page' => array(
|
||||
'callback' => array( $this, 'get_taxonomy' ),
|
||||
),
|
||||
'taxonomy_term' => array(
|
||||
'callback' => array( $this, 'get_term' ),
|
||||
),
|
||||
'page_url' => array(
|
||||
'callback' => array( $this, 'get_page_url' ),
|
||||
),
|
||||
'post_meta' => array(
|
||||
'callback' => null, // Pro feature, no callback in free version.
|
||||
),
|
||||
'post_id' => array(
|
||||
'callback' => null, // Pro feature, no callback in free version.
|
||||
),
|
||||
'page_template' => array(
|
||||
'callback' => null, // Pro feature, no callback in free version.
|
||||
),
|
||||
'post_author' => array(
|
||||
'callback' => null, // Pro feature, no callback in free version.
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type options for the admin mainly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_type_options() {
|
||||
$this->options = array(
|
||||
'type_of_page' => array(
|
||||
'label' => __( 'Type of page', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Choose a WordPress-specific type of page for your rule.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => array(
|
||||
array(
|
||||
'label' => __( 'Homepage', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_front_page',
|
||||
),
|
||||
array(
|
||||
'label' => __( 'Archive', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_archive',
|
||||
),
|
||||
array(
|
||||
'label' => __( 'Single post/page', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_single',
|
||||
),
|
||||
array(
|
||||
'label' => __( 'Search page', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_search',
|
||||
),
|
||||
array(
|
||||
'label' => __( '404 page', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_404',
|
||||
),
|
||||
array(
|
||||
'label' => __( 'Author page', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_author',
|
||||
),
|
||||
array(
|
||||
'label' => __( 'Blog home', 'insert-headers-and-footers' ),
|
||||
'value' => 'is_home',
|
||||
),
|
||||
),
|
||||
'callback' => array( $this, 'get_type_of_page' ),
|
||||
),
|
||||
'post_type' => array(
|
||||
'label' => __( 'Post type', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Target by post type: posts, pages or custom post types.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => $this->get_post_types(),
|
||||
'callback' => array( $this, 'get_current_post_type' ),
|
||||
),
|
||||
'referrer' => array(
|
||||
'label' => __( 'Referrer', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Use the page referrer/last visited page url as a condition.', 'insert-headers-and-footers' ),
|
||||
'type' => 'text',
|
||||
'callback' => array( $this, 'get_referer' ),
|
||||
),
|
||||
'taxonomy_page' => array(
|
||||
'label' => __( 'Taxonomy page', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Load only on pages for a specific category/taxonomy.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => $this->get_taxonomies_options(),
|
||||
'callback' => array( $this, 'get_taxonomy' ),
|
||||
),
|
||||
'taxonomy_term' => array(
|
||||
'label' => __( 'Taxonomy term', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Choose category/taxonomy terms to target for single or archive pages.', 'insert-headers-and-footers' ),
|
||||
'type' => 'ajax',
|
||||
'options' => 'wpcode_search_terms',
|
||||
'callback' => array( $this, 'get_term' ),
|
||||
'labels_callback' => array( $this, 'get_taxonomy_term_labels' ),
|
||||
'multiple' => true,
|
||||
),
|
||||
'page_url' => array(
|
||||
'label' => __( 'Page URL', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Use the page URL to limit where this snippet is loaded.', 'insert-headers-and-footers' ),
|
||||
'type' => 'text',
|
||||
'callback' => array( $this, 'get_page_url' ),
|
||||
),
|
||||
'post_meta' => array(
|
||||
'label' => __( 'Post meta', 'insert-headers-and-footers' ) . ' (PRO)',
|
||||
'description' => __( 'Target specific posts based on custom post meta values.', 'insert-headers-and-footers' ),
|
||||
'type' => 'text',
|
||||
'options' => array(),
|
||||
'upgrade' => array(
|
||||
'title' => __( 'Post Meta rules are a Pro feature', 'insert-headers-and-footers' ),
|
||||
'text' => __( 'Upgrade today to create conditional logic rules for specific pages or posts.', 'insert-headers-and-footers' ),
|
||||
'link' => wpcode_utm_url( 'https://wpcode.com/lite/', 'edit-snippet', 'conditional-logic', 'post_meta' ),
|
||||
),
|
||||
),
|
||||
'post_id' => array(
|
||||
'label' => __( 'Post/Page', 'insert-headers-and-footers' ) . ' (PRO)',
|
||||
'description' => __( 'Pick specific posts or pages to load the snippet on.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => array(),
|
||||
'upgrade' => array(
|
||||
'title' => __( 'Post specific rules are a Pro feature', 'insert-headers-and-footers' ),
|
||||
'text' => __( 'Upgrade today to create conditional logic rules for specific pages or posts.', 'insert-headers-and-footers' ),
|
||||
'link' => wpcode_utm_url( 'https://wpcode.com/lite/', 'edit-snippet', 'conditional-logic', 'post_id' ),
|
||||
),
|
||||
),
|
||||
'page_template' => array(
|
||||
'label' => __( 'Page Template', 'insert-headers-and-footers' ) . ' (PRO)',
|
||||
'description' => __( 'Load the snippet only on pages with a specific template.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => array(),
|
||||
'upgrade' => array(
|
||||
'title' => __( 'Page Template rules are a Pro feature', 'insert-headers-and-footers' ),
|
||||
'text' => __( 'Upgrade today to create conditional logic rules for specific page templates.', 'insert-headers-and-footers' ),
|
||||
'link' => wpcode_utm_url( 'https://wpcode.com/lite/', 'edit-snippet', 'conditional-logic', 'page_template' ),
|
||||
),
|
||||
),
|
||||
'post_author' => array(
|
||||
'label' => __( 'Author', 'insert-headers-and-footers' ) . ' (PRO)',
|
||||
'description' => __( 'Load the snippet only on pages with a specific author.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => array(),
|
||||
'upgrade' => array(
|
||||
'title' => __( 'Post Author rules are a Pro feature', 'insert-headers-and-footers' ),
|
||||
'text' => __( 'Upgrade today to create conditional logic rules based on the page/post author.', 'insert-headers-and-footers' ),
|
||||
'link' => wpcode_utm_url( 'https://wpcode.com/lite/', 'edit-snippet', 'conditional-logic', 'post_author' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of options with post types.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_post_types() {
|
||||
$post_types = get_post_types( array( 'public' => true ), 'objects' );
|
||||
$options = array();
|
||||
foreach ( $post_types as $post_type ) {
|
||||
$options[] = array(
|
||||
'label' => $post_type->label,
|
||||
'value' => $post_type->name,
|
||||
);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of taxonomy types.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_taxonomies_options() {
|
||||
$taxonomies = get_taxonomies(
|
||||
array(
|
||||
'public' => true,
|
||||
),
|
||||
'objects'
|
||||
);
|
||||
$options = array();
|
||||
foreach ( $taxonomies as $taxonomy ) {
|
||||
if ( 'post_format' === $taxonomy->name ) {
|
||||
continue;
|
||||
}
|
||||
$options[] = array(
|
||||
// Translators: this is the name of the taxonomy.
|
||||
'label' => $taxonomy->labels->singular_name,
|
||||
'value' => $taxonomy->name,
|
||||
);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type of page.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_type_of_page() {
|
||||
global $wp_query;
|
||||
|
||||
if ( ! isset( $wp_query ) ) {
|
||||
return '';
|
||||
}
|
||||
if ( is_front_page() ) {
|
||||
return 'is_front_page';
|
||||
}
|
||||
if ( is_home() ) {
|
||||
return 'is_home';
|
||||
}
|
||||
if ( is_singular() ) {
|
||||
return 'is_single';
|
||||
}
|
||||
if ( is_author() ) {
|
||||
return 'is_author';
|
||||
}
|
||||
if ( is_archive() ) {
|
||||
return 'is_archive';
|
||||
}
|
||||
if ( is_search() ) {
|
||||
return 'is_search';
|
||||
}
|
||||
if ( is_404() ) {
|
||||
return 'is_404';
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current page post type, if any.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_current_post_type() {
|
||||
return get_post_type();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the referrer from PHP.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_referer() {
|
||||
return isset( $_SERVER['HTTP_REFERER'] ) ? $_SERVER['HTTP_REFERER'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.MissingUnslash, WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the page URL.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_page_url() {
|
||||
global $wp;
|
||||
|
||||
if ( is_admin() ) {
|
||||
$url = isset( $_SERVER['REQUEST_URI'] ) ? basename( esc_url_raw( wp_unslash( $_SERVER['REQUEST_URI'] ) ) ) : '';
|
||||
$url = admin_url( $url );
|
||||
} else {
|
||||
$url = isset( $wp->request ) ? trailingslashit( home_url( $wp->request ) ) : '';
|
||||
}
|
||||
|
||||
if ( ! empty( $_GET ) ) { // phpcs:ignore
|
||||
foreach ( $_GET as $key => $value ) { // phpcs:ignore
|
||||
$key = sanitize_key( $key );
|
||||
$url = add_query_arg(
|
||||
array(
|
||||
$key => sanitize_text_field( wp_unslash( $value ) ),
|
||||
),
|
||||
$url
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
return $url;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the current page is a taxonomy page and if yes get the taxonomy name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_taxonomy() {
|
||||
global $wp_query;
|
||||
if ( is_null( $wp_query ) ) {
|
||||
return '';
|
||||
}
|
||||
$queried_object = get_queried_object();
|
||||
|
||||
return isset( $queried_object->taxonomy ) ? $queried_object->taxonomy : '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the term of the current page, if any.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_term() {
|
||||
global $wp_query;
|
||||
if ( is_null( $wp_query ) ) {
|
||||
return array();
|
||||
}
|
||||
if ( is_tax() || is_category() || is_tag() ) {
|
||||
$queried_object = get_queried_object();
|
||||
|
||||
return isset( $queried_object->term_id ) ? array( $queried_object->term_id ) : array();
|
||||
}
|
||||
if ( is_singular() ) {
|
||||
return get_terms(
|
||||
array(
|
||||
'object_ids' => array( get_the_ID() ),
|
||||
'fields' => 'ids',
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
return array();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the term labels for the taxonomy term value loading in the admin form.
|
||||
*
|
||||
* @param array $values The values that are selected.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_taxonomy_term_labels( $values ) {
|
||||
$labels = array();
|
||||
foreach ( $values as $term_id ) {
|
||||
$term = get_term( $term_id );
|
||||
if ( is_null( $term ) || is_wp_error( $term ) ) {
|
||||
continue;
|
||||
}
|
||||
$labels[] = array(
|
||||
'value' => $term_id,
|
||||
'label' => $term->name,
|
||||
);
|
||||
}
|
||||
|
||||
return $labels;
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Conditional_Page();
|
||||
+291
@@ -0,0 +1,291 @@
|
||||
<?php
|
||||
/**
|
||||
* Base class for types of conditional logic options.
|
||||
*
|
||||
* @package WPCode
|
||||
*/
|
||||
|
||||
/**
|
||||
* Abstract class WPCode_Conditional_Type
|
||||
*/
|
||||
abstract class WPCode_Conditional_Type {
|
||||
|
||||
/**
|
||||
* An array of options for this type.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $options;
|
||||
|
||||
/**
|
||||
* The type label.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $label;
|
||||
|
||||
/**
|
||||
* The type name.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name;
|
||||
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category;
|
||||
|
||||
/**
|
||||
* Constructor.
|
||||
*/
|
||||
public function __construct() {
|
||||
$this->register_type();
|
||||
$this->hooks();
|
||||
}
|
||||
|
||||
/**
|
||||
* Register conditional-logic hooks specific to each type (e.g. ajax callbacks).
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function hooks() {
|
||||
}
|
||||
|
||||
/**
|
||||
* Register this instance to the global auto-insert types.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
private function register_type() {
|
||||
wpcode()->conditional_logic->register_type( $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the options for this type.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function get_type_options() {
|
||||
if ( ! isset( $this->options ) ) {
|
||||
// If we're not in a context where translations are safe to load,
|
||||
// and this method exists, load evaluation-only options.
|
||||
if ( ! $this->can_load_translations() && method_exists( $this, 'load_evaluation_options' ) ) {
|
||||
$this->load_evaluation_options();
|
||||
} else {
|
||||
$this->load_type_options();
|
||||
}
|
||||
}
|
||||
|
||||
return $this->options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if we can safely load translations.
|
||||
* Translations should only be loaded after 'init' hook or in admin context.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function can_load_translations() {
|
||||
|
||||
// If 'init' hook has already fired, translations are loaded.
|
||||
if ( did_action( 'init' ) ) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Otherwise, it's too early to load translations.
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type label with a translatable string.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract protected function set_label();
|
||||
|
||||
/**
|
||||
* Load the options for this type of conditions.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
abstract public function load_type_options();
|
||||
|
||||
/**
|
||||
* Get the label.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_label() {
|
||||
if ( ! isset( $this->label ) ) {
|
||||
// Only load translated label if in appropriate context.
|
||||
if ( $this->can_load_translations() ) {
|
||||
$this->set_label();
|
||||
} else {
|
||||
// Return the type name as fallback before translations are loaded.
|
||||
return $this->name;
|
||||
}
|
||||
}
|
||||
|
||||
return $this->label;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the category.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_category() {
|
||||
return $this->category;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the type name.
|
||||
*
|
||||
* @return string
|
||||
*/
|
||||
public function get_name() {
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Process a rule group specific to the conditions type.
|
||||
*
|
||||
* @param array $rule_group An array of rules with keys option,relation and value.
|
||||
* @param WPCode_Snippet $snippet The snippet we are evaluating the rules for.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
public function evaluate_rule_row( $rule_group, $snippet ) {
|
||||
return $this->evaluate_rule( $rule_group['option'], $rule_group['relation'], $rule_group['value'], $snippet );
|
||||
}
|
||||
|
||||
/**
|
||||
* This takes an option name from the list of options for the type
|
||||
* and if it finds it, it executes the callback defined in the list of
|
||||
* options and compares that value to the set value using the operator
|
||||
* set in the settings.
|
||||
*
|
||||
* @param string $option The option to evaluate.
|
||||
* @param string $relation The comparison relation.
|
||||
* @param string $value The selected value for this condition.
|
||||
* @param WPCode_Snippet $snippet The snippet we are evaluating the rules for.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function evaluate_rule( $option, $relation, $value, $snippet ) {
|
||||
$options = $this->get_type_options();
|
||||
if ( ! isset( $options [ $option ] ) ) {
|
||||
return true;
|
||||
}
|
||||
$option_details = $options[ $option ];
|
||||
|
||||
if ( ! isset( $option_details['callback'] ) ) {
|
||||
return false;
|
||||
}
|
||||
$callback = $option_details['callback'];
|
||||
if ( ! is_callable( $callback ) ) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return $this->get_relation_comparison( $callback( $snippet ), $value, $relation );
|
||||
}
|
||||
|
||||
/**
|
||||
* Takes 2 values and an operator and finds the appropriate function
|
||||
* to evaluate the relation between them.
|
||||
*
|
||||
* @param mixed $value1 This is the first value to compare with value 2.
|
||||
* @param mixed $value2 This is the 2nd value.
|
||||
* @param string $operator This is the operator string.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
protected function get_relation_comparison( $value1, $value2, $operator ) {
|
||||
switch ( $operator ) {
|
||||
case '=':
|
||||
$result = $this->equals( $value1, $value2 );
|
||||
break;
|
||||
case '!=':
|
||||
$result = $this->does_not_equal( $value1, $value2 );
|
||||
break;
|
||||
case 'contains':
|
||||
$result = $this->contains( $value1, $value2 );
|
||||
break;
|
||||
case 'notcontains':
|
||||
$result = ! $this->contains( $value1, $value2 );
|
||||
break;
|
||||
default:
|
||||
$result = true;
|
||||
break;
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does an equals comparison (not strict), also handles arrays to
|
||||
* make it easier to compare things like user roles.
|
||||
*
|
||||
* @param mixed $value1 Value 1.
|
||||
* @param mixed $value2 Value to compare value 1 to.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function equals( $value1, $value2 ) {
|
||||
if ( is_array( $value1 ) ) {
|
||||
if ( is_array( $value2 ) ) {
|
||||
return count( array_intersect( $value1, $value2 ) ) > 0;
|
||||
}
|
||||
|
||||
return in_array( $value2, $value1 );
|
||||
}
|
||||
if ( is_array( $value2 ) ) {
|
||||
return in_array( $value1, $value2 );
|
||||
}
|
||||
|
||||
return $value1 == $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Does a "does not equal" comparison (not strict), also handles arrays to
|
||||
* make it easier to compare things like user roles.
|
||||
*
|
||||
* @param mixed $value1 Value 1.
|
||||
* @param mixed $value2 Value to compare value 1 to.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function does_not_equal( $value1, $value2 ) {
|
||||
if ( is_array( $value1 ) ) {
|
||||
if ( is_array( $value2 ) ) {
|
||||
return count( array_intersect( $value1, $value2 ) ) === 0;
|
||||
}
|
||||
|
||||
return ! in_array( $value2, $value1 );
|
||||
}
|
||||
|
||||
if ( is_array( $value2 ) ) {
|
||||
return ! in_array( $value1, $value2 );
|
||||
}
|
||||
|
||||
return $value1 != $value2;
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if value1 contains value2.
|
||||
*
|
||||
* @param string $value1 Value in which to look for value 2.
|
||||
* @param string $value2 The value to look for in value 1.
|
||||
*
|
||||
* @return bool
|
||||
*/
|
||||
private function contains( $value1, $value2 ) {
|
||||
if ( empty( $value2 ) ) {
|
||||
return false;
|
||||
}
|
||||
return false !== strpos( $value1, $value2 );
|
||||
}
|
||||
}
|
||||
+134
@@ -0,0 +1,134 @@
|
||||
<?php
|
||||
/**
|
||||
* Class that handles conditional logic related to users.
|
||||
*
|
||||
* @package WPCode
|
||||
*/
|
||||
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* The WPCode_Conditional_User class.
|
||||
*/
|
||||
class WPCode_Conditional_User extends WPCode_Conditional_Type {
|
||||
|
||||
/**
|
||||
* The type unique name (slug).
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $name = 'user';
|
||||
|
||||
/**
|
||||
* The category of this type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
public $category = 'who';
|
||||
|
||||
/**
|
||||
* Set the translatable label.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
protected function set_label() {
|
||||
$this->label = __( 'User', 'insert-headers-and-footers' );
|
||||
}
|
||||
|
||||
/**
|
||||
* Load evaluation-only options (without translations) for frontend use.
|
||||
* This method is called when translations are not yet loaded to avoid WP 6.7 notices.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_evaluation_options() {
|
||||
$this->options = array(
|
||||
'logged_in' => array(
|
||||
'callback' => 'is_user_logged_in',
|
||||
),
|
||||
'user_role' => array(
|
||||
'callback' => array( $this, 'get_user_role' ),
|
||||
),
|
||||
'user_meta' => array(
|
||||
'callback' => null, // Pro feature, no callback in free version.
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the type options for the admin mainly.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function load_type_options() {
|
||||
$this->options = array(
|
||||
'logged_in' => array(
|
||||
'label' => __( 'Logged-in', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Check if your site visitor is logged in.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => array(
|
||||
array(
|
||||
'label' => __( 'True', 'insert-headers-and-footers' ),
|
||||
'value' => true,
|
||||
),
|
||||
array(
|
||||
'label' => __( 'False', 'insert-headers-and-footers' ),
|
||||
'value' => false,
|
||||
),
|
||||
),
|
||||
'callback' => 'is_user_logged_in',
|
||||
),
|
||||
'user_role' => array(
|
||||
'label' => __( 'User Role', 'insert-headers-and-footers' ),
|
||||
'description' => __( 'Target a specific user role.', 'insert-headers-and-footers' ),
|
||||
'type' => 'select',
|
||||
'options' => $this->get_options_user_roles(),
|
||||
'callback' => array( $this, 'get_user_role' ),
|
||||
),
|
||||
'user_meta' => array(
|
||||
'label' => __( 'User Meta', 'insert-headers-and-footers') . ' (PRO)' ,
|
||||
'description' => __( 'Target users based on user meta values.', 'insert-headers-and-footers' ),
|
||||
'type' => 'text',
|
||||
'options' => array(),
|
||||
'upgrade' => array(
|
||||
'title' => __( 'User Meta is a Pro Feature', 'insert-headers-and-footers' ),
|
||||
'text' => __( 'Get access to User Meta conditional logic rules by upgrading to PRO today.', 'insert-headers-and-footers' ),
|
||||
'link' => wpcode_utm_url( 'https://wpcode.com/lite/', 'edit-snippet', 'conditional-logic', 'user-meta' ),
|
||||
),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a list of options for user roles.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
protected function get_options_user_roles() {
|
||||
$user_roles = wp_roles()->roles;
|
||||
$options = array();
|
||||
foreach ( $user_roles as $key => $role ) {
|
||||
$options[] = array(
|
||||
'label' => $role['name'],
|
||||
'value' => $key,
|
||||
);
|
||||
}
|
||||
|
||||
return $options;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get an array of user roles for the current user.
|
||||
*
|
||||
* @return string[]
|
||||
*/
|
||||
public function get_user_role() {
|
||||
$user = wp_get_current_user();
|
||||
|
||||
return $user->roles;
|
||||
}
|
||||
}
|
||||
|
||||
new WPCode_Conditional_User();
|
||||
Reference in New Issue
Block a user