-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathclass-code-widget.php
458 lines (402 loc) · 14.6 KB
/
class-code-widget.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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
<?php
/**
* Plugin Name: Code Widget
* Plugin URI: https://wordpress.org/plugins/code-widget/
* Description: Code Widget help you to run <code>Code</code> and simple text in widget which have different type <code>Short Code</code> <code>PHP Code</code>. Yes, you can also add <code>TEXT</code> and <code>HTML</code>.
* Version: 1.0.15
* Author: Solution Box
* Author URI: https://solbox.dev/
* Text Domain: code-widget
* Domain Path: /languages/
* License: GPLv2
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
*
* @package code widget
*/
/** If this file is called directly, abort. */
if ( ! defined( 'ABSPATH' ) ) {
die;
}
if ( ! defined( 'CODE_WIDGET_PATH' ) ) {
/**
* Absolute path of this plugin
*
* @since 1.0
*/
define( 'CODE_WIDGET_PATH', trailingslashit( plugin_dir_path( __FILE__ ) ) );
}
define( 'CODE_WIDGET_VERSION', '1.0.15' );
define( 'CODE_WIDGET_TEXT_DOMAIN', 'code-widget' );
if ( ! class_exists( 'Code_Widget' ) ) {
/**
* Adds Code_Widget.
*/
class Code_Widget extends WP_Widget {
/**
* Instance of the class
*
* @var instance
*/
public static $instance;
/**
* Register widget with WordPress.
*/
public function __construct() {
load_plugin_textdomain( CODE_WIDGET_TEXT_DOMAIN, false, dirname( plugin_basename( __FILE__ ) ) );
add_action( 'admin_init', [ $this, 'dismiss_review_notice' ] );
add_action( 'admin_init', [ $this, 'show_review_notice' ] );
add_action( 'wp_ajax_cw_deactivation_feedback', [ $this, 'deactivation_feedback' ] );
add_action( 'admin_notices', [ $this, 'sb_promote_plugins' ] );
parent::__construct(
'codewidget', // Base ID.
esc_html__( 'Code Widget', CODE_WIDGET_TEXT_DOMAIN ), // Name.
array(
'description' => esc_html__( 'Any Text,Short Code,HTML,PHP Code .', CODE_WIDGET_TEXT_DOMAIN ),
'show_instance_in_rest' => true,
) // Args.
);
require CODE_WIDGET_PATH .'/lib/solbox-plugin-deactivation-survey/deactivate-feedback-form.php';
}
/**
* Front-end display of widget.
*
* @see WP_Widget::widget()
*
* @param array $args Widget arguments.
*
* @param array $instance Saved values from database.
*/
public function widget( $args, $instance ) {
$cw_type = $instance['cw_type'];
$cw_filter = $instance['cw_filter'];
$cw_content = apply_filters( 'cw_content', $instance['cw_content'], $this );
if ( 'php_code' == $cw_type ) {
$cw_final_content = $this->php_exe( $cw_content );
}
if ( 'short_code' == $cw_type ) {
$cw_final_content = do_shortcode( $cw_content );
}
if ( 'html_code' == $cw_type ) {
$cw_final_content = convert_smilies( balanceTags( $cw_content ) );
}
if ( 'text_code' == $cw_type ) {
$cw_final_content = wptexturize( esc_html( $cw_content ) );
}
$cw_final_content = apply_filters( 'cw_final_content', $cw_final_content );
echo $args['before_widget'];
if ( ! empty( $instance['title'] ) ) {
echo $args['before_title'] . esc_html( apply_filters( 'widget_title', $instance['title'] ) ) . $args['after_title'];
}
echo '<div class="code-widget">' . ( $cw_filter ? wpautop( $cw_final_content ) : $cw_final_content ) . '</div>';
echo $args['after_widget'];
}
/**
* Back-end widget form.
*
* @see WP_Widget::form()
*
* @param array $instance Previously saved values from database.
*/
public function form( $instance ) {
if ( 0 == count( $instance ) ) {
$instance['cw_type'] = ! empty( $instance['cw_type'] ) ? $instance['cw_type'] : 'short_code';
$instance['cw_content'] = ! empty( $instance['cw_content'] ) ? $instance['cw_content'] : esc_html__( 'your code ....', CODE_WIDGET_TEXT_DOMAIN );
$instance['cw_filter'] = ! empty( $instance['cw_filter'] ) ? $instance['cw_filter'] : 0;
$title = ! empty( $instance['title'] ) ? $instance['title'] : esc_html__( 'New title', CODE_WIDGET_TEXT_DOMAIN );
} else {
$instance['cw_type'] = $instance['cw_type'];
$instance['cw_content'] = $instance['cw_content'];
$instance['cw_filter'] = $instance['cw_filter'];
$title = $instance['title'];
}
?>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_attr_e( 'Title:', CODE_WIDGET_TEXT_DOMAIN ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text"
value="<?php echo esc_attr( $title ); ?>">
</p>
<p>
<label for="<?php echo esc_attr( $this->get_field_id( 'cw_type' ) ); ?>">
<?php esc_attr_e( 'Widget Type:', CODE_WIDGET_TEXT_DOMAIN ); ?>
</label>
</p>
<select name="<?php echo esc_html( $this->get_field_name( 'cw_type' ) ); ?>" class="widefat" id="<?php esc_html_e( $this->get_field_id( 'cw_type' ) ); ?>">
<option value="short_code" <?php selected( $instance['cw_type'], 'short_code' ); ?>>
<?php esc_attr_e( 'Short Code', CODE_WIDGET_TEXT_DOMAIN ); ?>
</option>
<option value="php_code" <?php selected( $instance['cw_type'], 'php_code' ); ?>>
<?php esc_attr_e( 'PHP Code', CODE_WIDGET_TEXT_DOMAIN ); ?>
</option>
<option value="html_code" <?php selected( $instance['cw_type'], 'html_code' ); ?>>
<?php esc_attr_e( 'HTML', CODE_WIDGET_TEXT_DOMAIN ); ?>
</option>
<option value="text_code" <?php selected( $instance['cw_type'], 'text_code' ); ?>>
<?php esc_attr_e( 'Text', CODE_WIDGET_TEXT_DOMAIN ); ?>
</option>
</select>
<p>
<textarea class="widefat" rows="12" cols="20" id="<?php echo esc_attr( $this->get_field_id( 'cw_content' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'cw_content' ) ); ?>"><?php echo $instance['cw_content']; ?></textarea>
</p>
<p><input id="<?php echo esc_attr( $this->get_field_id( 'cw_filter' ) ); ?>"
name="<?php echo esc_attr( $this->get_field_name( 'cw_filter' ) ); ?>"
type="checkbox" <?php checked( $instance['cw_filter'], 'on' ); ?>/> <label
for="<?php echo esc_html( $this->get_field_id( 'cw_filter' ) ); ?>"><?php esc_html_e( 'Automatically add paragraphs.', CODE_WIDGET_TEXT_DOMAIN ); ?></label>
<a href="https://buy.paddle.com/product/640837" target="_blank"><?php _e( 'Donate this plugin', CODE_WIDGET_TEXT_DOMAIN )?></a>
</p>
<?php
}
/**
* Sanitize widget form values as they are saved.
*
* @see WP_Widget::update()
*
* @param array $new_instance Values just sent to be saved.
*
* @param array $old_instance Previously saved values from database.
*
* @return array Updated safe values to be saved.
*/
public function update( $new_instance, $old_instance ) {
$instance = array();
$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
$instance['cw_type'] = ( ! empty( $new_instance['cw_type'] ) ) ? strip_tags( $new_instance['cw_type'] ) : '';
$instance['cw_content'] = ( ! empty( $new_instance['cw_content'] ) ) ? strip_tags( $new_instance['cw_content'] ) : '';
$instance['cw_filter'] = ( ! empty( $new_instance['cw_filter'] ) ) ? strip_tags( $new_instance['cw_filter'] ) : 0;
/*
Unfiltered_html
Since 2.0
Allows user to post HTML markup or even JavaScript code in pages, posts, comments and widgets.
Note: Enabling this option for untrusted users may result in their posting malicious or poorly formatted code.
Note: In WordPress Multisite, only Super Admins have the unfiltered_html capability.
*/
if ( current_user_can( 'unfiltered_html' ) ) {
$instance['cw_content'] = $new_instance['cw_content'];
} else {
$instance['cw_content'] = stripslashes( wp_filter_post_kses( $new_instance['cw_content'] ) );
}
return $instance;
}
/**
* Php exe use to excute php code in string
*
* @param string $content string of content.
*/
private function php_exe( $content ) {
apply_filters( 'before_cw_php_exe', $content );
ob_start();
eval( '?>' . $content );
$text = ob_get_contents();
ob_end_clean();
return apply_filters( 'after_cw_php_exe', $text );
}
/**
* Returns the current instance of the class, in case some other
* plugin needs to use its public methods.
*
* @since 1.0.0
*
* @access public
*
* @return Code_Widget Returns the current instance of the class
*/
public static function get_instance() {
// If the single instance hasn't been set, set it now.
if ( null == self::$instance ) {
self::$instance = new self();
}
return self::$instance;
}
/**
* Check installation time set admin notice.
*
* @since 1.0.5
* @return void
*/
public function show_review_notice() {
$install_date = get_option( 'cw_activation_time' );
$already_done = get_option( 'cwrn_dismiss' );
$show_later_date = get_option( 'cwrn_show_later' );
$now = strtotime( 'now' );
// If already done don't show.
if ( $already_done ) {
return;
}
if ( ! $install_date ) {
update_option( 'cw_activation_time', strtotime( '-7 days' ) );
$install_date = strtotime( '-7 days' );
}
$past_date = strtotime( '-7 days' );
// If show later exist then must me greater than set time.
if ( ( $show_later_date && $now < $show_later_date ) ) {
return;
}
if ( $past_date >= $install_date ) {
add_action( 'admin_notices', array( $this, 'review_admin_notice' ) );
}
}
/**
* Review Show admin notice.
*
* @since 1.0.5
* @return void
*/
public function review_admin_notice() {
// WordPress global variable.
global $pagenow;
if ( $pagenow == 'index.php' ) {
$show_later = add_query_arg(
array(
'cw_show_later' => '1',
'_wpnonce' => wp_create_nonce( 'show-later' ),
),
get_admin_url()
);
$already_done = add_query_arg(
array(
'cw_already_done' => '1',
'_wpnonce' => wp_create_nonce( 'already-done' ),
),
get_admin_url()
);
$review_url = esc_url( 'https://wordpress.org/support/plugin/code-widget/reviews/#new-post' );
printf(__('<div class="notice notice-info"><p>You have been using <b> Code Widget </b> for a while. We hope you liked it ! Please give us a quick rating, it works as a boost for us to keep working on the plugin !</p><p class="action">
<a href="%s" class="button button-primary" target="_blank">Rate Now!</a>
<a href="%s" class="button button-secondary "> Show Later </a>
<a href="%s" class="void-grid-review-done"> Already Done !</a>
</p></div>', CODE_WIDGET_TEXT_DOMAIN ), $review_url, $show_later, $already_done );
}
}
/**
* Handel notice action.
*
* @since 1.0.5
* @return void
*/
public function dismiss_review_notice() {
if ( isset( $_GET['cw_show_later'] ) && isset( $_GET['_wpnonce'] ) ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'show-later' ) ) {
update_option( 'cwrn_show_later', strtotime( '+2 days' ) );
}
}
if ( isset( $_GET['cw_already_done'] ) && isset( $_GET['_wpnonce'] ) ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'already-done' ) ) {
update_option( 'cwrn_dismiss', true );
}
}
if ( isset( $_GET['satc_dismiss'] ) && isset( $_GET['_wpnonce'] ) ) {
if ( wp_verify_nonce( $_GET['_wpnonce'], 'satc_dismiss' ) ) {
update_option( 'satc_dismiss', true );
}
}
}
/**
* Submission deactivation feedback.
*
* @since 1.0.7
* @return void
*/
public function deactivation_feedback() {
check_ajax_referer( 'solbox-plugin-deactivate-nonce', 'security' );
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( 'Jack;@123// 1...' );
}
$_data = json_decode( wp_unslash( $_POST['plugin'] ), true );
$email = get_option( 'admin_email' );
$reason = sanitize_text_field( wp_unslash( $_data['reasons'][ $_POST['reason'] ] ) );
$reason_detail = '';
if ( 'other' == $_POST['reason'] ) {
$reason_detail = sanitize_text_field( wp_unslash( $_POST['comments'] ) );
}
if ( 'found-better-plugin' == $_POST['reason'] ) {
$reason_detail = sanitize_text_field( wp_unslash( $_POST['plugin-name'] ) );
}
$fields = [
'email' => $email,
'website' => get_site_url(),
'action' => 'deactivate',
'reason' => $reason,
'reason_detail' => $reason_detail,
'blog_language' => get_bloginfo( 'language' ),
'wordpress_version' => get_bloginfo( 'version' ),
'php_version' => PHP_VERSION,
'plugin_version' => CODE_WIDGET_VERSION,
'plugin_name' => 'Code Widget',
];
$response = wp_remote_post(
'https://solbox.dev/',
[
'method' => 'POST',
'timeout' => 5,
'httpversion' => '1.0',
'blocking' => false,
'headers' => [],
'body' => $fields,
]
);
wp_die();
}
/**
* Review Show admin notice.
*
* @since 1.0.10
* @return void
*/
public function sb_promote_plugins() {
// update_option( 'satc_dismiss', '' );
$already_dismiss = get_option( 'satc_dismiss' );
if ( $already_dismiss ) {
return;
}
// WordPress global variable.
global $pagenow;
$action = 'install-plugin';
$slug = 'sticky-add-to-cart-woo';
$satc_url = wp_nonce_url(
add_query_arg(
array(
'action' => $action,
'plugin' => $slug,
),
admin_url( 'update.php' )
),
$action . '_' . $slug
);
$dismiss_action = add_query_arg(
array(
'satc_dismiss' => '1',
'_wpnonce' => wp_create_nonce( 'satc_dismiss' ),
),
get_admin_url()
);
if ( class_exists( 'WooCommerce' ) ) {
printf( __( '<div class="notice notice-info is-dismissible"><p>Worried about conversion rates?😟 No worries,🥳 <a href="%s" style="text-decoration:none" class="button button-secondary"><b>Install</b></a> Simple Sticky Add To Cart For WooCommerce to increase the conversion rate of product page. </p> <a href="%s"><button type="button" class="notice-dismiss"><span class="screen-reader-text">Dismiss this notice.</span></button></a></div>' ), $satc_url, $dismiss_action );
}
}
} // class Code_Widget.
/**
* Widget Register hook callback.
*
* @since 1.0.0
* @version 1.0.5
* @return void
*/
function register_code_widget() {
/** Initialises an object of this class */
register_widget( 'Code_Widget' );
}
add_action( 'widgets_init', 'register_code_widget' );
}
register_activation_hook( __FILE__, 'cw_activation_time' );
/**
* Plugin Activation hook callback.
*
* @since 1.0.0
* @return void
*/
function cw_activation_time() {
$get_activation_time = strtotime( 'now' );
update_option( 'cw_activation_time', $get_activation_time ); // replace your_plugin with Your plugin name
}
?>