@@ -38,138 +38,138 @@ |
||
| 38 | 38 | * @since 4.0 |
| 39 | 39 | */ |
| 40 | 40 | if (function_exists('espresso_version')) { |
| 41 | - if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | - /** |
|
| 43 | - * espresso_duplicate_plugin_error |
|
| 44 | - * displays if more than one version of EE is activated at the same time. |
|
| 45 | - */ |
|
| 46 | - function espresso_duplicate_plugin_error() |
|
| 47 | - { |
|
| 48 | - ?> |
|
| 41 | + if (! function_exists('espresso_duplicate_plugin_error')) { |
|
| 42 | + /** |
|
| 43 | + * espresso_duplicate_plugin_error |
|
| 44 | + * displays if more than one version of EE is activated at the same time. |
|
| 45 | + */ |
|
| 46 | + function espresso_duplicate_plugin_error() |
|
| 47 | + { |
|
| 48 | + ?> |
|
| 49 | 49 | <div class="error"> |
| 50 | 50 | <p> |
| 51 | 51 | <?php |
| 52 | - echo esc_html__( |
|
| 53 | - 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | - 'event_espresso' |
|
| 55 | - ); ?> |
|
| 52 | + echo esc_html__( |
|
| 53 | + 'Can not run multiple versions of Event Espresso! One version has been automatically deactivated. Please verify that you have the correct version you want still active.', |
|
| 54 | + 'event_espresso' |
|
| 55 | + ); ?> |
|
| 56 | 56 | </p> |
| 57 | 57 | </div> |
| 58 | 58 | <?php |
| 59 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | - } |
|
| 61 | - } |
|
| 62 | - add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 59 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 60 | + } |
|
| 61 | + } |
|
| 62 | + add_action('admin_notices', 'espresso_duplicate_plugin_error', 1); |
|
| 63 | 63 | } else { |
| 64 | - define('EE_MIN_PHP_VER_REQUIRED', '7.4.0'); |
|
| 65 | - if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | - /** |
|
| 67 | - * espresso_minimum_php_version_error |
|
| 68 | - * |
|
| 69 | - * @return void |
|
| 70 | - */ |
|
| 71 | - function espresso_minimum_php_version_error() |
|
| 72 | - { |
|
| 73 | - ?> |
|
| 64 | + define('EE_MIN_PHP_VER_REQUIRED', '7.4.0'); |
|
| 65 | + if (! version_compare(PHP_VERSION, EE_MIN_PHP_VER_REQUIRED, '>=')) { |
|
| 66 | + /** |
|
| 67 | + * espresso_minimum_php_version_error |
|
| 68 | + * |
|
| 69 | + * @return void |
|
| 70 | + */ |
|
| 71 | + function espresso_minimum_php_version_error() |
|
| 72 | + { |
|
| 73 | + ?> |
|
| 74 | 74 | <div class="error"> |
| 75 | 75 | <p> |
| 76 | 76 | <?php |
| 77 | - printf( |
|
| 78 | - esc_html__( |
|
| 79 | - 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | - 'event_espresso' |
|
| 81 | - ), |
|
| 82 | - EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | - PHP_VERSION, |
|
| 84 | - '<br/>', |
|
| 85 | - '<a href="https://www.php.net/downloads.php">https://php.net/downloads.php</a>' |
|
| 86 | - ); |
|
| 87 | - ?> |
|
| 77 | + printf( |
|
| 78 | + esc_html__( |
|
| 79 | + 'We\'re sorry, but Event Espresso requires PHP version %1$s or greater in order to operate. You are currently running version %2$s.%3$sIn order to update your version of PHP, you will need to contact your current hosting provider.%3$sFor information on stable PHP versions, please go to %4$s.', |
|
| 80 | + 'event_espresso' |
|
| 81 | + ), |
|
| 82 | + EE_MIN_PHP_VER_REQUIRED, |
|
| 83 | + PHP_VERSION, |
|
| 84 | + '<br/>', |
|
| 85 | + '<a href="https://www.php.net/downloads.php">https://php.net/downloads.php</a>' |
|
| 86 | + ); |
|
| 87 | + ?> |
|
| 88 | 88 | </p> |
| 89 | 89 | </div> |
| 90 | 90 | <?php |
| 91 | - espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | - } |
|
| 91 | + espresso_deactivate_plugin(plugin_basename(__FILE__)); |
|
| 92 | + } |
|
| 93 | 93 | |
| 94 | - add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | - } else { |
|
| 96 | - define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 94 | + add_action('admin_notices', 'espresso_minimum_php_version_error', 1); |
|
| 95 | + } else { |
|
| 96 | + define('EVENT_ESPRESSO_MAIN_FILE', __FILE__); |
|
| 97 | 97 | |
| 98 | - require_once __DIR__ . '/vendor/autoload.php'; |
|
| 98 | + require_once __DIR__ . '/vendor/autoload.php'; |
|
| 99 | 99 | |
| 100 | - /** |
|
| 101 | - * espresso_version |
|
| 102 | - * Returns the plugin version |
|
| 103 | - * |
|
| 104 | - * @return string |
|
| 105 | - */ |
|
| 106 | - function espresso_version(): string |
|
| 107 | - { |
|
| 108 | - return apply_filters('FHEE__espresso__espresso_version', '5.0.49'); |
|
| 109 | - } |
|
| 100 | + /** |
|
| 101 | + * espresso_version |
|
| 102 | + * Returns the plugin version |
|
| 103 | + * |
|
| 104 | + * @return string |
|
| 105 | + */ |
|
| 106 | + function espresso_version(): string |
|
| 107 | + { |
|
| 108 | + return apply_filters('FHEE__espresso__espresso_version', '5.0.49'); |
|
| 109 | + } |
|
| 110 | 110 | |
| 111 | - /** |
|
| 112 | - * espresso_plugin_activation |
|
| 113 | - * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 114 | - */ |
|
| 115 | - function espresso_plugin_activation() |
|
| 116 | - { |
|
| 117 | - update_option('ee_espresso_activation', true); |
|
| 118 | - update_option('event-espresso-core_allow_tracking', 'no'); |
|
| 119 | - update_option('event-espresso-core_tracking_notice', 'hide'); |
|
| 120 | - // Run WP GraphQL activation callback |
|
| 121 | - espressoLoadWpGraphQL(); |
|
| 122 | - graphql_activation_callback(); |
|
| 123 | - } |
|
| 111 | + /** |
|
| 112 | + * espresso_plugin_activation |
|
| 113 | + * adds a wp-option to indicate that EE has been activated via the WP admin plugins page |
|
| 114 | + */ |
|
| 115 | + function espresso_plugin_activation() |
|
| 116 | + { |
|
| 117 | + update_option('ee_espresso_activation', true); |
|
| 118 | + update_option('event-espresso-core_allow_tracking', 'no'); |
|
| 119 | + update_option('event-espresso-core_tracking_notice', 'hide'); |
|
| 120 | + // Run WP GraphQL activation callback |
|
| 121 | + espressoLoadWpGraphQL(); |
|
| 122 | + graphql_activation_callback(); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | - register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 125 | + register_activation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_activation'); |
|
| 126 | 126 | |
| 127 | - /** |
|
| 128 | - * espresso_plugin_deactivation |
|
| 129 | - */ |
|
| 130 | - function espresso_plugin_deactivation() |
|
| 131 | - { |
|
| 132 | - // Run WP GraphQL deactivation callback |
|
| 133 | - espressoLoadWpGraphQL(); |
|
| 134 | - graphql_deactivation_callback(); |
|
| 135 | - delete_option('event-espresso-core_allow_tracking'); |
|
| 136 | - delete_option('event-espresso-core_tracking_notice'); |
|
| 137 | - } |
|
| 138 | - register_deactivation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_deactivation'); |
|
| 127 | + /** |
|
| 128 | + * espresso_plugin_deactivation |
|
| 129 | + */ |
|
| 130 | + function espresso_plugin_deactivation() |
|
| 131 | + { |
|
| 132 | + // Run WP GraphQL deactivation callback |
|
| 133 | + espressoLoadWpGraphQL(); |
|
| 134 | + graphql_deactivation_callback(); |
|
| 135 | + delete_option('event-espresso-core_allow_tracking'); |
|
| 136 | + delete_option('event-espresso-core_tracking_notice'); |
|
| 137 | + } |
|
| 138 | + register_deactivation_hook(EVENT_ESPRESSO_MAIN_FILE, 'espresso_plugin_deactivation'); |
|
| 139 | 139 | |
| 140 | - require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 141 | - bootstrap_espresso(); |
|
| 142 | - } |
|
| 140 | + require_once __DIR__ . '/core/bootstrap_espresso.php'; |
|
| 141 | + bootstrap_espresso(); |
|
| 142 | + } |
|
| 143 | 143 | } |
| 144 | 144 | |
| 145 | 145 | if (! function_exists('espresso_deactivate_plugin')) { |
| 146 | - /** |
|
| 147 | - * deactivate_plugin |
|
| 148 | - * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 149 | - * |
|
| 150 | - * @access public |
|
| 151 | - * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 152 | - * @return void |
|
| 153 | - */ |
|
| 154 | - function espresso_deactivate_plugin(string $plugin_basename = '') |
|
| 155 | - { |
|
| 156 | - if (! function_exists('deactivate_plugins')) { |
|
| 157 | - require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 158 | - } |
|
| 159 | - unset($_GET['activate'], $_REQUEST['activate']); |
|
| 160 | - deactivate_plugins($plugin_basename); |
|
| 161 | - } |
|
| 146 | + /** |
|
| 147 | + * deactivate_plugin |
|
| 148 | + * usage: espresso_deactivate_plugin( plugin_basename( __FILE__ )); |
|
| 149 | + * |
|
| 150 | + * @access public |
|
| 151 | + * @param string $plugin_basename - the results of plugin_basename( __FILE__ ) for the plugin's main file |
|
| 152 | + * @return void |
|
| 153 | + */ |
|
| 154 | + function espresso_deactivate_plugin(string $plugin_basename = '') |
|
| 155 | + { |
|
| 156 | + if (! function_exists('deactivate_plugins')) { |
|
| 157 | + require_once ABSPATH . 'wp-admin/includes/plugin.php'; |
|
| 158 | + } |
|
| 159 | + unset($_GET['activate'], $_REQUEST['activate']); |
|
| 160 | + deactivate_plugins($plugin_basename); |
|
| 161 | + } |
|
| 162 | 162 | } |
| 163 | 163 | |
| 164 | 164 | |
| 165 | 165 | if (! function_exists('espressoLoadWpGraphQL')) { |
| 166 | - function espressoLoadWpGraphQL() |
|
| 167 | - { |
|
| 168 | - if ( |
|
| 169 | - ! class_exists('WPGraphQL') |
|
| 170 | - && is_readable(__DIR__ . '/vendor/wp-graphql/wp-graphql/wp-graphql.php') |
|
| 171 | - ) { |
|
| 172 | - require_once __DIR__ . '/vendor/wp-graphql/wp-graphql/wp-graphql.php'; |
|
| 173 | - } |
|
| 174 | - } |
|
| 166 | + function espressoLoadWpGraphQL() |
|
| 167 | + { |
|
| 168 | + if ( |
|
| 169 | + ! class_exists('WPGraphQL') |
|
| 170 | + && is_readable(__DIR__ . '/vendor/wp-graphql/wp-graphql/wp-graphql.php') |
|
| 171 | + ) { |
|
| 172 | + require_once __DIR__ . '/vendor/wp-graphql/wp-graphql/wp-graphql.php'; |
|
| 173 | + } |
|
| 174 | + } |
|
| 175 | 175 | } |
@@ -36,239 +36,239 @@ |
||
| 36 | 36 | */ |
| 37 | 37 | abstract class EE_Restriction_Generator_Base |
| 38 | 38 | { |
| 39 | - /** |
|
| 40 | - * One of EEM_Base::valid_cap_contexts() |
|
| 41 | - * |
|
| 42 | - * @var string|null |
|
| 43 | - */ |
|
| 44 | - protected ?string $_action = ''; |
|
| 39 | + /** |
|
| 40 | + * One of EEM_Base::valid_cap_contexts() |
|
| 41 | + * |
|
| 42 | + * @var string|null |
|
| 43 | + */ |
|
| 44 | + protected ?string $_action = ''; |
|
| 45 | 45 | |
| 46 | - /** |
|
| 47 | - * The restrictions generated by this object, null before any are made (previously false). |
|
| 48 | - * |
|
| 49 | - * @var EE_Default_Where_Conditions[]|bool|null |
|
| 50 | - */ |
|
| 51 | - protected $_cap_restrictions_generated = null; |
|
| 46 | + /** |
|
| 47 | + * The restrictions generated by this object, null before any are made (previously false). |
|
| 48 | + * |
|
| 49 | + * @var EE_Default_Where_Conditions[]|bool|null |
|
| 50 | + */ |
|
| 51 | + protected $_cap_restrictions_generated = null; |
|
| 52 | 52 | |
| 53 | - /** |
|
| 54 | - * Model for which restrictions are generated |
|
| 55 | - * |
|
| 56 | - * @var EEM_Base|null |
|
| 57 | - */ |
|
| 58 | - protected ?EEM_Base $_model = null; |
|
| 53 | + /** |
|
| 54 | + * Model for which restrictions are generated |
|
| 55 | + * |
|
| 56 | + * @var EEM_Base|null |
|
| 57 | + */ |
|
| 58 | + protected ?EEM_Base $_model = null; |
|
| 59 | 59 | |
| 60 | 60 | |
| 61 | - private static ?array $caps_for_admin = null; |
|
| 61 | + private static ?array $caps_for_admin = null; |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | - /** |
|
| 65 | - * Puts the last necessary info onto the restriction generator class. This |
|
| 66 | - * is usually called by EEM_Base during its constructor, so child classes |
|
| 67 | - * don't have to always provide this info. |
|
| 68 | - * |
|
| 69 | - * @param EEM_Base $model |
|
| 70 | - * @param string $action |
|
| 71 | - */ |
|
| 72 | - public function _construct_finalize(EEM_Base $model, string $action) |
|
| 73 | - { |
|
| 74 | - $this->_model = $model; |
|
| 75 | - $this->_action = $action; |
|
| 76 | - } |
|
| 64 | + /** |
|
| 65 | + * Puts the last necessary info onto the restriction generator class. This |
|
| 66 | + * is usually called by EEM_Base during its constructor, so child classes |
|
| 67 | + * don't have to always provide this info. |
|
| 68 | + * |
|
| 69 | + * @param EEM_Base $model |
|
| 70 | + * @param string $action |
|
| 71 | + */ |
|
| 72 | + public function _construct_finalize(EEM_Base $model, string $action) |
|
| 73 | + { |
|
| 74 | + $this->_model = $model; |
|
| 75 | + $this->_action = $action; |
|
| 76 | + } |
|
| 77 | 77 | |
| 78 | 78 | |
| 79 | - /** |
|
| 80 | - * Returns the model set for this restriction generator |
|
| 81 | - * |
|
| 82 | - * @return EEM_Base |
|
| 83 | - * @throws EE_Error |
|
| 84 | - */ |
|
| 85 | - public function model(): EEM_Base |
|
| 86 | - { |
|
| 87 | - if ($this->_model instanceof EEM_Base) { |
|
| 88 | - return $this->_model; |
|
| 89 | - } |
|
| 90 | - throw new EE_Error( |
|
| 91 | - sprintf( |
|
| 92 | - esc_html__( |
|
| 93 | - 'Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', |
|
| 94 | - 'event_espresso' |
|
| 95 | - ), |
|
| 96 | - get_class($this) |
|
| 97 | - ) |
|
| 98 | - ); |
|
| 99 | - } |
|
| 79 | + /** |
|
| 80 | + * Returns the model set for this restriction generator |
|
| 81 | + * |
|
| 82 | + * @return EEM_Base |
|
| 83 | + * @throws EE_Error |
|
| 84 | + */ |
|
| 85 | + public function model(): EEM_Base |
|
| 86 | + { |
|
| 87 | + if ($this->_model instanceof EEM_Base) { |
|
| 88 | + return $this->_model; |
|
| 89 | + } |
|
| 90 | + throw new EE_Error( |
|
| 91 | + sprintf( |
|
| 92 | + esc_html__( |
|
| 93 | + 'Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', |
|
| 94 | + 'event_espresso' |
|
| 95 | + ), |
|
| 96 | + get_class($this) |
|
| 97 | + ) |
|
| 98 | + ); |
|
| 99 | + } |
|
| 100 | 100 | |
| 101 | 101 | |
| 102 | - /** |
|
| 103 | - * Returns the action this restriction generator will generate restrictions for. |
|
| 104 | - * It should be one of EEM_Base::valid_cap_contexts() |
|
| 105 | - * |
|
| 106 | - * @return string |
|
| 107 | - * @throws EE_Error |
|
| 108 | - */ |
|
| 109 | - public function action(): string |
|
| 110 | - { |
|
| 111 | - if ($this->_action) { |
|
| 112 | - return $this->_action; |
|
| 113 | - } |
|
| 114 | - throw new EE_Error( |
|
| 115 | - sprintf( |
|
| 116 | - esc_html__( |
|
| 117 | - 'Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', |
|
| 118 | - 'event_espresso' |
|
| 119 | - ), |
|
| 120 | - get_class($this) |
|
| 121 | - ) |
|
| 122 | - ); |
|
| 123 | - } |
|
| 102 | + /** |
|
| 103 | + * Returns the action this restriction generator will generate restrictions for. |
|
| 104 | + * It should be one of EEM_Base::valid_cap_contexts() |
|
| 105 | + * |
|
| 106 | + * @return string |
|
| 107 | + * @throws EE_Error |
|
| 108 | + */ |
|
| 109 | + public function action(): string |
|
| 110 | + { |
|
| 111 | + if ($this->_action) { |
|
| 112 | + return $this->_action; |
|
| 113 | + } |
|
| 114 | + throw new EE_Error( |
|
| 115 | + sprintf( |
|
| 116 | + esc_html__( |
|
| 117 | + 'Cannot generate capability restrictions because model has not yet been set on the %s. Please ensure _construct_finalize() was called', |
|
| 118 | + 'event_espresso' |
|
| 119 | + ), |
|
| 120 | + get_class($this) |
|
| 121 | + ) |
|
| 122 | + ); |
|
| 123 | + } |
|
| 124 | 124 | |
| 125 | 125 | |
| 126 | - /** |
|
| 127 | - * Returns whether _construct_finalize() has been called on this |
|
| 128 | - * restriction generator object |
|
| 129 | - * |
|
| 130 | - * @return boolean |
|
| 131 | - */ |
|
| 132 | - public function construction_finalized(): bool |
|
| 133 | - { |
|
| 134 | - return $this->_model instanceof EEM_Base && $this->_action; |
|
| 135 | - } |
|
| 126 | + /** |
|
| 127 | + * Returns whether _construct_finalize() has been called on this |
|
| 128 | + * restriction generator object |
|
| 129 | + * |
|
| 130 | + * @return boolean |
|
| 131 | + */ |
|
| 132 | + public function construction_finalized(): bool |
|
| 133 | + { |
|
| 134 | + return $this->_model instanceof EEM_Base && $this->_action; |
|
| 135 | + } |
|
| 136 | 136 | |
| 137 | 137 | |
| 138 | - /** |
|
| 139 | - * Gets the capability restrictions generated by this object. Caches them in |
|
| 140 | - * case they are required for subsequent requests |
|
| 141 | - * |
|
| 142 | - * @return array @see EEM_Base::_cap_restrictions |
|
| 143 | - */ |
|
| 144 | - public function generate_restrictions(): array |
|
| 145 | - { |
|
| 146 | - if ($this->_cap_restrictions_generated === null) { |
|
| 147 | - $this->_cap_restrictions_generated = apply_filters( |
|
| 148 | - 'FHEE__EE_Restriction_Generator_Base__generate_restrictions__first_time', |
|
| 149 | - $this->_generate_restrictions(), |
|
| 150 | - $this |
|
| 151 | - ); |
|
| 152 | - } |
|
| 153 | - return apply_filters( |
|
| 154 | - 'FHEE__EE_Restriction_Generator_Base__generate_restrictions__every_time', |
|
| 155 | - $this->_cap_restrictions_generated, |
|
| 156 | - $this |
|
| 157 | - ); |
|
| 158 | - } |
|
| 138 | + /** |
|
| 139 | + * Gets the capability restrictions generated by this object. Caches them in |
|
| 140 | + * case they are required for subsequent requests |
|
| 141 | + * |
|
| 142 | + * @return array @see EEM_Base::_cap_restrictions |
|
| 143 | + */ |
|
| 144 | + public function generate_restrictions(): array |
|
| 145 | + { |
|
| 146 | + if ($this->_cap_restrictions_generated === null) { |
|
| 147 | + $this->_cap_restrictions_generated = apply_filters( |
|
| 148 | + 'FHEE__EE_Restriction_Generator_Base__generate_restrictions__first_time', |
|
| 149 | + $this->_generate_restrictions(), |
|
| 150 | + $this |
|
| 151 | + ); |
|
| 152 | + } |
|
| 153 | + return apply_filters( |
|
| 154 | + 'FHEE__EE_Restriction_Generator_Base__generate_restrictions__every_time', |
|
| 155 | + $this->_cap_restrictions_generated, |
|
| 156 | + $this |
|
| 157 | + ); |
|
| 158 | + } |
|
| 159 | 159 | |
| 160 | 160 | |
| 161 | - /** |
|
| 162 | - * Provided with the model, and using global knowledge about what |
|
| 163 | - * capabilities exist, generates an array for use in one of the subarrays |
|
| 164 | - * in EEM_Base::_cap_restrictions, where keys are capability names, and |
|
| 165 | - * values are children of EE_Default_Where_Conditions |
|
| 166 | - * |
|
| 167 | - * @return EE_Default_Where_Conditions[]|EE_Return_None_Where_Conditions[] |
|
| 168 | - * @see EEM_Base::_cap_restrictions |
|
| 169 | - */ |
|
| 170 | - abstract protected function _generate_restrictions(): array; |
|
| 161 | + /** |
|
| 162 | + * Provided with the model, and using global knowledge about what |
|
| 163 | + * capabilities exist, generates an array for use in one of the subarrays |
|
| 164 | + * in EEM_Base::_cap_restrictions, where keys are capability names, and |
|
| 165 | + * values are children of EE_Default_Where_Conditions |
|
| 166 | + * |
|
| 167 | + * @return EE_Default_Where_Conditions[]|EE_Return_None_Where_Conditions[] |
|
| 168 | + * @see EEM_Base::_cap_restrictions |
|
| 169 | + */ |
|
| 170 | + abstract protected function _generate_restrictions(): array; |
|
| 171 | 171 | |
| 172 | 172 | |
| 173 | - /** |
|
| 174 | - * Whether this restriction generator has already done its job of |
|
| 175 | - * making restrictions and caching them on itself in case its asked later |
|
| 176 | - * |
|
| 177 | - * @return boolean |
|
| 178 | - */ |
|
| 179 | - public function has_generated_cap_restrictions(): bool |
|
| 180 | - { |
|
| 181 | - return $this->_cap_restrictions_generated !== null; |
|
| 182 | - } |
|
| 173 | + /** |
|
| 174 | + * Whether this restriction generator has already done its job of |
|
| 175 | + * making restrictions and caching them on itself in case its asked later |
|
| 176 | + * |
|
| 177 | + * @return boolean |
|
| 178 | + */ |
|
| 179 | + public function has_generated_cap_restrictions(): bool |
|
| 180 | + { |
|
| 181 | + return $this->_cap_restrictions_generated !== null; |
|
| 182 | + } |
|
| 183 | 183 | |
| 184 | 184 | |
| 185 | - /** |
|
| 186 | - * Given an action like 'edit' generates the cap name based off |
|
| 187 | - * the EEM_Base::_cap_slug, which for events would be 'events', to generate |
|
| 188 | - * the cap name like 'ee_edit_events'. If a $qualifier is passed, |
|
| 189 | - * |
|
| 190 | - * @param EEM_Base $model |
|
| 191 | - * @param string $action |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - public static function get_cap_name(EEM_Base $model, string $action): string |
|
| 195 | - { |
|
| 196 | - $prefix = $model->is_wp_core_model() ? '' : 'ee_'; |
|
| 197 | - $cap_slug = $model->cap_slug(); |
|
| 198 | - return apply_filters( |
|
| 199 | - 'FHEE__EE_Restriction_Generator__get_cap_name', |
|
| 200 | - "$prefix{$action}_$cap_slug", |
|
| 201 | - $model, |
|
| 202 | - $action |
|
| 203 | - ); |
|
| 204 | - } |
|
| 185 | + /** |
|
| 186 | + * Given an action like 'edit' generates the cap name based off |
|
| 187 | + * the EEM_Base::_cap_slug, which for events would be 'events', to generate |
|
| 188 | + * the cap name like 'ee_edit_events'. If a $qualifier is passed, |
|
| 189 | + * |
|
| 190 | + * @param EEM_Base $model |
|
| 191 | + * @param string $action |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + public static function get_cap_name(EEM_Base $model, string $action): string |
|
| 195 | + { |
|
| 196 | + $prefix = $model->is_wp_core_model() ? '' : 'ee_'; |
|
| 197 | + $cap_slug = $model->cap_slug(); |
|
| 198 | + return apply_filters( |
|
| 199 | + 'FHEE__EE_Restriction_Generator__get_cap_name', |
|
| 200 | + "$prefix{$action}_$cap_slug", |
|
| 201 | + $model, |
|
| 202 | + $action |
|
| 203 | + ); |
|
| 204 | + } |
|
| 205 | 205 | |
| 206 | 206 | |
| 207 | - /** |
|
| 208 | - * Checks if there is a cap for this model and this action |
|
| 209 | - * |
|
| 210 | - * @param EEM_Base $model |
|
| 211 | - * @param string $action |
|
| 212 | - * @return boolean |
|
| 213 | - * @throws EE_Error |
|
| 214 | - */ |
|
| 215 | - public static function is_cap(EEM_Base $model, string $action): bool |
|
| 216 | - { |
|
| 217 | - if (self::$caps_for_admin === null) { |
|
| 218 | - self::$caps_for_admin = EE_Registry::instance()->CAP->get_ee_capabilities(); |
|
| 219 | - } |
|
| 220 | - return in_array(self::get_cap_name($model, $action), self::$caps_for_admin); |
|
| 221 | - } |
|
| 207 | + /** |
|
| 208 | + * Checks if there is a cap for this model and this action |
|
| 209 | + * |
|
| 210 | + * @param EEM_Base $model |
|
| 211 | + * @param string $action |
|
| 212 | + * @return boolean |
|
| 213 | + * @throws EE_Error |
|
| 214 | + */ |
|
| 215 | + public static function is_cap(EEM_Base $model, string $action): bool |
|
| 216 | + { |
|
| 217 | + if (self::$caps_for_admin === null) { |
|
| 218 | + self::$caps_for_admin = EE_Registry::instance()->CAP->get_ee_capabilities(); |
|
| 219 | + } |
|
| 220 | + return in_array(self::get_cap_name($model, $action), self::$caps_for_admin); |
|
| 221 | + } |
|
| 222 | 222 | |
| 223 | 223 | |
| 224 | - /** |
|
| 225 | - * Returns the default capability used to determine if the current user can |
|
| 226 | - * access something. |
|
| 227 | - * |
|
| 228 | - * @return string |
|
| 229 | - */ |
|
| 230 | - public static function get_default_restrictions_cap(): string |
|
| 231 | - { |
|
| 232 | - return apply_filters( |
|
| 233 | - 'FHEE__EE_Restriction_Generator_Base__default_restrictions_cap', |
|
| 234 | - 'manage_options' |
|
| 235 | - ); |
|
| 236 | - } |
|
| 224 | + /** |
|
| 225 | + * Returns the default capability used to determine if the current user can |
|
| 226 | + * access something. |
|
| 227 | + * |
|
| 228 | + * @return string |
|
| 229 | + */ |
|
| 230 | + public static function get_default_restrictions_cap(): string |
|
| 231 | + { |
|
| 232 | + return apply_filters( |
|
| 233 | + 'FHEE__EE_Restriction_Generator_Base__default_restrictions_cap', |
|
| 234 | + 'manage_options' |
|
| 235 | + ); |
|
| 236 | + } |
|
| 237 | 237 | |
| 238 | 238 | |
| 239 | - /** |
|
| 240 | - * Gets WHERE conditions for the query that show the post model is |
|
| 241 | - * published, or that it's sold out and it was previously published |
|
| 242 | - * |
|
| 243 | - * @param array $where_conditions |
|
| 244 | - * @param boolean $check_if_published If true, will add conditions like status=publish |
|
| 245 | - * If false, will add conditions like status!=private |
|
| 246 | - * @param string $path_to_event_model including the period at the end |
|
| 247 | - * @return array |
|
| 248 | - * @throws EE_Error |
|
| 249 | - */ |
|
| 250 | - protected function addPublishedPostConditions( |
|
| 251 | - array $where_conditions = [], |
|
| 252 | - bool $check_if_published = true, |
|
| 253 | - string $path_to_event_model = '' |
|
| 254 | - ): array { |
|
| 255 | - $published_value = $check_if_published ? 'publish' : ['!=', 'private']; |
|
| 256 | - // only add a check for the previous event status |
|
| 257 | - // if the model is the event or it's related to the event model |
|
| 258 | - if ( |
|
| 259 | - $this->model() instanceof EEM_Event |
|
| 260 | - || strpos($path_to_event_model, 'Event') !== false |
|
| 261 | - ) { |
|
| 262 | - $where_conditions['OR*status'] = [ |
|
| 263 | - "{$path_to_event_model}status" => $published_value, |
|
| 264 | - 'AND' => [ |
|
| 265 | - "{$path_to_event_model}Post_Meta.meta_key" => '_previous_event_status', |
|
| 266 | - "{$path_to_event_model}Post_Meta.meta_value" => $published_value, |
|
| 267 | - ], |
|
| 268 | - ]; |
|
| 269 | - } else { |
|
| 270 | - $where_conditions[ "{$path_to_event_model}status" ] = $published_value; |
|
| 271 | - } |
|
| 272 | - return $where_conditions; |
|
| 273 | - } |
|
| 239 | + /** |
|
| 240 | + * Gets WHERE conditions for the query that show the post model is |
|
| 241 | + * published, or that it's sold out and it was previously published |
|
| 242 | + * |
|
| 243 | + * @param array $where_conditions |
|
| 244 | + * @param boolean $check_if_published If true, will add conditions like status=publish |
|
| 245 | + * If false, will add conditions like status!=private |
|
| 246 | + * @param string $path_to_event_model including the period at the end |
|
| 247 | + * @return array |
|
| 248 | + * @throws EE_Error |
|
| 249 | + */ |
|
| 250 | + protected function addPublishedPostConditions( |
|
| 251 | + array $where_conditions = [], |
|
| 252 | + bool $check_if_published = true, |
|
| 253 | + string $path_to_event_model = '' |
|
| 254 | + ): array { |
|
| 255 | + $published_value = $check_if_published ? 'publish' : ['!=', 'private']; |
|
| 256 | + // only add a check for the previous event status |
|
| 257 | + // if the model is the event or it's related to the event model |
|
| 258 | + if ( |
|
| 259 | + $this->model() instanceof EEM_Event |
|
| 260 | + || strpos($path_to_event_model, 'Event') !== false |
|
| 261 | + ) { |
|
| 262 | + $where_conditions['OR*status'] = [ |
|
| 263 | + "{$path_to_event_model}status" => $published_value, |
|
| 264 | + 'AND' => [ |
|
| 265 | + "{$path_to_event_model}Post_Meta.meta_key" => '_previous_event_status', |
|
| 266 | + "{$path_to_event_model}Post_Meta.meta_value" => $published_value, |
|
| 267 | + ], |
|
| 268 | + ]; |
|
| 269 | + } else { |
|
| 270 | + $where_conditions[ "{$path_to_event_model}status" ] = $published_value; |
|
| 271 | + } |
|
| 272 | + return $where_conditions; |
|
| 273 | + } |
|
| 274 | 274 | } |
@@ -58,7 +58,7 @@ discard block |
||
| 58 | 58 | protected ?EEM_Base $_model = null; |
| 59 | 59 | |
| 60 | 60 | |
| 61 | - private static ?array $caps_for_admin = null; |
|
| 61 | + private static ? array $caps_for_admin = null; |
|
| 62 | 62 | |
| 63 | 63 | |
| 64 | 64 | /** |
@@ -267,7 +267,7 @@ discard block |
||
| 267 | 267 | ], |
| 268 | 268 | ]; |
| 269 | 269 | } else { |
| 270 | - $where_conditions[ "{$path_to_event_model}status" ] = $published_value; |
|
| 270 | + $where_conditions["{$path_to_event_model}status"] = $published_value; |
|
| 271 | 271 | } |
| 272 | 272 | return $where_conditions; |
| 273 | 273 | } |
@@ -15,482 +15,482 @@ |
||
| 15 | 15 | */ |
| 16 | 16 | abstract class EE_Shortcodes extends EE_Base |
| 17 | 17 | { |
| 18 | - /** |
|
| 19 | - * matches a full shortcode including shortcodes with attributes or dynamic shortcodes |
|
| 20 | - * examples: |
|
| 21 | - * [RECIPIENT_LNAME] |
|
| 22 | - * [RECEIPT_URL download=true] |
|
| 23 | - * [PAYMENT_LINK_IF_NEEDED_* custom_text='pay me now man!'] |
|
| 24 | - */ |
|
| 25 | - public const REGEX_SHORTCODE_FULL = '/(\[.+?])/'; |
|
| 26 | - |
|
| 27 | - /** |
|
| 28 | - * matches the opening [ plus the shortcode name, but nothing else |
|
| 29 | - * examples: |
|
| 30 | - * [RECIPIENT_LNAME] matches as [RECIPIENT_LNAME |
|
| 31 | - * [RECEIPT_URL download=true] matches as [RECEIPT_URL |
|
| 32 | - * [PAYMENT_LINK_IF_NEEDED_* custom_text='pay me now man!'] matches as [PAYMENT_LINK_IF_NEEDED_* |
|
| 33 | - */ |
|
| 34 | - public const REGEX_SHORTCODE_NAME_ONLY = '/\[[A-Z\d_*]+/'; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * matches the opening [ plus the dynamic shortcode name including _*, but nothing else |
|
| 38 | - * examples: |
|
| 39 | - * [RECIPIENT_LNAME] does not match |
|
| 40 | - * [RECEIPT_URL download=true] does not match |
|
| 41 | - * [PAYMENT_LINK_IF_NEEDED_* custom_text='pay me now man!'] matches as [PAYMENT_LINK_IF_NEEDED_* |
|
| 42 | - */ |
|
| 43 | - public const REGEX_SHORTCODE_DYNAMIC = '/(\[[A-Z_]+_\*)/'; |
|
| 44 | - |
|
| 45 | - |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * holds label for library |
|
| 49 | - * This is used for referencing the library label |
|
| 50 | - * |
|
| 51 | - * @var string |
|
| 52 | - */ |
|
| 53 | - public string $label; |
|
| 54 | - |
|
| 55 | - |
|
| 56 | - public string $name; |
|
| 57 | - |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * This property is used for referencing a short description of the library |
|
| 61 | - * |
|
| 62 | - * @var string |
|
| 63 | - */ |
|
| 64 | - public string $description; |
|
| 65 | - |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * This will hold an array of shortcodes with the key as the shortcode ([shortcode]) and the value as a |
|
| 69 | - * label/description for the shortcode. |
|
| 70 | - * |
|
| 71 | - * @var array |
|
| 72 | - */ |
|
| 73 | - protected array $_shortcodes = []; |
|
| 74 | - |
|
| 75 | - |
|
| 76 | - /** |
|
| 77 | - * This will hold the incoming data item sent to the parser method |
|
| 78 | - * |
|
| 79 | - * @var array|object |
|
| 80 | - */ |
|
| 81 | - protected $_data; |
|
| 82 | - |
|
| 83 | - |
|
| 84 | - /** |
|
| 85 | - * some shortcodes may require extra data to parse. This property is provided for that. |
|
| 86 | - * |
|
| 87 | - * @var array|EE_Messages_Addressee |
|
| 88 | - */ |
|
| 89 | - protected $_extra_data; |
|
| 90 | - |
|
| 91 | - |
|
| 92 | - /** |
|
| 93 | - * EE_messenger used to generate the template being parsed. |
|
| 94 | - * |
|
| 95 | - * @since 4.5.0 |
|
| 96 | - * @var EE_messenger |
|
| 97 | - */ |
|
| 98 | - protected EE_messenger $_messenger; |
|
| 99 | - |
|
| 100 | - |
|
| 101 | - /** |
|
| 102 | - * message type used to generate the template being parsed. |
|
| 103 | - * |
|
| 104 | - * @since 4.5.0 |
|
| 105 | - * @var EE_message_type|null |
|
| 106 | - */ |
|
| 107 | - protected ?EE_message_type $_message_type = null; |
|
| 108 | - |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * context used for the template being parsed |
|
| 112 | - * |
|
| 113 | - * @since 4.5.0 |
|
| 114 | - * @var string |
|
| 115 | - */ |
|
| 116 | - protected string $_context; |
|
| 117 | - |
|
| 118 | - |
|
| 119 | - /** |
|
| 120 | - * Specific Message Template Group ID |
|
| 121 | - * |
|
| 122 | - * @since 4.5.0 |
|
| 123 | - * @var int |
|
| 124 | - */ |
|
| 125 | - protected int $_GRP_ID; |
|
| 126 | - |
|
| 127 | - |
|
| 128 | - /** |
|
| 129 | - * @since 4.9.0 |
|
| 130 | - * @type EE_Message|null |
|
| 131 | - */ |
|
| 132 | - protected ?EE_Message $_message = null; |
|
| 133 | - |
|
| 134 | - |
|
| 135 | - /** |
|
| 136 | - * This will hold an instance of the EEH_Parse_Shortcodes helper that will be used when handling list type |
|
| 137 | - * shortcodes |
|
| 138 | - * |
|
| 139 | - * @var EEH_Parse_Shortcodes |
|
| 140 | - */ |
|
| 141 | - protected EEH_Parse_Shortcodes $_shortcode_helper; |
|
| 142 | - |
|
| 143 | - |
|
| 144 | - public function __construct() |
|
| 145 | - { |
|
| 146 | - $this->_set_defaults(); |
|
| 147 | - $this->_set_shortcode_helper(); |
|
| 148 | - $this->_init_props(); |
|
| 149 | - } |
|
| 150 | - |
|
| 151 | - |
|
| 152 | - /** |
|
| 153 | - * This sets the defaults for the properties. Child classes will override these properties in their _init_props |
|
| 154 | - * method |
|
| 155 | - */ |
|
| 156 | - private function _set_defaults() |
|
| 157 | - { |
|
| 158 | - $this->name = $this->description = ''; |
|
| 159 | - $this->_shortcodes = []; |
|
| 160 | - } |
|
| 161 | - |
|
| 162 | - |
|
| 163 | - /** |
|
| 164 | - * loads an instance of the EE_Shortcode_Parser helper when requested |
|
| 165 | - */ |
|
| 166 | - protected function _set_shortcode_helper() |
|
| 167 | - { |
|
| 168 | - // get shortcode_replace instance - set when _get_messages is called in child... |
|
| 169 | - $this->_shortcode_helper = new EEH_Parse_Shortcodes(); |
|
| 170 | - } |
|
| 171 | - |
|
| 172 | - |
|
| 173 | - public function get_shortcode_helper(): EEH_Parse_Shortcodes |
|
| 174 | - { |
|
| 175 | - return $this->_shortcode_helper; |
|
| 176 | - } |
|
| 177 | - |
|
| 178 | - |
|
| 179 | - /** |
|
| 180 | - * This is the public method for kicking of the parser included with each child. It can be overridden by child |
|
| 181 | - * classes if necessary (see EE_Questions_Answers for example) |
|
| 182 | - * |
|
| 183 | - * @param string $shortcode incoming shortcode to be parsed |
|
| 184 | - * @param object|array $data incoming data to be used for parsing |
|
| 185 | - * @param object|array $extra_data extra incoming data (usually EE_Messages_Addressee) |
|
| 186 | - * @return string parsed shortcode. |
|
| 187 | - */ |
|
| 188 | - public function parser(string $shortcode, $data, $extra_data = []): string |
|
| 189 | - { |
|
| 190 | - // filter setup shortcodes |
|
| 191 | - $this->_shortcodes = $this->get_shortcodes(); |
|
| 192 | - |
|
| 193 | - // we need to set up any dynamic shortcodes so that they work with the array_key_exists |
|
| 194 | - preg_match_all(EE_Shortcodes::REGEX_SHORTCODE_NAME_ONLY, $shortcode, $matches); |
|
| 195 | - $shortcode_to_verify = ! empty($matches[0]) ? $matches[0][0] . ']' : $shortcode; |
|
| 196 | - |
|
| 197 | - // first we want to make sure this is a valid shortcode |
|
| 198 | - if (! array_key_exists($shortcode_to_verify, $this->_shortcodes)) { |
|
| 199 | - // get out, this parser doesn't handle the incoming shortcode. |
|
| 200 | - return ''; |
|
| 201 | - } |
|
| 202 | - $this->_data = $data; |
|
| 203 | - $this->_extra_data = $extra_data; |
|
| 204 | - $this->_set_messages_properties(); |
|
| 205 | - $parsed = (string) apply_filters( |
|
| 206 | - 'FHEE__' . get_class($this) . '__parser_after', |
|
| 207 | - $this->_parser($shortcode), |
|
| 208 | - $shortcode, |
|
| 209 | - $data, |
|
| 210 | - $extra_data, |
|
| 211 | - $this |
|
| 212 | - ); |
|
| 213 | - |
|
| 214 | - // note the below filter applies to ALL shortcode parsers... be careful! |
|
| 215 | - return (string) apply_filters( |
|
| 216 | - 'FHEE__EE_Shortcodes__parser_after', |
|
| 217 | - $parsed, |
|
| 218 | - $shortcode, |
|
| 219 | - $data, |
|
| 220 | - $extra_data, |
|
| 221 | - $this |
|
| 222 | - ); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - |
|
| 226 | - /** |
|
| 227 | - * This method just returns the shortcodes in the $_shortcodes array property. |
|
| 228 | - * |
|
| 229 | - * @return array array of shortcodes => description pairs |
|
| 230 | - */ |
|
| 231 | - public function get_shortcodes(): array |
|
| 232 | - { |
|
| 233 | - $this->_shortcodes = (array) apply_filters( |
|
| 234 | - 'FHEE__' . get_class($this) . '__shortcodes', |
|
| 235 | - $this->_shortcodes, |
|
| 236 | - $this |
|
| 237 | - ); |
|
| 238 | - |
|
| 239 | - // note the below filter applies to ALL shortcode parsers... be careful! |
|
| 240 | - return (array) apply_filters('FHEE__EE_Shortcodes__shortcodes', $this->_shortcodes, $this); |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - |
|
| 244 | - /** |
|
| 245 | - * Child classes use this method to set the $name, $description, and $_shortcodes properties. |
|
| 246 | - * |
|
| 247 | - * @abstract |
|
| 248 | - * @return void |
|
| 249 | - */ |
|
| 250 | - abstract protected function _init_props(); |
|
| 251 | - |
|
| 252 | - |
|
| 253 | - /** |
|
| 254 | - * This method will give parsing instructions for each shortcode defined in the _shortcodes array. Child methods |
|
| 255 | - * will have to take care of handling. |
|
| 256 | - * |
|
| 257 | - * @abstract |
|
| 258 | - * @param string $shortcode the shortcode to be parsed. |
|
| 259 | - * @return string parsed shortcode |
|
| 260 | - */ |
|
| 261 | - abstract protected function _parser($shortcode); |
|
| 262 | - |
|
| 263 | - |
|
| 264 | - /** |
|
| 265 | - * This just validates incoming data for list type shortcode parsers (and they call this method) to make sure it |
|
| 266 | - * meets their requirements |
|
| 267 | - * |
|
| 268 | - * @return void If validation fails we'll throw an exception. |
|
| 269 | - * @throws EE_Error |
|
| 270 | - */ |
|
| 271 | - protected function _validate_list_requirements() |
|
| 272 | - { |
|
| 273 | - // first test to make sure we've got an array! |
|
| 274 | - if (! is_array($this->_data)) { |
|
| 275 | - throw new EE_Error( |
|
| 276 | - sprintf( |
|
| 277 | - esc_html__( |
|
| 278 | - 'Expecting an array for the data sent to %s. Instead it was %s', |
|
| 279 | - 'event_espresso' |
|
| 280 | - ), |
|
| 281 | - get_class($this), |
|
| 282 | - gettype($this->_data) |
|
| 283 | - ) |
|
| 284 | - ); |
|
| 285 | - } |
|
| 286 | - |
|
| 287 | - // next test to make sure we've got the required template in the index! |
|
| 288 | - if (! isset($this->_data['template'])) { |
|
| 289 | - throw new EE_Error( |
|
| 290 | - esc_html__( |
|
| 291 | - 'The incoming data does not have the required template index in its array', |
|
| 292 | - 'event_espresso' |
|
| 293 | - ) |
|
| 294 | - ); |
|
| 295 | - } |
|
| 296 | - |
|
| 297 | - // next test to make sure we've got a data index in the incoming data array |
|
| 298 | - if (! isset($this->_data['data'])) { |
|
| 299 | - throw new EE_Error( |
|
| 300 | - esc_html__( |
|
| 301 | - 'The incoming data does not have the required data index in its array', |
|
| 302 | - 'event_espresso' |
|
| 303 | - ) |
|
| 304 | - ); |
|
| 305 | - } |
|
| 306 | - |
|
| 307 | - // all is well let's make sure _extra_data always has the values needed. |
|
| 308 | - // let's make sure that extra_data includes all templates (for later parsing if necessary) |
|
| 309 | - if (empty($this->_extra_data) || (empty($this->_extra_data['data']) && empty($this->_extra_data['template']))) { |
|
| 310 | - $this->_extra_data['data'] = $this->_data['data']; |
|
| 311 | - $this->_extra_data['template'] = $this->_data['template']; |
|
| 312 | - } |
|
| 313 | - } |
|
| 314 | - |
|
| 315 | - |
|
| 316 | - /** |
|
| 317 | - * This returns any attributes that may be existing on an EE_Shortcode |
|
| 318 | - * |
|
| 319 | - * @param string $shortcode incoming shortcode |
|
| 320 | - * @return array An array with the attributes |
|
| 321 | - * @since 4.5.0 |
|
| 322 | - */ |
|
| 323 | - protected function _get_shortcode_attrs(string $shortcode): array |
|
| 324 | - { |
|
| 325 | - // make sure the required wp helper function is present |
|
| 326 | - // require the shortcode file if necessary |
|
| 327 | - if (! function_exists('shortcode_parse_atts')) { |
|
| 328 | - require_once(ABSPATH . WPINC . '/shortcodes.php'); |
|
| 329 | - } |
|
| 330 | - |
|
| 331 | - // let's get any attributes that may be present and set the defaults. |
|
| 332 | - $shortcode_to_parse = str_replace(['[', ']'], '', $shortcode); |
|
| 333 | - return shortcode_parse_atts($shortcode_to_parse); |
|
| 334 | - } |
|
| 335 | - |
|
| 336 | - |
|
| 337 | - /** |
|
| 338 | - * Conditional blocks are shortcode patterns with an opening conditional tag `[IF_*]` and a corresponding |
|
| 339 | - * closing tag (eg `[/IF_*]`). The content within the tags will be displayed/hidden depending on whatever |
|
| 340 | - * conditions existed in the opening tag. This method handles parsing the actual template to show/hide this |
|
| 341 | - * conditional content. |
|
| 342 | - * |
|
| 343 | - * @param string $shortcode This should be the original shortcode as used in the template and passed to the parser. |
|
| 344 | - * @param bool $show true means the opening and closing tags are removed and the content is left showing, |
|
| 345 | - * false means the opening and closing tags and the contained content are removed. |
|
| 346 | - * @return string The template for the shortcode is returned. |
|
| 347 | - * @since 4.9.32 |
|
| 348 | - * |
|
| 349 | - */ |
|
| 350 | - protected function _mutate_conditional_block_in_template(string $shortcode, bool $show = true): string |
|
| 351 | - { |
|
| 352 | - // first let's get all the matches in the template for this particular shortcode. |
|
| 353 | - preg_match_all('~' . $this->_get_conditional_block_regex($shortcode) . '~', $this->_data['template'], $matches); |
|
| 354 | - |
|
| 355 | - if ($matches && is_array($matches[0]) && ! empty($matches[0])) { |
|
| 356 | - // we need to hide all instances of the matches |
|
| 357 | - foreach ($matches[0] as $index => $content_to_show_or_hide) { |
|
| 358 | - $content_to_show_or_hide = preg_quote($content_to_show_or_hide); |
|
| 359 | - $replacement = $show ? $matches[4][ $index ] : ''; |
|
| 360 | - $this->_data['template'] = preg_replace( |
|
| 361 | - '~' . $content_to_show_or_hide . '~', |
|
| 362 | - $replacement, |
|
| 363 | - $this->_data['template'] |
|
| 364 | - ); |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - // return $template |
|
| 368 | - return $this->_data['template']; |
|
| 369 | - } |
|
| 370 | - |
|
| 371 | - |
|
| 372 | - /** |
|
| 373 | - * This returns the regex pattern to use for conditional shortcodes parsing. |
|
| 374 | - * |
|
| 375 | - * Note: regex comes in part from the WP `get_shortcode_regex` expression in \wp-includes\shortcodes.php |
|
| 376 | - * |
|
| 377 | - * @param string $shortcode |
|
| 378 | - * @return string |
|
| 379 | - * @since 4.9.32 |
|
| 380 | - */ |
|
| 381 | - private function _get_conditional_block_regex(string $shortcode): string |
|
| 382 | - { |
|
| 383 | - // get just the shortcode tag for the match |
|
| 384 | - preg_match('@\[([^<>&/\[\]\x00-\x20=]++)@', $shortcode, $shortcode_tag_matches); |
|
| 385 | - if (empty($shortcode_tag_matches[1])) { |
|
| 386 | - return $this->_data['template']; |
|
| 387 | - } |
|
| 388 | - |
|
| 389 | - $shortcode_tag = $shortcode_tag_matches[1]; |
|
| 390 | - // get attributes_part_of_tag |
|
| 391 | - $attributes_part = preg_quote(str_replace([$shortcode_tag, '[', ']'], '', $shortcode)); |
|
| 392 | - // escape |
|
| 393 | - $shortcode_tag = preg_quote($shortcode_tag); |
|
| 394 | - |
|
| 395 | - return |
|
| 396 | - '\[' // Opening Bracket |
|
| 397 | - . "($shortcode_tag)$attributes_part" // 1: Shortcode Name |
|
| 398 | - . '(?![\w-])' // Not followed by word character or hyphen |
|
| 399 | - . '(' // 2: Unroll the loop: Inside the opening shortcode tag |
|
| 400 | - . '[^\]\/]*' // Not a closing bracket or forward slash |
|
| 401 | - . '(?:' |
|
| 402 | - . '\/(?!\])' // A forward slash not followed by a closing bracket |
|
| 403 | - . '[^\]\/]*' // Not a closing bracket or forward slash. |
|
| 404 | - . ')*?' |
|
| 405 | - . ')' |
|
| 406 | - . '(?:' |
|
| 407 | - . '(\/)' // 3. Self closing tag ... |
|
| 408 | - . '\]' // ... and closing bracket |
|
| 409 | - . '|' |
|
| 410 | - . '\]' // Closing bracket |
|
| 411 | - . '(?:' |
|
| 412 | - . '(' // 4: Unroll the loop: Optionally, anything between the opening and closing brackets |
|
| 413 | - . '[^\[]*+' // Not an opening bracket |
|
| 414 | - . '(?:' |
|
| 415 | - . '\[(?!\/\1\])' // An opening bracket not followed by the closing shortcode tag. |
|
| 416 | - . '[^\[]*+' // Not an opening bracket |
|
| 417 | - . ')*+' |
|
| 418 | - . ')' |
|
| 419 | - . '\[\/\1\]' // Closing shortcode tag |
|
| 420 | - . ')?' |
|
| 421 | - . ')'; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - |
|
| 425 | - /** |
|
| 426 | - * This sets the properties related to the messages system |
|
| 427 | - * |
|
| 428 | - * @return void |
|
| 429 | - * @since 4.5.0 |
|
| 430 | - */ |
|
| 431 | - protected function _set_messages_properties() |
|
| 432 | - { |
|
| 433 | - // should be in _extra_data |
|
| 434 | - if (isset($this->_extra_data['messenger'])) { |
|
| 435 | - $this->_messenger = $this->_extra_data['messenger']; |
|
| 436 | - $this->_message_type = $this->_extra_data['message_type']; |
|
| 437 | - $this->_context = $this->_extra_data['message'] instanceof EE_Message |
|
| 438 | - ? $this->_extra_data['message']->context() |
|
| 439 | - : ''; |
|
| 440 | - $this->_GRP_ID = $this->_extra_data['message'] instanceof EE_Message |
|
| 441 | - ? $this->_extra_data['message']->GRP_ID() |
|
| 442 | - : 0; |
|
| 443 | - $this->_message = $this->_extra_data['message'] instanceof EE_Message |
|
| 444 | - ? $this->_extra_data['message'] |
|
| 445 | - : null; |
|
| 446 | - } |
|
| 447 | - } |
|
| 448 | - |
|
| 449 | - |
|
| 450 | - /** |
|
| 451 | - * This returns whatever the set message type object is that was set on this shortcode parser. |
|
| 452 | - * |
|
| 453 | - * @return EE_message_type|null |
|
| 454 | - * @since 4.5.0 |
|
| 455 | - */ |
|
| 456 | - public function get_set_message_type(): ?EE_message_type |
|
| 457 | - { |
|
| 458 | - return $this->_message_type; |
|
| 459 | - } |
|
| 460 | - |
|
| 461 | - |
|
| 462 | - /** |
|
| 463 | - * This returns whatever the set messenger object is that was set on this shortcode parser |
|
| 464 | - * |
|
| 465 | - * @return EE_messenger |
|
| 466 | - * @since 4.5.0 |
|
| 467 | - */ |
|
| 468 | - public function get_set_messenger(): EE_messenger |
|
| 469 | - { |
|
| 470 | - return $this->_messenger; |
|
| 471 | - } |
|
| 472 | - |
|
| 473 | - |
|
| 474 | - /** |
|
| 475 | - * This returns whatever the set context string is on this shortcode parser. |
|
| 476 | - * |
|
| 477 | - * @return string |
|
| 478 | - * @since 4.5.0 |
|
| 479 | - */ |
|
| 480 | - public function get_set_context(): string |
|
| 481 | - { |
|
| 482 | - return $this->_context; |
|
| 483 | - } |
|
| 484 | - |
|
| 485 | - |
|
| 486 | - /** |
|
| 487 | - * This returns whatever the set EE_Message object is on this shortcode. |
|
| 488 | - * |
|
| 489 | - * @return EE_Message|null |
|
| 490 | - * @since 4.9.0 |
|
| 491 | - */ |
|
| 492 | - public function get_set_message(): ?EE_Message |
|
| 493 | - { |
|
| 494 | - return $this->_message; |
|
| 495 | - } |
|
| 18 | + /** |
|
| 19 | + * matches a full shortcode including shortcodes with attributes or dynamic shortcodes |
|
| 20 | + * examples: |
|
| 21 | + * [RECIPIENT_LNAME] |
|
| 22 | + * [RECEIPT_URL download=true] |
|
| 23 | + * [PAYMENT_LINK_IF_NEEDED_* custom_text='pay me now man!'] |
|
| 24 | + */ |
|
| 25 | + public const REGEX_SHORTCODE_FULL = '/(\[.+?])/'; |
|
| 26 | + |
|
| 27 | + /** |
|
| 28 | + * matches the opening [ plus the shortcode name, but nothing else |
|
| 29 | + * examples: |
|
| 30 | + * [RECIPIENT_LNAME] matches as [RECIPIENT_LNAME |
|
| 31 | + * [RECEIPT_URL download=true] matches as [RECEIPT_URL |
|
| 32 | + * [PAYMENT_LINK_IF_NEEDED_* custom_text='pay me now man!'] matches as [PAYMENT_LINK_IF_NEEDED_* |
|
| 33 | + */ |
|
| 34 | + public const REGEX_SHORTCODE_NAME_ONLY = '/\[[A-Z\d_*]+/'; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * matches the opening [ plus the dynamic shortcode name including _*, but nothing else |
|
| 38 | + * examples: |
|
| 39 | + * [RECIPIENT_LNAME] does not match |
|
| 40 | + * [RECEIPT_URL download=true] does not match |
|
| 41 | + * [PAYMENT_LINK_IF_NEEDED_* custom_text='pay me now man!'] matches as [PAYMENT_LINK_IF_NEEDED_* |
|
| 42 | + */ |
|
| 43 | + public const REGEX_SHORTCODE_DYNAMIC = '/(\[[A-Z_]+_\*)/'; |
|
| 44 | + |
|
| 45 | + |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * holds label for library |
|
| 49 | + * This is used for referencing the library label |
|
| 50 | + * |
|
| 51 | + * @var string |
|
| 52 | + */ |
|
| 53 | + public string $label; |
|
| 54 | + |
|
| 55 | + |
|
| 56 | + public string $name; |
|
| 57 | + |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * This property is used for referencing a short description of the library |
|
| 61 | + * |
|
| 62 | + * @var string |
|
| 63 | + */ |
|
| 64 | + public string $description; |
|
| 65 | + |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * This will hold an array of shortcodes with the key as the shortcode ([shortcode]) and the value as a |
|
| 69 | + * label/description for the shortcode. |
|
| 70 | + * |
|
| 71 | + * @var array |
|
| 72 | + */ |
|
| 73 | + protected array $_shortcodes = []; |
|
| 74 | + |
|
| 75 | + |
|
| 76 | + /** |
|
| 77 | + * This will hold the incoming data item sent to the parser method |
|
| 78 | + * |
|
| 79 | + * @var array|object |
|
| 80 | + */ |
|
| 81 | + protected $_data; |
|
| 82 | + |
|
| 83 | + |
|
| 84 | + /** |
|
| 85 | + * some shortcodes may require extra data to parse. This property is provided for that. |
|
| 86 | + * |
|
| 87 | + * @var array|EE_Messages_Addressee |
|
| 88 | + */ |
|
| 89 | + protected $_extra_data; |
|
| 90 | + |
|
| 91 | + |
|
| 92 | + /** |
|
| 93 | + * EE_messenger used to generate the template being parsed. |
|
| 94 | + * |
|
| 95 | + * @since 4.5.0 |
|
| 96 | + * @var EE_messenger |
|
| 97 | + */ |
|
| 98 | + protected EE_messenger $_messenger; |
|
| 99 | + |
|
| 100 | + |
|
| 101 | + /** |
|
| 102 | + * message type used to generate the template being parsed. |
|
| 103 | + * |
|
| 104 | + * @since 4.5.0 |
|
| 105 | + * @var EE_message_type|null |
|
| 106 | + */ |
|
| 107 | + protected ?EE_message_type $_message_type = null; |
|
| 108 | + |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * context used for the template being parsed |
|
| 112 | + * |
|
| 113 | + * @since 4.5.0 |
|
| 114 | + * @var string |
|
| 115 | + */ |
|
| 116 | + protected string $_context; |
|
| 117 | + |
|
| 118 | + |
|
| 119 | + /** |
|
| 120 | + * Specific Message Template Group ID |
|
| 121 | + * |
|
| 122 | + * @since 4.5.0 |
|
| 123 | + * @var int |
|
| 124 | + */ |
|
| 125 | + protected int $_GRP_ID; |
|
| 126 | + |
|
| 127 | + |
|
| 128 | + /** |
|
| 129 | + * @since 4.9.0 |
|
| 130 | + * @type EE_Message|null |
|
| 131 | + */ |
|
| 132 | + protected ?EE_Message $_message = null; |
|
| 133 | + |
|
| 134 | + |
|
| 135 | + /** |
|
| 136 | + * This will hold an instance of the EEH_Parse_Shortcodes helper that will be used when handling list type |
|
| 137 | + * shortcodes |
|
| 138 | + * |
|
| 139 | + * @var EEH_Parse_Shortcodes |
|
| 140 | + */ |
|
| 141 | + protected EEH_Parse_Shortcodes $_shortcode_helper; |
|
| 142 | + |
|
| 143 | + |
|
| 144 | + public function __construct() |
|
| 145 | + { |
|
| 146 | + $this->_set_defaults(); |
|
| 147 | + $this->_set_shortcode_helper(); |
|
| 148 | + $this->_init_props(); |
|
| 149 | + } |
|
| 150 | + |
|
| 151 | + |
|
| 152 | + /** |
|
| 153 | + * This sets the defaults for the properties. Child classes will override these properties in their _init_props |
|
| 154 | + * method |
|
| 155 | + */ |
|
| 156 | + private function _set_defaults() |
|
| 157 | + { |
|
| 158 | + $this->name = $this->description = ''; |
|
| 159 | + $this->_shortcodes = []; |
|
| 160 | + } |
|
| 161 | + |
|
| 162 | + |
|
| 163 | + /** |
|
| 164 | + * loads an instance of the EE_Shortcode_Parser helper when requested |
|
| 165 | + */ |
|
| 166 | + protected function _set_shortcode_helper() |
|
| 167 | + { |
|
| 168 | + // get shortcode_replace instance - set when _get_messages is called in child... |
|
| 169 | + $this->_shortcode_helper = new EEH_Parse_Shortcodes(); |
|
| 170 | + } |
|
| 171 | + |
|
| 172 | + |
|
| 173 | + public function get_shortcode_helper(): EEH_Parse_Shortcodes |
|
| 174 | + { |
|
| 175 | + return $this->_shortcode_helper; |
|
| 176 | + } |
|
| 177 | + |
|
| 178 | + |
|
| 179 | + /** |
|
| 180 | + * This is the public method for kicking of the parser included with each child. It can be overridden by child |
|
| 181 | + * classes if necessary (see EE_Questions_Answers for example) |
|
| 182 | + * |
|
| 183 | + * @param string $shortcode incoming shortcode to be parsed |
|
| 184 | + * @param object|array $data incoming data to be used for parsing |
|
| 185 | + * @param object|array $extra_data extra incoming data (usually EE_Messages_Addressee) |
|
| 186 | + * @return string parsed shortcode. |
|
| 187 | + */ |
|
| 188 | + public function parser(string $shortcode, $data, $extra_data = []): string |
|
| 189 | + { |
|
| 190 | + // filter setup shortcodes |
|
| 191 | + $this->_shortcodes = $this->get_shortcodes(); |
|
| 192 | + |
|
| 193 | + // we need to set up any dynamic shortcodes so that they work with the array_key_exists |
|
| 194 | + preg_match_all(EE_Shortcodes::REGEX_SHORTCODE_NAME_ONLY, $shortcode, $matches); |
|
| 195 | + $shortcode_to_verify = ! empty($matches[0]) ? $matches[0][0] . ']' : $shortcode; |
|
| 196 | + |
|
| 197 | + // first we want to make sure this is a valid shortcode |
|
| 198 | + if (! array_key_exists($shortcode_to_verify, $this->_shortcodes)) { |
|
| 199 | + // get out, this parser doesn't handle the incoming shortcode. |
|
| 200 | + return ''; |
|
| 201 | + } |
|
| 202 | + $this->_data = $data; |
|
| 203 | + $this->_extra_data = $extra_data; |
|
| 204 | + $this->_set_messages_properties(); |
|
| 205 | + $parsed = (string) apply_filters( |
|
| 206 | + 'FHEE__' . get_class($this) . '__parser_after', |
|
| 207 | + $this->_parser($shortcode), |
|
| 208 | + $shortcode, |
|
| 209 | + $data, |
|
| 210 | + $extra_data, |
|
| 211 | + $this |
|
| 212 | + ); |
|
| 213 | + |
|
| 214 | + // note the below filter applies to ALL shortcode parsers... be careful! |
|
| 215 | + return (string) apply_filters( |
|
| 216 | + 'FHEE__EE_Shortcodes__parser_after', |
|
| 217 | + $parsed, |
|
| 218 | + $shortcode, |
|
| 219 | + $data, |
|
| 220 | + $extra_data, |
|
| 221 | + $this |
|
| 222 | + ); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + |
|
| 226 | + /** |
|
| 227 | + * This method just returns the shortcodes in the $_shortcodes array property. |
|
| 228 | + * |
|
| 229 | + * @return array array of shortcodes => description pairs |
|
| 230 | + */ |
|
| 231 | + public function get_shortcodes(): array |
|
| 232 | + { |
|
| 233 | + $this->_shortcodes = (array) apply_filters( |
|
| 234 | + 'FHEE__' . get_class($this) . '__shortcodes', |
|
| 235 | + $this->_shortcodes, |
|
| 236 | + $this |
|
| 237 | + ); |
|
| 238 | + |
|
| 239 | + // note the below filter applies to ALL shortcode parsers... be careful! |
|
| 240 | + return (array) apply_filters('FHEE__EE_Shortcodes__shortcodes', $this->_shortcodes, $this); |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + |
|
| 244 | + /** |
|
| 245 | + * Child classes use this method to set the $name, $description, and $_shortcodes properties. |
|
| 246 | + * |
|
| 247 | + * @abstract |
|
| 248 | + * @return void |
|
| 249 | + */ |
|
| 250 | + abstract protected function _init_props(); |
|
| 251 | + |
|
| 252 | + |
|
| 253 | + /** |
|
| 254 | + * This method will give parsing instructions for each shortcode defined in the _shortcodes array. Child methods |
|
| 255 | + * will have to take care of handling. |
|
| 256 | + * |
|
| 257 | + * @abstract |
|
| 258 | + * @param string $shortcode the shortcode to be parsed. |
|
| 259 | + * @return string parsed shortcode |
|
| 260 | + */ |
|
| 261 | + abstract protected function _parser($shortcode); |
|
| 262 | + |
|
| 263 | + |
|
| 264 | + /** |
|
| 265 | + * This just validates incoming data for list type shortcode parsers (and they call this method) to make sure it |
|
| 266 | + * meets their requirements |
|
| 267 | + * |
|
| 268 | + * @return void If validation fails we'll throw an exception. |
|
| 269 | + * @throws EE_Error |
|
| 270 | + */ |
|
| 271 | + protected function _validate_list_requirements() |
|
| 272 | + { |
|
| 273 | + // first test to make sure we've got an array! |
|
| 274 | + if (! is_array($this->_data)) { |
|
| 275 | + throw new EE_Error( |
|
| 276 | + sprintf( |
|
| 277 | + esc_html__( |
|
| 278 | + 'Expecting an array for the data sent to %s. Instead it was %s', |
|
| 279 | + 'event_espresso' |
|
| 280 | + ), |
|
| 281 | + get_class($this), |
|
| 282 | + gettype($this->_data) |
|
| 283 | + ) |
|
| 284 | + ); |
|
| 285 | + } |
|
| 286 | + |
|
| 287 | + // next test to make sure we've got the required template in the index! |
|
| 288 | + if (! isset($this->_data['template'])) { |
|
| 289 | + throw new EE_Error( |
|
| 290 | + esc_html__( |
|
| 291 | + 'The incoming data does not have the required template index in its array', |
|
| 292 | + 'event_espresso' |
|
| 293 | + ) |
|
| 294 | + ); |
|
| 295 | + } |
|
| 296 | + |
|
| 297 | + // next test to make sure we've got a data index in the incoming data array |
|
| 298 | + if (! isset($this->_data['data'])) { |
|
| 299 | + throw new EE_Error( |
|
| 300 | + esc_html__( |
|
| 301 | + 'The incoming data does not have the required data index in its array', |
|
| 302 | + 'event_espresso' |
|
| 303 | + ) |
|
| 304 | + ); |
|
| 305 | + } |
|
| 306 | + |
|
| 307 | + // all is well let's make sure _extra_data always has the values needed. |
|
| 308 | + // let's make sure that extra_data includes all templates (for later parsing if necessary) |
|
| 309 | + if (empty($this->_extra_data) || (empty($this->_extra_data['data']) && empty($this->_extra_data['template']))) { |
|
| 310 | + $this->_extra_data['data'] = $this->_data['data']; |
|
| 311 | + $this->_extra_data['template'] = $this->_data['template']; |
|
| 312 | + } |
|
| 313 | + } |
|
| 314 | + |
|
| 315 | + |
|
| 316 | + /** |
|
| 317 | + * This returns any attributes that may be existing on an EE_Shortcode |
|
| 318 | + * |
|
| 319 | + * @param string $shortcode incoming shortcode |
|
| 320 | + * @return array An array with the attributes |
|
| 321 | + * @since 4.5.0 |
|
| 322 | + */ |
|
| 323 | + protected function _get_shortcode_attrs(string $shortcode): array |
|
| 324 | + { |
|
| 325 | + // make sure the required wp helper function is present |
|
| 326 | + // require the shortcode file if necessary |
|
| 327 | + if (! function_exists('shortcode_parse_atts')) { |
|
| 328 | + require_once(ABSPATH . WPINC . '/shortcodes.php'); |
|
| 329 | + } |
|
| 330 | + |
|
| 331 | + // let's get any attributes that may be present and set the defaults. |
|
| 332 | + $shortcode_to_parse = str_replace(['[', ']'], '', $shortcode); |
|
| 333 | + return shortcode_parse_atts($shortcode_to_parse); |
|
| 334 | + } |
|
| 335 | + |
|
| 336 | + |
|
| 337 | + /** |
|
| 338 | + * Conditional blocks are shortcode patterns with an opening conditional tag `[IF_*]` and a corresponding |
|
| 339 | + * closing tag (eg `[/IF_*]`). The content within the tags will be displayed/hidden depending on whatever |
|
| 340 | + * conditions existed in the opening tag. This method handles parsing the actual template to show/hide this |
|
| 341 | + * conditional content. |
|
| 342 | + * |
|
| 343 | + * @param string $shortcode This should be the original shortcode as used in the template and passed to the parser. |
|
| 344 | + * @param bool $show true means the opening and closing tags are removed and the content is left showing, |
|
| 345 | + * false means the opening and closing tags and the contained content are removed. |
|
| 346 | + * @return string The template for the shortcode is returned. |
|
| 347 | + * @since 4.9.32 |
|
| 348 | + * |
|
| 349 | + */ |
|
| 350 | + protected function _mutate_conditional_block_in_template(string $shortcode, bool $show = true): string |
|
| 351 | + { |
|
| 352 | + // first let's get all the matches in the template for this particular shortcode. |
|
| 353 | + preg_match_all('~' . $this->_get_conditional_block_regex($shortcode) . '~', $this->_data['template'], $matches); |
|
| 354 | + |
|
| 355 | + if ($matches && is_array($matches[0]) && ! empty($matches[0])) { |
|
| 356 | + // we need to hide all instances of the matches |
|
| 357 | + foreach ($matches[0] as $index => $content_to_show_or_hide) { |
|
| 358 | + $content_to_show_or_hide = preg_quote($content_to_show_or_hide); |
|
| 359 | + $replacement = $show ? $matches[4][ $index ] : ''; |
|
| 360 | + $this->_data['template'] = preg_replace( |
|
| 361 | + '~' . $content_to_show_or_hide . '~', |
|
| 362 | + $replacement, |
|
| 363 | + $this->_data['template'] |
|
| 364 | + ); |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + // return $template |
|
| 368 | + return $this->_data['template']; |
|
| 369 | + } |
|
| 370 | + |
|
| 371 | + |
|
| 372 | + /** |
|
| 373 | + * This returns the regex pattern to use for conditional shortcodes parsing. |
|
| 374 | + * |
|
| 375 | + * Note: regex comes in part from the WP `get_shortcode_regex` expression in \wp-includes\shortcodes.php |
|
| 376 | + * |
|
| 377 | + * @param string $shortcode |
|
| 378 | + * @return string |
|
| 379 | + * @since 4.9.32 |
|
| 380 | + */ |
|
| 381 | + private function _get_conditional_block_regex(string $shortcode): string |
|
| 382 | + { |
|
| 383 | + // get just the shortcode tag for the match |
|
| 384 | + preg_match('@\[([^<>&/\[\]\x00-\x20=]++)@', $shortcode, $shortcode_tag_matches); |
|
| 385 | + if (empty($shortcode_tag_matches[1])) { |
|
| 386 | + return $this->_data['template']; |
|
| 387 | + } |
|
| 388 | + |
|
| 389 | + $shortcode_tag = $shortcode_tag_matches[1]; |
|
| 390 | + // get attributes_part_of_tag |
|
| 391 | + $attributes_part = preg_quote(str_replace([$shortcode_tag, '[', ']'], '', $shortcode)); |
|
| 392 | + // escape |
|
| 393 | + $shortcode_tag = preg_quote($shortcode_tag); |
|
| 394 | + |
|
| 395 | + return |
|
| 396 | + '\[' // Opening Bracket |
|
| 397 | + . "($shortcode_tag)$attributes_part" // 1: Shortcode Name |
|
| 398 | + . '(?![\w-])' // Not followed by word character or hyphen |
|
| 399 | + . '(' // 2: Unroll the loop: Inside the opening shortcode tag |
|
| 400 | + . '[^\]\/]*' // Not a closing bracket or forward slash |
|
| 401 | + . '(?:' |
|
| 402 | + . '\/(?!\])' // A forward slash not followed by a closing bracket |
|
| 403 | + . '[^\]\/]*' // Not a closing bracket or forward slash. |
|
| 404 | + . ')*?' |
|
| 405 | + . ')' |
|
| 406 | + . '(?:' |
|
| 407 | + . '(\/)' // 3. Self closing tag ... |
|
| 408 | + . '\]' // ... and closing bracket |
|
| 409 | + . '|' |
|
| 410 | + . '\]' // Closing bracket |
|
| 411 | + . '(?:' |
|
| 412 | + . '(' // 4: Unroll the loop: Optionally, anything between the opening and closing brackets |
|
| 413 | + . '[^\[]*+' // Not an opening bracket |
|
| 414 | + . '(?:' |
|
| 415 | + . '\[(?!\/\1\])' // An opening bracket not followed by the closing shortcode tag. |
|
| 416 | + . '[^\[]*+' // Not an opening bracket |
|
| 417 | + . ')*+' |
|
| 418 | + . ')' |
|
| 419 | + . '\[\/\1\]' // Closing shortcode tag |
|
| 420 | + . ')?' |
|
| 421 | + . ')'; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + |
|
| 425 | + /** |
|
| 426 | + * This sets the properties related to the messages system |
|
| 427 | + * |
|
| 428 | + * @return void |
|
| 429 | + * @since 4.5.0 |
|
| 430 | + */ |
|
| 431 | + protected function _set_messages_properties() |
|
| 432 | + { |
|
| 433 | + // should be in _extra_data |
|
| 434 | + if (isset($this->_extra_data['messenger'])) { |
|
| 435 | + $this->_messenger = $this->_extra_data['messenger']; |
|
| 436 | + $this->_message_type = $this->_extra_data['message_type']; |
|
| 437 | + $this->_context = $this->_extra_data['message'] instanceof EE_Message |
|
| 438 | + ? $this->_extra_data['message']->context() |
|
| 439 | + : ''; |
|
| 440 | + $this->_GRP_ID = $this->_extra_data['message'] instanceof EE_Message |
|
| 441 | + ? $this->_extra_data['message']->GRP_ID() |
|
| 442 | + : 0; |
|
| 443 | + $this->_message = $this->_extra_data['message'] instanceof EE_Message |
|
| 444 | + ? $this->_extra_data['message'] |
|
| 445 | + : null; |
|
| 446 | + } |
|
| 447 | + } |
|
| 448 | + |
|
| 449 | + |
|
| 450 | + /** |
|
| 451 | + * This returns whatever the set message type object is that was set on this shortcode parser. |
|
| 452 | + * |
|
| 453 | + * @return EE_message_type|null |
|
| 454 | + * @since 4.5.0 |
|
| 455 | + */ |
|
| 456 | + public function get_set_message_type(): ?EE_message_type |
|
| 457 | + { |
|
| 458 | + return $this->_message_type; |
|
| 459 | + } |
|
| 460 | + |
|
| 461 | + |
|
| 462 | + /** |
|
| 463 | + * This returns whatever the set messenger object is that was set on this shortcode parser |
|
| 464 | + * |
|
| 465 | + * @return EE_messenger |
|
| 466 | + * @since 4.5.0 |
|
| 467 | + */ |
|
| 468 | + public function get_set_messenger(): EE_messenger |
|
| 469 | + { |
|
| 470 | + return $this->_messenger; |
|
| 471 | + } |
|
| 472 | + |
|
| 473 | + |
|
| 474 | + /** |
|
| 475 | + * This returns whatever the set context string is on this shortcode parser. |
|
| 476 | + * |
|
| 477 | + * @return string |
|
| 478 | + * @since 4.5.0 |
|
| 479 | + */ |
|
| 480 | + public function get_set_context(): string |
|
| 481 | + { |
|
| 482 | + return $this->_context; |
|
| 483 | + } |
|
| 484 | + |
|
| 485 | + |
|
| 486 | + /** |
|
| 487 | + * This returns whatever the set EE_Message object is on this shortcode. |
|
| 488 | + * |
|
| 489 | + * @return EE_Message|null |
|
| 490 | + * @since 4.9.0 |
|
| 491 | + */ |
|
| 492 | + public function get_set_message(): ?EE_Message |
|
| 493 | + { |
|
| 494 | + return $this->_message; |
|
| 495 | + } |
|
| 496 | 496 | } |