Admin
1 week ago
Ajax
1 week ago
Asset
1 week ago
Context
1 week ago
Customizer
1 week ago
Debug_Bar
1 week ago
Dialog
1 week ago
Documentation
1 week ago
Duplicate
1 week ago
Editor
1 week ago
Image
1 week ago
JSON_LD
1 week ago
Languages
1 week ago
Log
1 week ago
Meta
1 week ago
Models
1 week ago
PUE
1 week ago
Process
1 week ago
Promoter
1 week ago
REST
1 week ago
Repository
1 week ago
Service_Providers
1 week ago
Shortcode
1 week ago
Support
1 week ago
Tabbed_View
1 week ago
Tooltip
1 week ago
Traits
1 week ago
Utils
1 week ago
Validator
1 week ago
Widget
1 week ago
Abstract_Deactivation.php
1 week ago
Abstract_Plugin_Register.php
1 week ago
App_Shop.php
1 week ago
Assets.php
1 week ago
Assets_Pipeline.php
1 week ago
Autoloader.php
1 week ago
Cache.php
1 week ago
Cache_Listener.php
1 week ago
Changelog_Reader.php
1 week ago
Container.php
1 week ago
Context.php
1 week ago
Cost_Utils.php
1 week ago
Credits.php
1 week ago
Customizer.php
1 week ago
DB_Lock.php
1 week ago
Data.php
1 week ago
Date_Utils.php
1 week ago
Db.php
1 week ago
Debug.php
1 week ago
Dependency.php
1 week ago
Deprecation.php
1 week ago
Editor.php
1 week ago
Error.php
1 week ago
Exception.php
1 week ago
Extension.php
1 week ago
Extension_Loader.php
1 week ago
Feature_Detection.php
1 week ago
Field.php
1 week ago
Field_Conditional.php
1 week ago
Freemius.php
1 week ago
Log.php
1 week ago
Main.php
1 week ago
Notices.php
1 week ago
Plugin_Meta_Links.php
1 week ago
Plugins.php
1 week ago
Plugins_API.php
1 week ago
Post_History.php
1 week ago
Post_Transient.php
1 week ago
Promise.php
1 week ago
Repository.php
1 week ago
Rewrite.php
1 week ago
Settings.php
1 week ago
Settings_Manager.php
1 week ago
Settings_Tab.php
1 week ago
Simple_Table.php
1 week ago
Support.php
1 week ago
Tabbed_View.php
1 week ago
Template.php
1 week ago
Template_Factory.php
1 week ago
Template_Part_Cache.php
1 week ago
Templates.php
1 week ago
Terms.php
1 week ago
Timezones.php
1 week ago
Tracker.php
1 week ago
Updater.php
1 week ago
Validate.php
1 week ago
View_Helpers.php
1 week ago
Field_Conditional.php
80 lines
| 1 | <?php |
| 2 | |
| 3 | /** |
| 4 | * Class Tribe__Field_Conditional |
| 5 | * |
| 6 | * A work-around for PHP 5.2 lack of closure support to wrap |
| 7 | * |
| 8 | * Example usage: in the context of the definition of a list of fields to be fed to the Tribe__Settings class: |
| 9 | * |
| 10 | * $fields = array( |
| 11 | * 'foo' => array( |
| 12 | * 'type' => 'checkbox_bool', |
| 13 | * // ... |
| 14 | * ), |
| 15 | * 'bar' => array( |
| 16 | * 'type' => 'text' |
| 17 | * 'validate_if' => new Tribe__Field_Conditional( 'foo', 'tribe_is_truthy' ) |
| 18 | * 'conditional' => tribe_is_truthy( tribe_get_option( 'foo' ) ), |
| 19 | * // ... |
| 20 | * ), |
| 21 | * ); |
| 22 | * |
| 23 | * The above will modify the validation logic to make it so that the field will not be validated if |
| 24 | * the parent (`foo` in the example) is not "truthy". |
| 25 | * If you need to hide/show the field conditionally use the `conditional` attribute of the field. |
| 26 | * |
| 27 | * @since 4.7.7 |
| 28 | */ |
| 29 | class Tribe__Field_Conditional { |
| 30 | |
| 31 | /** |
| 32 | * @var string The slug of the field the condition |
| 33 | */ |
| 34 | protected $depends_on; |
| 35 | /** |
| 36 | * @var bool |
| 37 | */ |
| 38 | protected $condition; |
| 39 | |
| 40 | /** |
| 41 | * Tribe__Field_Conditional constructor. |
| 42 | * |
| 43 | * @since 4.7.7 |
| 44 | * |
| 45 | * @param string $depends_on_field The slug or identifier of the parent field. |
| 46 | * @param mixed|callable $condition Either a valid callable function or method or a |
| 47 | * value that will be used for a shallow comparison. |
| 48 | */ |
| 49 | public function __construct( $depends_on_field, $condition = true ) { |
| 50 | $this->depends_on = $depends_on_field; |
| 51 | $this->condition = $condition; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * @param mixed $value The value to check, typically the parent field value. |
| 56 | * @param array $fields An array of all the current fields; this will be passed to |
| 57 | * the condition callback function for context if the condition |
| 58 | * is a callable function or method. |
| 59 | * |
| 60 | * @return bool Whether the check was successful (the parent field does have the required |
| 61 | * value) or not. |
| 62 | */ |
| 63 | public function check( $value, array $fields ) { |
| 64 | return is_callable( $this->condition ) |
| 65 | ? call_user_func( $this->condition, $value, $fields ) |
| 66 | : $value == $this->condition; |
| 67 | } |
| 68 | |
| 69 | /** |
| 70 | * Return the id/slug of the field this condition depends on. |
| 71 | * |
| 72 | * @since 4.7.7 |
| 73 | * |
| 74 | * @return string |
| 75 | */ |
| 76 | public function depends_on() { |
| 77 | return $this->depends_on; |
| 78 | } |
| 79 | } |
| 80 |