-
Notifications
You must be signed in to change notification settings - Fork 9
/
gk-nsp.php
759 lines (686 loc) · 22.8 KB
/
gk-nsp.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
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
<?php
/*
Plugin Name: GK News Show Pro
Plugin URI: http://www.gavick.com/
Description: Advanced widget for the posts display
Version: 1.0.1.6
Author: GavickPro
Author URI: http://www.gavick.com/
License: GPLv2 or later
License URI: http://www.gnu.org/licenses/gpl-2.0.html
*/
/*
Copyright 2013-2013 GavickPro ([email protected])
this program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2, as
published by the Free Software Foundation.
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 St, Fifth Floor, Boston, MA 02110-1301 USA
*/
if ( !defined( 'WPINC' ) ) {
die;
}
if ( !defined( 'GK_DS' ) ) {
define('GK_DS', DIRECTORY_SEPARATOR);
}
include(dirname(__FILE__) . '/gk-nsp-form.php');
include(dirname(__FILE__) . '/gk-nsp-form-walkers.php');
include(dirname(__FILE__) . '/gk-nsp-helpers.php');
include(dirname(__FILE__) . '/gk-nsp-image-filters.php');
include(dirname(__FILE__) . '/gk-nsp-shortcodes.php');
/**
* i18n - language files should be like gk-nsp-en_GB.po and gk-nsp-en_GB.mo
*/
add_action( 'plugins_loaded', 'gk_nsp_load_textdomain' );
function gk_nsp_load_textdomain() {
load_plugin_textdomain( 'gk-nsp', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' );
}
/**
* Register the GK News Show Pro Widget.
*
* Hooks into the widgets_init action.
*/
function gk_nsp_register() {
register_widget( 'GK_NewsShowPro_Widget' );
}
add_action( 'widgets_init', 'gk_nsp_register' );
/**
* install & uninstall
*/
register_activation_hook( __FILE__, array( 'GK_NewsShowPro_Widget', 'install' ) );
register_deactivation_hook( __FILE__, array( 'GK_NewsShowPro_Widget', 'uninstall' ) );
//
add_action('edit_post', array('GK_NewsShowPro_Widget', 'refresh_cache'));
add_action('post_updated', array('GK_NewsShowPro_Widget', 'refresh_slugs'), 10, 3);
add_action('delete_post', array('GK_NewsShowPro_Widget', 'refresh_cache'));
add_action('trashed_post', array('GK_NewsShowPro_Widget', 'refresh_cache'));
add_action('save_post', array('GK_NewsShowPro_Widget', 'refresh_cache'));
/**
* The main widget class
*/
class GK_NewsShowPro_Widget extends WP_Widget {
// variable used to store the object configuration
public $config = array(
'title' => '',
'widget_css_suffix' => '',
'use_css' => 'on',
// data source
'data_source_type' => 'wp-latest',
'data_source' => '',
'json_filelist' => '',
'xml_filelist' => '',
'wp_category_list' => array(),
'woocommerce_category_list' => array(),
'post_types_list' => array(),
'orderby' => 'ID',
'one_per_category' => 'off',
'order' => 'DESC',
'offset' => '0',
'data_source_blog' => '',
'article_wrapper' => 'default',
// articles amount
'article_pages' => '1',
'article_cols' => '1',
'article_rows' => '1',
// links amount
'links_pages' => '0',
'links_rows' => '0',
// paginations
'article_pagination' => 'pagination',
'links_pagination' => 'pagination',
// article format
'article_format' => 'none',
// article title format
'article_title_state' => 'on',
'article_title_len' => '10',
'article_title_len_type' => 'words',
'article_title_order' => '1',
// article text format
'article_text_state' => 'on',
'article_text_len' => '20',
'article_text_len_type' => 'words',
'article_text_order' => '2',
'parse_shortcodes' => 'off',
// article text format
'article_image_state' => 'on',
'article_image_w' => '160',
'article_image_h' => '120',
'article_image_pos' => 'top',
'article_image_order' => '3',
'article_image_popup' => 'on',
'article_image_filter' => 'none',
'article_default_image' => '',
// article info format
'article_info_state' => 'on',
'article_info_format' => '{DATE} {CATEGORY} {AUTHOR} {COMMENTS}',
'article_info_date_format' => 'd M Y',
'article_info_order' => '4',
// article readmore format
'article_readmore_state' => 'on',
'article_readmore_order'=> '5',
// links title format
'links_title_state' => 'on',
'links_title_len' => '10',
'links_title_len_type' => 'words',
// links text format
'links_text_state' => 'on',
'links_text_len' => '20',
'links_text_len_type' => 'words',
// links image format
'links_image_state' => 'off',
'links_image_w' => '80',
'links_image_h' => '60',
'links_default_image' => '',
'links_image_filter' => 'none',
'links_image_popup' => 'off',
'links_image_block_padding' => '',
'links_readmorelink_state' => 'off',
// links readmore format
'links_readmore_state' => 'off',
'links_readmore_text' => '',
'links_readmore_url' => '',
'links_readmore_title_state' => 'on',
// paddings
'article_block_padding' => '20px 0',
'image_block_padding' => '0',
// cache time
'cache_time' => '60',
// Autoanimation
'autoanim' => 'off',
'autoanim_interval' => '5000',
'autoanim_hover' => 'on',
// Font Awesome
'fontawesome_state' => 'off'
);
// variable used to store the object query results
public $wdgt_results;
// variable used to store the widget CSS class
public $wdgt_class;
// variable used for the internal classes
private $generator;
// variables for storing pathes
public $ds_path;
public $aw_path;
public $af_path;
/**
*
* Constructor
*
* @return void
*
**/
function __construct() {
// paths
$this->ds_path = dirname(__FILE__) . GK_DS . 'data_sources';
$this->aw_path = dirname(__FILE__) . GK_DS . 'article_wrappers';
$this->af_path = dirname(__FILE__) . GK_DS . 'article_formats';
//
$widget_ops = array(
'classname' => 'gk_nsp',
'description' => __( 'Use this widget to show recent items with images and additional elements like title, date etc', 'gk-nsp')
);
$control_ops = array(
'width' => 400,
'height' => 240
);
parent::__construct('gk_nsp', __( 'News Show Pro by GavickPro', 'gk-nsp' ), $widget_ops, $control_ops );
//
$this->alt_option_name = 'gk_nsp';
// DEV-FEATURE:
//$this->cleanup();
//
add_action('wp_enqueue_scripts', array($this, 'add_js'));
add_action('wp_enqueue_scripts', array($this, 'add_css'));
add_action('admin_enqueue_scripts', array($this, 'add_admin_js'));
add_action('admin_enqueue_scripts', array($this, 'add_admin_css'));
}
/**
*
*
*
*/
static function install() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$upload_dir = wp_upload_dir();
if(is_multisite() && stripos($upload_dir['baseurl'], '/sites/') !== FALSE) {
$upload_dir_basedir = substr($upload_dir['basedir'], 0, stripos($upload_dir['basedir'], '/sites/'));
} else {
$upload_dir_basedir = $upload_dir['basedir'];
}
$files = array(
array(
'base' => $upload_dir_basedir . '/gk_nsp_cache',
'file' => 'index.html',
'content' => ''
),
array(
'base' => $upload_dir_basedir . '/gk_nsp_cache/overrides',
'file' => 'index.html',
'content' => ''
),
array(
'base' => $upload_dir_basedir . '/gk_nsp_external_data',
'file' => 'index.html',
'content' => ''
)
);
foreach ( $files as $file ) {
if ( wp_mkdir_p( $file['base'] ) && ! file_exists( trailingslashit( $file['base'] ) . $file['file'] ) ) {
if ( $file_handle = @fopen( trailingslashit( $file['base'] ) . $file['file'], 'w' ) ) {
fwrite( $file_handle, $file['content'] );
fclose( $file_handle );
}
}
}
}
/**
*
*
*
*/
static function uninstall() {
if ( ! current_user_can( 'activate_plugins' ) ) {
return;
}
$upload_dir = wp_upload_dir();
if(is_multisite() && stripos($upload_dir['baseurl'], '/sites/') !== FALSE) {
$upload_dir_basedir = substr($upload_dir['basedir'], 0, stripos($upload_dir['basedir'], '/sites/'));
} else {
$upload_dir_basedir = $upload_dir['basedir'];
}
// remove the dir for the image cache
if(is_dir($upload_dir_basedir . '/gk_nsp_cache')) {
// error flag for the files remove
$error_flag = false;
// generate the list of files to remove
$files = scandir($upload_dir_basedir . '/gk_nsp_cache');
// remove all files in the directory
foreach($files as $file) {
if($file != '.' && $file != '..') {
if(!unlink($upload_dir_basedir . GK_DS . 'gk_nsp_cache' . GK_DS . $file)) {
$error_flag = true;
}
}
}
// remove the directory if all files was removed
if(!$error_flag) {
rmdir($upload_dir_basedir . '/gk_nsp_cache');
}
}
// remove the dir for the external data
if(is_dir($upload_dir_basedir . '/gk_nsp_external_data')) {
// error flag for the files remove
$error_flag = false;
// generate the list of files to remove
$files = scandir($upload_dir_basedir . '/gk_nsp_external_data');
// remove all files in the directory
foreach($files as $file) {
if($file != '.' && $file != '..') {
if(!unlink($upload_dir_basedir . GK_DS . 'gk_nsp_external_data' . GK_DS . $file)) {
$error_flag = true;
}
}
}
// remove the directory if all files was removed
if(!$error_flag) {
rmdir($upload_dir_basedir . '/gk_nsp_external_data');
}
}
}
/*
*
*
*
*/
function cleanup() {
$active_instances_info = get_option('sidebars_widgets');
$active_instances = array();
if(count($active_instances_info) > 0 && is_array($active_instances_info)) {
foreach($active_instances_info as $sidebar_name => $widgets) {
if(count($widgets) > 0 && is_array($widgets)) {
foreach($widgets as $widget) {
if(stripos($widget, 'gk_nsp-') !== FALSE) {
array_push($active_instances, str_replace('gk_nsp-', '', $widget));
}
}
}
}
}
$instances = get_option('widget_gk_nsp');
foreach($instances as $key => $widget) {
if(!in_array($key, $active_instances) && is_array($instances[$key])) {
unset($instances[$key]);
}
}
update_option('widget_gk_nsp', $instances);
}
function add_js() {
// read the widget settings
$json_cache = get_option('widget_gk_nsp_json_cache');
$instances = get_option('widget_gk_nsp');
$loaded_files = array();
$thickbox_loaded = false;
// check if the instances are correct
if(is_array($instances) || is_object($instances)) {
// iterate through instances
foreach($instances as $instance) {
if(is_array($instance)) {
$article_wrapper = isset($instance['article_wrapper']) ? $instance['article_wrapper'] : $this->config['article_wrapper'];
$article_image_popup = isset($instance['article_image_popup']) ? $instance['article_image_popup'] : $this->config['article_image_popup'];
// check if the wrapper exist in the specific instance and isn't duplicated
if($article_wrapper != '' && !in_array($article_wrapper, $loaded_files)) {
// check the type of wrapper
if($article_wrapper == 'default') {
wp_register_script( 'gk-nsp-default', plugins_url('gk-nsp.js', __FILE__), array('jquery'), false, true);
wp_enqueue_script('gk-nsp-default');
} else {
if(isset($json_cache[$article_wrapper]) && $json_cache[$article_wrapper]) {
if($json_cache[$article_wrapper]->js == true) {
wp_register_script( 'gk-nsp-' . $article_wrapper, plugins_url('article_wrappers/'.$article_wrapper.'/'. $article_wrapper .'.js', __FILE__), array('jquery'), false, true);
wp_enqueue_script('gk-nsp-' . $article_wrapper);
}
}
}
// push the wrapper to teh list - avoid duplicates
array_push($loaded_files, $article_wrapper);
}
// load Thickbox script if popup is used
if(!$thickbox_loaded && $article_image_popup === 'on') {
wp_enqueue_script('thickbox');
$thickbox_loaded = true;
}
}
}
}
}
function add_css() {
// read the widget settings
$json_cache = get_option('widget_gk_nsp_json_cache');
$instances = get_option('widget_gk_nsp');
$loaded_files = array();
$thickbox_loaded = false;
// check if the instances are correct
if(is_array($instances) || is_object($instances)) {
// iterate through instances
foreach($instances as $instance) {
if(is_array($instance)) {
$use_css = isset($instance['use_css']) ? $instance['use_css'] : $this->config['use_css'];
$fontawesome_state = isset($instance['fontawesome_state']) ? $instance['fontawesome_state'] : $this->config['fontawesome_state'];
$article_wrapper = isset($instance['article_wrapper']) ? $instance['article_wrapper'] : $this->config['article_wrapper'];
$article_image_popup = isset($instance['article_image_popup']) ? $instance['article_image_popup'] : $this->config['article_image_popup'];
// check if the wrapper exist in the specific instance and isn't duplicated
if(
$use_css == 'on' &&
$article_wrapper != '' &&
!in_array($article_wrapper, $loaded_files)
) {
// check the type of wrapper
if($article_wrapper == 'default') {
wp_register_style( 'gk-nsp', plugins_url('gk-nsp.css', __FILE__), array(), false, 'all');
wp_enqueue_style('gk-nsp');
} else {
if(isset($json_cache[$article_wrapper]) && $json_cache[$article_wrapper]) {
if($json_cache[$article_wrapper]->css == true) {
wp_register_style( 'gk-nsp-' . $article_wrapper, plugins_url('article_wrappers/'.$article_wrapper.'/'. $article_wrapper .'.css', __FILE__), array(), false, 'all');
wp_enqueue_style('gk-nsp-' . $article_wrapper);
}
}
}
// push the wrapper to teh list - avoid duplicates
array_push($loaded_files, $article_wrapper);
}
// load Thickbox stylesheet if popup is used
if(!$thickbox_loaded && $article_image_popup === 'on') {
wp_enqueue_style('thickbox');
$thickbox_loaded = true;
}
// load Font Awesome stylesheet
if($fontawesome_state === 'on') {
wp_register_style( 'gk-nsp-font-awesome', '//netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.min.css', array(), false, 'all' );
wp_enqueue_style( 'gk-nsp-font-awesome' );
}
}
}
}
}
function add_admin_js() {
wp_enqueue_media();
wp_register_script( 'gk-nsp', plugins_url('gk-nsp-admin.js', __FILE__), array('jquery', 'thickbox'), false, 'all');
wp_enqueue_script('gk-nsp');
}
function add_admin_css() {
wp_register_style( 'gk-nsp', plugins_url('gk-nsp-admin.css', __FILE__), array(), false, 'all');
wp_enqueue_style('gk-nsp');
}
/**
*
* Outputs the HTML code of this widget.
*
* @param array An array of standard parameters for widgets in this theme
* @param array An array of settings for this widget instance
* @return void
*
**/
function widget($args, $instance) {
$cache = get_transient('widget_' . $this->id);
// the part with the title and widget wrappers cannot be cached!
// in order to avoid problems with the calculating columns
//
extract($args, EXTR_SKIP);
//
if((is_array($instance) || is_object($instance)) && count($instance)) {
foreach($instance as $key => $value) {
if($key == 'title') {
$this->config['title'] = apply_filters('widget_title', !isset($instance['title']) ? $this->config['title'] : $instance['title'], $instance, $this->id_base);
} else {
$this->config[$key] = !isset($instance[$key]) ? $this->config[$key] : $instance[$key];
}
}
}
extract($this->config);
echo $before_widget;
if($title != '') {
// check if the links should be displayed in the title
$title_link = $links_readmore_title_state == 'on' && $links_readmore_url != '';
echo $before_title;
// open the link
if($title_link) {
echo '<a href="'.$links_readmore_url.'">';
}
// output the title
echo $title;
// close the link
if($title_link) {
echo '</a>';
}
echo $after_title;
}
if($cache && $cache_time > 0) {
echo $cache;
echo $after_widget;
return;
}
// start cache buffering
ob_start();
// get the posts data
// let's save the global $post variable
global $post;
$tmp_post = $post;
// change the source blog
if(is_multisite() && $data_source_blog != '' && is_numeric($data_source_blog)) {
switch_to_blog($data_source_blog);
}
// generate the data source class name
$data_source_prefix = explode('-', $data_source_type);
if(!(
file_exists($this->ds_path . GK_DS . $data_source_prefix[0] . GK_DS . $data_source_prefix[0] . '.php') &&
file_exists($this->aw_path . GK_DS . $article_wrapper . GK_DS . 'helper.php')
)) {
_e('The specified data source is not correct', 'gk-nsp');
echo $after_widget;
return false;
}
require_once($this->ds_path . GK_DS . $data_source_prefix[0] . GK_DS . $data_source_prefix[0] . '.php');
require_once($this->aw_path . GK_DS . $article_wrapper . GK_DS . 'helper.php');
$data_source_class = 'GK_NSP_Data_Source_' . $data_source_prefix[0];
$article_wrapper_helper_class = 'GK_NSP_Article_Wrapper_' . $article_wrapper;
// get the results
// PHP >= 5.3.* version
// $results = $data_source_class::get_results($this, $article_wrapper_helper_class::number_of_articles($this->config));
// PHP 5.2.* version
$num_of_arts = call_user_func(array($article_wrapper_helper_class, 'number_of_articles'), $this->config);
$results = call_user_func(array($data_source_class, "get_results"), $this, $num_of_arts);
// back to the current blog
if(is_multisite() && $data_source_blog != '' && is_numeric($data_source_blog)) {
restore_current_blog();
}
// restore the global $post variable
$post = $tmp_post;
// parse the data into a widget code
// prepare widget classes
$this->wdgt_class = 'gk-nsp';
if(trim($widget_css_suffix) != '') {
$this->wdgt_class .= ' ' . $this->config['widget_css_suffix'];
}
// check if the results exists
if(count($results)) {
//
$tmp_post = $post;
// change the source blog
if(is_multisite() && $data_source_blog != '' && is_numeric($data_source_blog)) {
switch_to_blog($data_source_blog);
}
$this->wdgt_results = $results;
include($this->aw_path . GK_DS . $article_wrapper . GK_DS . $article_wrapper . '.php');
// back to the current blog
if(is_multisite() && $data_source_blog != '' && is_numeric($data_source_blog)) {
restore_current_blog();
}
// restore the global $post variable
$post = $tmp_post;
} else {
// ToDo: add something more interesting with graphic image
echo '<p>' . __('Warning! There is no posts to display. Please check your widget settings', 'gk-nsp') . '</p>';
}
// save the cache results
$cache_output = ob_get_clean();
echo $cache_output;
$cache_time = ($cache_time == '' || !is_numeric($cache_time)) ? 60 : (int) $cache_time;
set_transient('widget_' . $this->id, $cache_output, $cache_time * 60);
//
echo $after_widget;
}
/**
*
* Used in the back-end to render the widget form
*
* @param array instance of the widget settings
* @return void - the HTML output of the widget form
*
**/
function form($instance) {
//
// save the widget json cache
//
$default_language = 'en_US';
$language = get_locale() != '' ? get_locale() : $default_language;
$format_files = scandir($this->aw_path);
$base_path = $this->aw_path . GK_DS;
$json_results = array();
// iterate through files
foreach($format_files as $file) {
// filter the files
if(
$file != '.' &&
$file != '..' &&
is_dir($base_path . $file)
) {
$json_data = '';
// read the json file
if(is_file($base_path . $file . GK_DS . 'config-' . $language . '.json')) {
$json_data = json_decode(file_get_contents($base_path . $file . GK_DS . 'config-' . $language . '.json'));
} else {
$json_data = json_decode(file_get_contents($base_path . $file . GK_DS . 'config-' . $default_language . '.json'));
}
// if the data are proper
if(is_object($json_data) && isset($json_data->css)) {
$json_results[$json_data->name] = $json_data;
}
}
}
// storage the results
update_option('widget_gk_nsp_json_cache', $json_results);
// get the proper values of options
foreach($instance as $key => $value) {
$this->config[$key] = !isset($instance[$key]) ? $this->config[$key] : $instance[$key];
}
// generate the form
$form = new GK_NSP_Widget_Form();
$form->get_form($this, $instance);
}
/**
*
* Used in the back-end to update the widget options
*
* @param array new instance of the widget settings
* @param array old instance of the widget settings
* @return updated instance of the widget settings
*
**/
function update( $new_instance, $old_instance ) {
//
// save the widget settings
//
$instance = $old_instance;
if(count($new_instance) > 0) {
foreach($new_instance as $key => $option) {
if(is_string($new_instance[$key])) {
$instance[$key] = esc_attr(strip_tags($new_instance[$key]));
} else {
if(is_array($new_instance[$key])) {
foreach($new_instance[$key] as $id => $value) {
$new_instance[$key][$id] = esc_attr(strip_tags($value));
}
$instance[$key] = $new_instance[$key];
}
}
}
}
// array of checkboxes
$checkboxes = array(
'use_css',
'one_per_category',
'autoanim',
'autoanim_hover',
'article_image_popup',
'links_image_popup',
'links_readmorelink_state',
'parse_shortcodes'
);
// check checkboxes
foreach($instance as $key => $option) {
if(substr($key, -6) == '_state' || in_array($key, $checkboxes)) {
if(!isset($new_instance[$key])) {
$instance[$key] = 'off';
}
}
}
delete_transient('widget_' . $this->id);
$alloptions = wp_cache_get('alloptions', 'options');
if(isset($alloptions['gk_nsp'])) {
delete_option( 'gk_nsp' );
}
// return teh instance
return $instance;
}
/**
*
* Refreshes the widget cache data (for all instances)
*
* @return void
*
**/
static function refresh_cache() {
if(is_array(get_option('widget_gk_nsp'))) {
$ids = array_keys(get_option('widget_gk_nsp'));
for($i = 0; $i < count($ids); $i++) {
if(is_numeric($ids[$i])) {
delete_transient('widget_gk_nsp-' . $ids[$i]);
}
}
}
}
/**
*
* Refreshes the post slugs (for all instances)
*
* @return void
*
**/
static function refresh_slugs($id, $post_after, $post_before) {
$instances = get_option('widget_gk_nsp');
// check if the instances are correct
if(is_array($instances) || is_object($instances)) {
// iterate through instances
foreach($instances as $widget_id => $instance) {
// check if the wrapper exist in the specific instance and isn't duplicated
if(
$instance['data_source_type'] == 'wp-post' &&
in_array($post_before->post_name, explode(',', $instance['data_source']))
) {
$instance['data_source'] = str_replace($post_before->post_name, $post_after->post_name, $instance['data_source']);
$instances[$widget_id]['data_source'] = $instance['data_source'];
}
}
}
update_option('widget_gk_nsp', $instances);
}
}
// EOF