PluginProbe
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI / 8.0.5
GamiPress – Gamification plugin to reward points, badges & ranks in WordPress, now with AI v8.0.5
8.0.5 8.0.4 8.0.3 8.0.1 8.0.2 8.0.0 7.9.9.7 7.9.9.6 7.9.9.5 7.9.9.4 7.9.9.3 7.9.9.2 7.9.9.1 7.9.9 7.9.8 7.9.7 7.9.6 7.9.5 7.9.4 7.9.3 7.9.2 7.9.1 7.9.0 7.8.9 7.8.8 All 47 releases
← All changes | libraries/cmb2-conditional-fields/cmb2-conditional-fields.php +166 -0 7.9.4 → 8.0.5 View file →
@@ -1,0 +1,166 @@
1 +<?php
2 +/**
3 + * @package RGC\CMB2\Conditional_Fields
4 + * @author Ruben Garcia (RubenGC) <[email protected]>, GamiPress <[email protected]>
5 + * @copyright Copyright (c) Tsunoa
6 + *
7 + * Plugin Name: CMB2 Conditional Fields
8 + * Plugin URI: https://github.com/rubengc/cmb2-field-conditional-fields
9 + * GitHub Plugin URI: https://github.com/rubengc/cmb2-field-conditional-fields
10 + * Description: Able to show/hide conditional fields.
11 + * Version: 1.0.2
12 + * Author: Tsunoa
13 + * Author URI: https://tsunoa.com/
14 + * License: GPLv2+
15 + */
16 +// Exit if accessed directly
17 +if( !defined( 'ABSPATH' ) ) exit;
18 +
19 +// Prevent CMB2 autoload adding "RGC_" at start
20 +if( !class_exists( 'RGC_CMB2_Conditional_Fields' ) ) {
21 +
22 + /**
23 + * Class RGC_CMB2_Conditional_Fields
24 + */
25 + class RGC_CMB2_Conditional_Fields {
26 +
27 + /**
28 + * Current version number
29 + */
30 + const VERSION = '1.0.2';
31 +
32 + function conditions_sample() {
33 + // NOTE: DO NOT use the 'classes' parameter, use 'custom_classes' instead
34 +
35 + $field_args['show_if'] = array(
36 + // Text field
37 + 'text-field' => 'empty',
38 + 'text-field' => 'not empty',
39 + 'text-field' => '! empty',
40 + // Checkbox
41 + 'checkbox-field' => 'checked',
42 + 'checkbox-field' => 'not checked',
43 + 'checkbox-field' => '! checked',
44 + // Multicheck
45 + 'multicheck-field' => 'option_1',
46 + );
47 +
48 + $field_args['hide_if'] = array(
49 + // Text field
50 + 'text-field' => 'empty',
51 + 'text-field' => 'not empty',
52 + 'text-field' => '! empty',
53 + // Checkbox
54 + 'checkbox-field' => 'checked',
55 + 'checkbox-field' => '! checked',
56 + 'checkbox-field' => 'not checked',
57 + // Multicheck
58 + 'multicheck-field' => 'option_1',
59 + );
60 + }
61 +
62 + /**
63 + * Initialize the plugin by hooking into CMB2
64 + */
65 + public function __construct() {
66 + add_action( 'admin_enqueue_scripts', array( $this, 'setup_admin_scripts' ) );
67 +
68 + // TODO: Find a way to add this content if field has parameter 'js_controls' => true
69 + }
70 +
71 + /**
72 + * @param array $field_args Current field args
73 + * @param CMB2_Field $field Current field object
74 + * @return array
75 + */
76 + public function classes_cb( $field_args, $field ) {
77 +
78 + $classes = array();
79 +
80 + if( isset( $field_args['custom_classes'] ) ) {
81 + $classes[] = $field_args['custom_classes'];
82 + }
83 +
84 + if( ! isset( $field_args['show_if'] ) && ! isset( $field_args['hide_if'] ) ) {
85 + return $classes;
86 + }
87 +
88 + $classes[] = 'cmb-conditional-fields';
89 +
90 + if( isset( $field_args['show_if'] ) && is_array( $field_args['show_if'] ) ) {
91 + foreach( $field_args['show_if'] as $field => $condition ) {
92 +
93 + $classes[] = 'cmb-show-if-field-' . esc_attr( $field );
94 +
95 + if( is_array( $condition ) ) {
96 + foreach( $condition as $condition_value ) {
97 + $condition_value = $this->parse_condition( $condition_value );
98 +
99 + $classes[] = 'cmb-show-if-field-' . esc_attr( $field ) . '-condition-' . esc_attr( $condition_value );
100 + }
101 + } else {
102 + $condition = $this->parse_condition( $condition );
103 + $classes[] = 'cmb-show-if-field-' . esc_attr( $field ) . '-condition-' . esc_attr( $condition );
104 + }
105 +
106 +
107 + }
108 + }
109 +
110 + if( isset( $field_args['hide_if'] ) && is_array( $field_args['hide_if'] ) ) {
111 + foreach( $field_args['hide_if'] as $field => $condition ) {
112 +
113 + $classes[] = 'cmb-hide-if-field-' . esc_attr( $field );
114 +
115 + if( is_array( $condition ) ) {
116 + foreach( $condition as $condition_value ) {
117 + $condition_value = $this->parse_condition( $condition_value );
118 +
119 + $classes[] = 'cmb-hide-if-field-' . esc_attr( $field ) . '-condition-' . esc_attr( $condition_value );
120 + }
121 + } else {
122 + $condition = $this->parse_condition( $condition );
123 + $classes[] = 'cmb-hide-if-field-' . esc_attr( $field ) . '-condition-' . esc_attr( $condition );
124 + }
125 +
126 + }
127 + }
128 +
129 + return $classes;
130 + }
131 +
132 + private function parse_condition( $condition ) {
133 +
134 + // ! checked to not-checked
135 + $condition = str_replace( '! ', 'not-', $condition );
136 +
137 + // !checked to not-checked
138 + $condition = str_replace( '!', 'not-', $condition );
139 +
140 + // not checked to not-checked
141 + $condition = str_replace( ' ', '-', $condition );
142 +
143 + return $condition;
144 +
145 + }
146 +
147 + /**
148 + * Enqueue scripts and styles
149 + */
150 + public function setup_admin_scripts() {
151 + wp_register_script( 'cmb-conditional-fields', plugins_url( 'js/conditional-fields.js', __FILE__ ), array( 'jquery' ), self::VERSION, true );
152 +
153 + wp_enqueue_script( 'cmb-conditional-fields' );
154 + }
155 +
156 + }
157 +
158 + // TODO: Temporal solution to output html content
159 + function cmb_conditional_fields_classes_cb( $field_args, $field ) {
160 + $cmb2_conditional_fields = new RGC_CMB2_Conditional_Fields();
161 +
162 + return $cmb2_conditional_fields->classes_cb( $field_args, $field );
163 + }
164 +
165 + $cmb2_conditional_fields = new RGC_CMB2_Conditional_Fields();
166 +}