1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Amarkal\Shortcode; |
4
|
|
|
|
5
|
|
|
class Manager |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var Singleton The reference to *Singleton* instance of this class |
9
|
|
|
*/ |
10
|
|
|
private static $instance; |
11
|
|
|
|
12
|
|
|
private $shortcodes = array(); |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Returns the *Singleton* instance of this class. |
16
|
|
|
* |
17
|
|
|
* @return Singleton The *Singleton* instance. |
18
|
|
|
*/ |
19
|
|
|
public static function get_instance() |
20
|
|
|
{ |
21
|
|
|
if( null === static::$instance ) |
|
|
|
|
22
|
|
|
{ |
23
|
|
|
static::$instance = new static(); |
|
|
|
|
24
|
|
|
} |
25
|
|
|
return static::$instance; |
|
|
|
|
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
public function register_shortcode( $args ) |
29
|
|
|
{ |
30
|
|
|
$this->validate_args($args); |
31
|
|
|
if($this->shortcode_exists($args['id'])) |
32
|
|
|
{ |
33
|
|
|
throw new \RuntimeException("A shortcode with id '{$args['id']}' has already been registered"); |
34
|
|
|
} |
35
|
|
|
$this->shortcodes[$args['id']] = array_merge($this->default_args(), $args); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
public function print_json_object($plugin_array) |
39
|
|
|
{ |
40
|
|
|
|
41
|
|
|
return $plugin_array; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function enqueue_script($plugins_array) |
45
|
|
|
{ |
46
|
|
|
// Printing the JSON object ensures that it will be available whenever |
47
|
|
|
// the visual editor is present. |
48
|
|
|
echo "<script id='amarkal-shortcode-json' type='application/json'>{$this->prepare_json_object()}</script>"; |
49
|
|
|
|
50
|
|
|
// This script must be included after the JSON object, since it refers |
51
|
|
|
// to it, and so the JSON must be readily available. |
52
|
|
|
$plugins_array['amarkal_shortcode'] = \Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/js/dist/amarkal-shortcode.min.js'); |
53
|
|
|
return $plugins_array; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
public function enqueue_style() |
57
|
|
|
{ |
58
|
|
|
\add_editor_style(\Amarkal\Core\Utility::path_to_url(__DIR__.'/assets/css/dist/amarkal-shortcode.min.css')); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Create a JSON object that will be printed in the admin section |
63
|
|
|
* to be used by the TinyMCE plugin code. |
64
|
|
|
*/ |
65
|
|
|
private function prepare_json_object() |
66
|
|
|
{ |
67
|
|
|
$json = array(); |
68
|
|
|
foreach($this->shortcodes as $id => $shortcode) |
69
|
|
|
{ |
70
|
|
|
$popup = new Popup($shortcode['fields']); |
71
|
|
|
$json[$id] = $shortcode; |
72
|
|
|
$json[$id]['html'] = $popup->render(); |
73
|
|
|
} |
74
|
|
|
return json_encode($json); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Default shortcode arguments |
79
|
|
|
*/ |
80
|
|
|
private function default_args() |
81
|
|
|
{ |
82
|
|
|
return array( |
83
|
|
|
'id' => null, |
84
|
|
|
'title' => '', |
85
|
|
|
'template' => null, |
86
|
|
|
'cmd' => '', |
87
|
|
|
'width' => 550, |
88
|
|
|
'height' => 450, |
89
|
|
|
'fields' => array(), |
90
|
|
|
'show_placeholder' => true, |
91
|
|
|
'placeholder_class' => null |
92
|
|
|
); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Check if a shortcode with the given ID has already been registered |
97
|
|
|
*/ |
98
|
|
|
private function shortcode_exists( $id ) |
99
|
|
|
{ |
100
|
|
|
return array_key_exists($id, $this->shortcodes); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* Validate that the provided arguments have the required arguments as |
105
|
|
|
* specified in self::required_args() |
106
|
|
|
*/ |
107
|
|
|
private function validate_args( $args ) |
108
|
|
|
{ |
109
|
|
|
foreach($this->required_args() as $arg) |
110
|
|
|
{ |
111
|
|
|
if(!array_key_exists($arg, $args)) |
112
|
|
|
{ |
113
|
|
|
throw new \RuntimeException("Missing required argument '$arg'"); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* A list of required arguments |
120
|
|
|
*/ |
121
|
|
|
private function required_args() |
122
|
|
|
{ |
123
|
|
|
return array('id','template','fields'); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* Private constructor to prevent instantiation |
128
|
|
|
*/ |
129
|
|
|
private function __construct() |
130
|
|
|
{ |
131
|
|
|
\add_filter('mce_external_plugins',array($this,'enqueue_script')); |
132
|
|
|
\add_action('admin_init', array($this,'enqueue_style')); |
133
|
|
|
} |
134
|
|
|
} |
Let’s assume you have a class which uses late-static binding:
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: