-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathplugin.php
More file actions
164 lines (139 loc) · 3.45 KB
/
Copy pathplugin.php
File metadata and controls
164 lines (139 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
<?php
namespace Blocktree;
if (!defined('ABSPATH')) {
exit('Press Enter to proceed...');
}
/**
* ClientRender plugin.
*
* @since 1.0.0
*/
class Plugin
{
/**
* Instance.
*
* Holds the plugin instance.
*
* @since 1.0.0
* @access public
* @static
*
* @var $instance
*/
public static $instance = null;
/**
* Render the widget output.
*
* Written in PHP and used to generate the final HTML.
*
* @since 1.0.0
* @access public
*
*/
public function get_markup($component_name, $settings = [], $handler_key = null)
{
// $this->assets();
$handler_key = $handler_key ? $handler_key : esc_attr(get_option('blocktree_handler_key'));
$settings = json_encode($settings);
$hash = uniqid(rand(), TRUE);
return "<div id='$hash'></div>
<script>
window['$handler_key']('$component_name', document.getElementById('$hash'), $settings);
</script>";
// return "<textarea data-blocktree
// data-component-name='$component_name'
// data-handler-key='$handler_key'
// >$settings</textarea>";
}
/**
* Load the assets.
*
* @since 1.0.0
* @access private
*
*/
private function assets()
{
$widgets_files = explode(PHP_EOL, get_option('blocktree_sources'));
// mode filter
$widgets_files = array_filter($widgets_files, function($url, $key) {
$query = parse_url(trim($url), PHP_URL_QUERY);
parse_str($query, $params);
$mode = $params ? $params['mode'] : null;
if (!$mode) {
return true;
}
else if ($mode == 'admin-page' && is_admin()) {
return true;
}
elseif ($mode == 'user' && !is_admin()) {
return true;
}
else {
return false;
}
}, ARRAY_FILTER_USE_BOTH);
foreach ($widgets_files as $key => $path) {
$extension = pathinfo(strtok(trim($path), '?'), PATHINFO_EXTENSION);
if ($extension == 'js') {
wp_enqueue_script('blocktree-sources-' . $key, trim($path), [], null, false);
}
if ($extension == 'css') {
wp_enqueue_style('blocktree-sources-' . $key, trim($path));
}
}
// wp_enqueue_script('blocktree-widgets', plugin_dir_url(__FILE__) . '/blocktree-widgets-example.js');
//wp_enqueue_script('blocktree-wp', plugin_dir_url(__FILE__) . '/blocktree-wp.js');
}
/**
* Basic shortcode.
*
* Written a shortcode, using blocktree widget. [blocktree component-name="my_widget_name" value="123" /]
*
* @since 1.0.0
* @access public
*
*/
public function blocktree_shortcode($atts = array(), $content = null)
{
return $this->get_markup($atts['component-name'], $atts, $atts['handler_key']);
}
/**
* Instance.
*
* Ensures only one instance of the plugin class is loaded or can be loaded.
*
* @since 1.0.0
* @access public
* @static
*
* @return Plugin An instance of the class.
*/
public static function instance()
{
if (is_null(self::$instance)) {
self::$instance = new self();
}
}
/**
* Plugin constructor.
*
* Initializing Elementor Experts plugin.
*
* @since 1.0.0
* @access private
*/
private function __construct()
{
// setup settings page
new Settings();
// add shortcodes
add_shortcode('blocktree', [$this, 'blocktree_shortcode']);
// add assets
add_action('init', function () {
$this->assets();
}, 100);
}
}
Plugin::instance();