-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwhich-addon-for-elementor.php
executable file
·337 lines (293 loc) · 9.76 KB
/
which-addon-for-elementor.php
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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
<?php
/**
* Plugin Name: Which Elementor Addon
* Plugin URI: https://obiplabon.im
* Description: Find the unnecessary, repeating, replaceable Elementor add-ons or widgets easily with this simple and easy to use super lightweight plugin! This plugin simply adds a tooltip which shows the widget name along with the plugin name, amazing!
* Version: 1.2.0
* Author: obiPlabon
* Author URI: https://obiplabon.im
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: which-elementor-addon
* Domain Path: /languages/
*
* @package Which_Elementor_Addon
*/
/*
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
Copyright 2019 obiPlabon <[email protected]>
*/
namespace obiPlabon;
// just like do or die
defined( 'ABSPATH' ) || die();
use Elementor\Tools;
/**
* Class Which_Elementor_Addon
*
* @package obiPlabon
*/
class Which_Elementor_Addon {
/**
* Plugin version
*/
const VERSION = '1.2.0';
/**
* Required minimum php version
*/
const REQUIRED_PHP_VERSION = '5.4';
/**
* Plugin slug
*/
const SLUG = 'which-elementor-addon';
/**
* DB field prefix
*/
const DB_PREFIX = 'which_elementor_addon_';
/**
* Store active plugins
*
* @var array
*/
private static $plugins = [];
/**
* Initialize the plugin function here
*
* @return void
*/
public static function init() {
// Check for required PHP version
if ( version_compare( PHP_VERSION, self::REQUIRED_PHP_VERSION, '<' ) ) {
add_action( 'admin_notices', [ __CLASS__, 'show_required_php_version_missing_notice' ] );
return;
}
// Check if Elementor installed and activated
if ( ! did_action( 'elementor/loaded' ) ) {
add_action( 'admin_notices', [ __CLASS__, 'show_elementor_missing_notice' ] );
return;
}
add_action( 'admin_init', [ __CLASS__, 'admin_init' ] );
add_action( 'wp_enqueue_scripts', [ __CLASS__, 'enqueue_scripts' ] );
}
// private static function get_elementor_post_types() {
// return get_post_types_by_support( 'elementor' );
// }
public static function admin_init() {
// foreach ( self::get_elementor_post_types() as $type ) {
// add_filter( 'views_edit-'. $type, [ __CLASS__, 'add_list_table_link' ] );
// }
add_action( 'elementor/admin/after_create_settings/elementor-tools', [ __CLASS__, 'add_settings_page' ] );
}
public static function get_setting( $field_id, $default = '' ) {
return get_option( self::DB_PREFIX . $field_id, $default );
}
public static function add_settings_page( Tools $tools ) {
$tools->add_tab(
self::SLUG,
[
'label' => __( 'Which Elementor Addon', 'which-elementor-addon' ),
'sections' => [
'which_elementor_addon_settings' => [
'callback' => function() {
echo '<h2>' . esc_html__( 'Label Settings', 'which-elementor-addon' ) . '</h2>';
},
'fields' => [
'enable_on' => [
'label' => __( 'Enable On', 'which-elementor-addon' ),
'full_field_id' => self::DB_PREFIX . 'enable_on',
'field_args' => [
'type' => 'checkbox_list',
'std' => ['editor'],
'options' => [
'editor' => __( 'Editor', 'which-elementor-addon' ),
'frontend' => __( 'Frontend', 'which-elementor-addon' ),
],
],
],
'show_label_on' => [
'label' => __( 'Show Label On', 'which-elementor-addon' ),
'full_field_id' => self::DB_PREFIX . 'show_label_on',
'field_args' => [
'type' => 'select',
'std' => 'hover',
'options' => [
'hover' => __( 'Hover', 'which-elementor-addon' ),
'always' => __( 'Always', 'which-elementor-addon' ),
],
],
],
'label_position' => [
'label' => __( 'Label Position', 'which-elementor-addon' ),
'full_field_id' => self::DB_PREFIX . 'label_position',
'field_args' => [
'type' => 'select',
'std' => 'hover',
'options' => [
'top-left' => __( 'Top Left', 'which-elementor-addon' ),
'top-center' => __( 'Top Center', 'which-elementor-addon' ),
'center-center' => __( 'Center Center', 'which-elementor-addon' ),
],
],
],
'show_widget_name' => [
'label' => __( 'Show Widget Name', 'which-elementor-addon' ),
'full_field_id' => self::DB_PREFIX . 'show_widget_name',
'field_args' => [
'type' => 'checkbox',
'value' => 1,
'std' => 0,
'sub_desc' => esc_html__( 'Check this box to show widget name with plugin name.', 'which-elementor-addon' ),
],
],
]
]
]
]
);
}
public static function add_list_table_link( $views ) {
$views[ self::SLUG ] = sprintf(
'<a href="%1$s">%2$s</a>',
admin_url( 'edit.php' ),
esc_html__( 'Which Elmentor Addon', 'which-elementor-addon' )
);
return $views;
}
/**
* Show required minimum php version missing notice to admin
*
* @return void
*/
public static function show_required_php_version_missing_notice() {
if ( ! self::user_can_see_notice() ) {
return;
}
$notice = sprintf(
/* translators: 1: Plugin name 2: PHP 3: Required PHP version */
esc_html__( '"%1$s" requires "%2$s" version %3$s or greater.', 'which-elementor-addon' ),
'<strong>' . esc_html__( 'Which Elementor Addon', 'which-elementor-addon' ) . '</strong>',
'<strong>' . esc_html__( 'PHP', 'which-elementor-addon' ) . '</strong>',
self::REQUIRED_PHP_VERSION
);
printf( '<div class="notice notice-warning is-dismissible"><p style="padding: 13px 0">%1$s</p></div>', $notice );
}
/**
* Show Elementor missing notice to admin
*
* @return void
*/
public static function show_elementor_missing_notice() {
if ( ! self::user_can_see_notice() ) {
return;
}
$notice = sprintf(
/* translators: 1: Plugin name 2: Elementor 3: Elementor installation link */
__( '%1$s requires %2$s to be installed and activated to function properly. %3$s', 'which-elementor-addon' ),
'<strong>' . __( 'Which Elementor Addon', 'which-elementor-addon' ) . '</strong>',
'<strong>' . __( 'Elementor', 'which-elementor-addon' ) . '</strong>',
'<a href="' . esc_url( admin_url( 'plugin-install.php?s=Elementor&tab=search&type=term' ) ) . '">' . __( 'Please click on this link and install Elementor', 'which-elementor-addon' ) . '</a>'
);
printf( '<div class="notice notice-warning is-dismissible"><p style="padding: 13px 0">%1$s</p></div>', $notice );
}
/**
* Check if current user has the capability to install or activate plugins
*
* @return bool
*/
private static function user_can_see_notice() {
return current_user_can( 'install_plugins' ) || current_user_can( 'activate_plugins' );
}
/**
* Get plugin directory name from plugin base
*
* @param $plugin_base_name
* @return bool|string
*/
private static function get_plugin_slug( $plugin_base_name ) {
return substr( $plugin_base_name, 0, strpos( $plugin_base_name, '/' ) );
}
/**
* Get the active plugins.
*
* @return array
*/
protected static function get_active_plugins() {
if ( empty( self::$plugins ) ) {
// Ensure get_plugins function is loaded
if ( ! function_exists( 'get_plugins' ) ) {
include ABSPATH . '/wp-admin/includes/plugin.php';
}
$active_plugins = get_option( 'active_plugins' );
self::$plugins = array_intersect_key( get_plugins(), array_flip( $active_plugins ) );
}
return self::$plugins;
}
/**
* @return array
*/
private static function get_data() {
$widget_types = \Elementor\Plugin::instance()->widgets_manager->get_widget_types();
$data_map = [];
$plugins = self::get_active_plugins();
foreach ( $widget_types as $widget_key => $widget_data ) {
$reflection = new \ReflectionClass( $widget_data );
$widget_file = plugin_basename( $reflection->getFileName() );
$plugin_slug = self::get_plugin_slug( $widget_file );
foreach ( $plugins as $plugin_root => $plugin_meta ) {
$_plugin_slug = self::get_plugin_slug( $plugin_root );
if ( $plugin_slug === $_plugin_slug ) {
$data_map[ $widget_key ] = [
'plugin' => $plugin_meta['Name'],
'widget' => $widget_data->get_title()
];
}
}
}
return $data_map;
}
public static function enqueue_scripts() {
if ( ! current_user_can( 'edit_posts' ) ) {
return;
}
$enable_on = self::get_setting( 'enable_on', [ 'editor' ] );
if ( empty( $enable_on ) || ! ( in_array( 'editor', $enable_on ) || in_array( 'frontend', $enable_on ) ) ) {
return;
}
wp_enqueue_style(
self::SLUG,
plugins_url( 'assets/css/which-elementor-addon.min.css', __FILE__ ),
[],
self::VERSION
);
wp_enqueue_script(
self::SLUG,
plugins_url( 'assets/js/which-elementor-addon.min.js', __FILE__ ),
[ 'jquery' ],
self::VERSION,
true
);
wp_localize_script(
self::SLUG,
'whichElementorAddon',
[
'widgetPluginMap' => self::get_data(),
'settings' => [
'enableOn' => self::get_setting( 'enable_on', ['editor'] ),
'showLabelOn' => self::get_setting( 'show_label_on', 'hover' ),
'labelPosition' => self::get_setting( 'label_position', 'top-left' ),
'showWidgetName' => (bool) self::get_setting( 'show_widget_name', 0 ),
]
]
);
}
}
Which_Elementor_Addon::init();