Completed
Branch BUG-10381-asset-loading (189bf4)
by
unknown
13:54
created

EES_Shortcode::instance()   B

Complexity

Conditions 7
Paths 18

Size

Total Lines 13
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 11
nc 18
nop 1
dl 0
loc 13
rs 8.2222
c 0
b 0
f 0
1
<?php
2
use EventEspresso\core\services\shortcodes\LegacyShortcodesManager;
3
4
defined('EVENT_ESPRESSO_VERSION') || exit();
5
/**
6
 * EES_Shortcode
7
 *
8
 * @deprecated  4.9.26
9
 * @package     Event Espresso
10
 * @subpackage	/shortcodes/
11
 * @author      Brent Christensen
12
 */
13
abstract class EES_Shortcode extends EE_Base {
14
15
	/**
16
	 * @protected 	public
17
	 * @var 	array $_attributes
18
	 */
19
	protected $_attributes = array();
20
21
22
23
    /**
24
     * class constructor - should ONLY be instantiated by EE_Front_Controller
25
     */
26
    final public function __construct()
27
    {
28
        $shortcode = LegacyShortcodesManager::generateShortcodeTagFromClassName(get_class($this));
29
        // assign shortcode to the preferred callback, which overwrites the "fallback shortcode processor" assigned earlier
30
        add_shortcode($shortcode, array($this, 'process_shortcode'));
31
        // make sure system knows this is an EE page
32
        EE_Registry::instance()->REQ->set_espresso_page(true);
33
    }
34
35
36
37
	/**
38
	 * run - initial shortcode module setup called during "parse_request" hook by
39
	 * \EE_Front_Controller::_initialize_shortcodes() IF this shortcode is going to execute during this request !
40
	 * It may also get called by \EES_Shortcode::fallback_shortcode_processor() if the shortcode is being implemented
41
	 * by a theme or plugin in a non-standard way.
42
	 * Basically this method is primarily used for loading resources and assets like CSS or JS
43
	 * that will be required by the shortcode when it is actually processed.
44
	 * Please note that assets may not load if the fallback_shortcode_processor() is being used.
45
	 *
46
	 * @access    public
47
	 * @param WP $WP
48
	 * @return    void
49
	 */
50
	public abstract function run( WP $WP );
51
52
53
54
	/**
55
	 * 	process_shortcode
56
	 * 	this method is the callback function for the actual shortcode, and is what runs when WP encounters the shortcode within the_content
57
	 *
58
	 *  @access 	public
59
	 *  @param		array 	$attributes
60
	 *  @return 	mixed
61
	 */
62
	public abstract function process_shortcode( $attributes = array() );
63
64
65
66
	/**
67
	 *    instance - returns instance of child class object
68
	 *
69
	 * @access 	public
70
	 * @param 	string $shortcode_class
71
	 * @return 	\EES_Shortcode
72
	 */
73
	final public static function instance( $shortcode_class = null ) {
74
		$shortcode_class = ! empty( $shortcode_class ) ? $shortcode_class : get_called_class();
75
		if ( $shortcode_class === 'EES_Shortcode' || empty( $shortcode_class )) {
76
			return null;
77
		}
78
		$shortcode = str_replace( 'EES_', '', strtoupper( $shortcode_class ));
79
		$shortcode_obj = isset( EE_Registry::instance()->shortcodes->{$shortcode} )
80
            ? EE_Registry::instance()->shortcodes->{$shortcode}
81
            : null;
82
		return $shortcode_obj instanceof $shortcode_class || $shortcode_class === 'self'
83
            ? $shortcode_obj
84
            : new $shortcode_class();
85
	}
86
87
88
89
90
	/**
91
	 *    fallback_shortcode_processor - create instance and call process_shortcode
92
	 *    NOTE: shortcode may not function perfectly dues to missing assets, but it's better than not having things work at all
93
	 *
94
	 * @access 	public
95
	 * @param 	$attributes
96
	 * @return 	mixed
97
	 */
98
	final public static function fallback_shortcode_processor( $attributes ) {
99
		if ( EE_Maintenance_Mode::disable_frontend_for_maintenance() ) {
100
			return null;
101
		}
102
		// what shortcode was actually parsed ?
103
		$shortcode_class = get_called_class();
104
		// notify rest of system that fallback processor was triggered
105
		add_filter( 'FHEE__fallback_shortcode_processor__' . $shortcode_class, '__return_true' );
106
		// get instance of actual shortcode
107
		$shortcode_obj = self::instance( $shortcode_class );
108
		// verify class
109
		if ( $shortcode_obj instanceof EES_Shortcode ) {
110
			global $wp;
111
			$shortcode_obj->run( $wp );
112
			// set attributes and run the shortcode
113
			$shortcode_obj->_attributes = (array)$attributes;
114
			return $shortcode_obj->process_shortcode( $shortcode_obj->_attributes );
115
		} else {
116
			return null;
117
		}
118
	}
119
120
121
122
123
	/**
124
	 *    invalid_shortcode_processor -  used in cases where we know the shortcode is invalid, most likely due to a deactivated addon, and simply returns an empty string
125
	 *
126
	 * @access 	public
127
	 * @param 	$attributes
128
	 * @return 	string
129
	 */
130
	final public static function invalid_shortcode_processor( $attributes ) {
0 ignored issues
show
Unused Code introduced by
The parameter $attributes is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
131
		return '';
132
	}
133
134
135
136
137
138
    /**
139
     * Performs basic sanitization on shortcode attributes
140
     * Since incoming attributes from the shortcode usage in the WP editor will all be strings,
141
     * most attributes will by default be sanitized using the sanitize_text_field() function.
142
     * This can be overridden by supplying an array for the $custom_sanitization param,
143
     * where keys match keys in your attributes array,
144
     * and values represent the sanitization function you wish to be applied to that attribute.
145
     * So for example, if you had an integer attribute named "event_id"
146
     * that you wanted to be sanitized using absint(),
147
     * then you would pass the following for your $custom_sanitization array:
148
     *      array('event_id' => 'absint')
149
     * all other attributes would be sanitized using the defaults in the switch statement below
150
     *
151
     * @param array $attributes
152
     * @param array $custom_sanitization
153
     * @return array
154
     */
155
    public static function sanitize_attributes(array $attributes, $custom_sanitization = array())
156
    {
157 View Code Duplication
        foreach ($attributes as $key => $value) {
158
            // is a custom sanitization callback specified ?
159
            if ( isset($custom_sanitization[$key])) {
160
                $callback = $custom_sanitization[$key];
161
                if ($callback === 'skip_sanitization') {
162
                    $attributes[$key] = $value;
163
                    continue;
164
                } else if (function_exists($callback)){
165
                    $attributes[$key] = $callback($value);
166
                    continue;
167
                }
168
            }
169
            switch (true) {
170
                case $value === null :
171
                case is_int($value) :
172
                case is_float($value) :
173
                    // typical booleans
174
                case in_array($value, array(true, 'true', '1', 'on', 'yes', false, 'false', '0', 'off', 'no'), true) :
175
                    $attributes[$key] = $value;
176
                    break;
177
                case is_string($value) :
178
                    $attributes[$key] = sanitize_text_field($value);
179
                    break;
180
                case is_array($value) :
181
                    $attributes[$key] = \EES_Shortcode::sanitize_attributes($value);
182
                    break;
183
                default :
184
                    // only remaining data types are Object and Resource
185
                    // which are not allowed as shortcode attributes
186
                    $attributes[$key] = null;
187
                    break;
188
            }
189
        }
190
        return $attributes;
191
	}
192
193
194
}
195
// End of file EES_Shortcode.shortcode.php
196
// Location: /shortcodes/EES_Shortcode.shortcode.php