-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathms-taxonomies-sync.php
640 lines (504 loc) · 15.4 KB
/
ms-taxonomies-sync.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
<?php
/**
* Plugin Name: Multisite Taxonomies Sync
* Plugin URI: http://www.htmline.com/
* Description: Extends WordPress Multisite platforms with taxonomies sync capabilities
* Version: 1.0.0
* Author: Nir Goldberg
* Author URI: http://www.htmline.com/
* License: GPLv3
* Text Domain: mstaxsync
* Domain Path: /lang
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
if ( ! class_exists( 'MSTaxSync' ) ) :
class MSTaxSync {
/**
* Plugin version
*
* @var (string)
*/
private $version;
/**
* Required plugins must be active for MSTaxSync
*
* @var (array)
*/
private $required_plugins;
/**
* __construct
*
* A dummy constructor to ensure MSTaxSync is only initialized once
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function __construct() {
$this->version = '1.0.0';
$this->required_plugins = array();
/* Do nothing here */
}
/**
* initialize
*
* The real constructor to initialize MSTaxSync
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function initialize() {
// vars
$basename = plugin_basename( __FILE__ );
$path = plugin_dir_path( __FILE__ );
$url = plugin_dir_url( __FILE__ );
$slug = dirname( $basename );
// settings
$this->settings = array(
// basic
'name' => __( 'Multisite Taxonomies Sync', 'mstaxsync' ),
'version' => $this->version,
// urls
'basename' => $basename,
'path' => $path, // with trailing slash
'url' => $url, // with trailing slash
'slug' => $slug,
// options
'show_admin' => true,
'capability' => 'manage_options',
'debug' => false,
);
if ( ! $this->check_multisite() )
return;
if ( ! $this->check_required_plugins() )
return;
// constants
$this->define( 'MSTaxSync', true );
$this->define( 'MSTaxSync_VERSION', $this->version );
$this->define( 'MSTaxSync_PATH', $path );
// helpers
include_once( MSTaxSync_PATH . 'includes/api/api-helpers.php' );
// api
mstaxsync_include( 'includes/api/api-taxonomy-terms.php' );
mstaxsync_include( 'includes/api/api-posts.php' );
mstaxsync_include( 'includes/api/api-broadcast.php' );
mstaxsync_include( 'includes/api/api-import.php' );
mstaxsync_include( 'includes/api/api-resync.php' );
// classes
mstaxsync_include( 'includes/classes/class-mstaxsync-core.php' );
mstaxsync_include( 'includes/classes/class-mstaxsync-attachment.php' );
mstaxsync_include( 'includes/classes/class-mstaxsync-broadcast.php' );
mstaxsync_include( 'includes/classes/class-mstaxsync-import.php' );
// actions
add_action( 'init', array( $this, 'init' ), 99 );
add_action( 'init', array( $this, 'register_assets' ), 99 );
// admin
if ( is_admin() ) {
// actions
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue_scripts') );
}
// plugin activation / deactivation
register_activation_hook ( __FILE__, array( $this, 'mstaxsync_activate' ) );
register_deactivation_hook ( __FILE__, array( $this, 'mstaxsync_deactivate' ) );
}
/**
* init
*
* This function will run after all plugins and theme functions have been included
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function init() {
// exit if called too early
if ( ! did_action( 'plugins_loaded' ) )
return;
// exit if already init
if ( mstaxsync_get_setting( 'init' ) )
return;
// only run once
mstaxsync_update_setting( 'init', true );
// update url - allow another plugin to modify dir
mstaxsync_update_setting( 'url', plugin_dir_url( __FILE__ ) );
// set textdomain
$this->load_plugin_textdomain();
// admin
if ( is_admin() ) {
mstaxsync_include( 'includes/admin/settings-api.php' );
mstaxsync_include( 'includes/admin/class-admin.php' );
mstaxsync_include( 'includes/admin/class-admin-page.php' );
mstaxsync_include( 'includes/admin/class-admin-dashboard.php' );
mstaxsync_include( 'includes/admin/class-admin-taxonomies.php' );
mstaxsync_include( 'includes/admin/class-admin-settings-page.php' );
mstaxsync_include( 'includes/admin/class-admin-settings.php' );
mstaxsync_include( 'includes/admin/class-admin-tools.php' );
}
// action for 3rd party
do_action( 'mstaxsync/init' );
}
/**
* register_assets
*
* This function will register scripts and styles
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function register_assets() {
// append styles
$styles = array(
'mstaxsync-admin' => array(
'src' => mstaxsync_get_url( 'assets/css/mstaxsync-admin-style.css' ),
'deps' => false,
),
'mstaxsync-admin-rtl' => array(
'src' => mstaxsync_get_url( 'assets/css/mstaxsync-admin-style-rtl.css' ),
'deps' => array( 'mstaxsync-admin' ),
),
'jquery-ui' => array(
'src' => '//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css',
'deps' => false,
),
);
// append scripts
$scripts = array(
'jquery-ui' => array(
'src' => 'https://code.jquery.com/ui/1.12.1/jquery-ui.js',
'deps' => array( 'jquery' ),
),
'nestedSortable' => array(
'src' => mstaxsync_get_url( 'assets/js/lib/jquery.mjs.nestedSortable.js' ),
'deps' => array( 'jquery-ui' ),
),
'mstaxsync' => array(
'src' => mstaxsync_get_url( 'assets/js/min/mstaxsync.min.js' ),
'deps' => array( 'jquery-ui' ),
),
);
// register styles
foreach( $styles as $handle => $style ) {
wp_register_style( $handle, $style[ 'src' ], $style[ 'deps' ], MSTaxSync_VERSION );
}
// register scripts
foreach( $scripts as $handle => $script ) {
wp_register_script( $handle, $script[ 'src' ], $script[ 'deps' ], MSTaxSync_VERSION, true );
}
}
/**
* admin_enqueue_scripts
*
* This function will enque admin scripts and styles
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function admin_enqueue_scripts() {
// enqueue styles
wp_enqueue_style( 'mstaxsync-admin' );
wp_enqueue_style( 'jquery-ui' );
// rtl
if ( is_rtl() ) {
wp_enqueue_style( 'mstaxsync-admin-rtl' );
}
// enqueue scripts
wp_enqueue_script( 'jquery-ui' );
wp_enqueue_script( 'nestedSortable' );
// localize mstaxsync
$translation_arr = array(
'settings' => array(
'advanced_treeview' => get_option( 'mstaxsync_advanced_treeview', array( 'can' ) ),
'edit_terms' => get_option( 'mstaxsync_edit_taxonomy_terms', array( 'can' ) ),
'detach_terms' => get_option( 'mstaxsync_detach_taxonomy_terms' ),
'delete_terms' => get_option( 'mstaxsync_delete_taxonomy_terms' ),
'import_posts' => get_option( 'mstaxsync_import_taxonomy_terms_posts', array( 'can' ) ),
'resync_posts' => get_option( 'mstaxsync_resync_posts', array( 'can' ) ),
),
'strings' => array(
'relationship_new_item_str' => __( 'New item', 'mstaxsync' ),
'relationship_changed_item_str' => __( 'Changed', 'mstaxsync' ),
'relationship_success_str' => __( 'All terms were updated successfully.', 'mstaxsync' ),
'relationship_main_terms_str' => __( 'new terms were synced.', 'mstaxsync' ),
'relationship_local_terms_str' => __( 'terms were updated.', 'mstaxsync' ),
'relationship_errors_str' => __( 'The following errors were found:', 'mstaxsync' ),
'relationship_error_str' => __( 'Error', 'mstaxsync' ),
'relationship_internal_server_error_str' => __( 'Internal server error', 'mstaxsync' ),
'confirm_detach' => __( 'You are about to permanently detach this taxonomy term.', 'mstaxsync' ) . "\n" . __( 'This action cannot be undone.', 'mstaxsync' ) . "\n" . __( "'Cancel' to stop, 'OK' to detach.", 'mstaxsync' ),
'confirm_delete' => __( 'You are about to permanently delete this taxonomy term.', 'mstaxsync' ) . "\n" . __( 'This action cannot be undone.', 'mstaxsync' ) . "\n" . __( "'Cancel' to stop, 'OK' to delete.", 'mstaxsync' ),
'confirm_import' => __( 'You are about to import %s posts.', 'mstaxsync' ) . "\n" . __( 'This action cannot be undone.', 'mstaxsync' ) . "\n" . __( "'Cancel' to stop, 'OK' to import.", 'mstaxsync' ),
'confirm_single_import' => __( 'You are about to import one post.', 'mstaxsync' ) . "\n" . __( 'This action cannot be undone.', 'mstaxsync' ) . "\n" . __( "'Cancel' to stop, 'OK' to import.", 'mstaxsync' ),
'success_import' => __( 'Number of posts imported successfully: ', 'mstaxsync' ),
'timeout_import' => __( 'Timeout occurred in import process', 'mstaxsync' ),
'failed_import' => __( 'Import process has been failed', 'mstaxsync' ),
'confirm_resync' => __( 'You are about to reassign current synced category and taxonomy terms for all synced posts.', 'mstaxsync' ) . "\n" . __( 'Be aware that terms which have been manually removed after last import might be reassigned.', 'mstaxsync' ) . "\n" . __( 'This action cannot be undone.', 'mstaxsync' ) . "\n" . __( "'Cancel' to stop, 'OK' to resync.", 'mstaxsync' ),
'synced_posts_found' => __( 'Found %s synced posts.', 'mstaxsync' ),
'total_posts_resynced' => __( '%s posts resynced successfully', 'mstaxsync' ),
'no_synced_posts' => __( 'There are no synced posts', 'mstaxsync' ),
'failed_resync' => __( 'Resync process has been failed', 'mstaxsync' ),
),
'ajaxurl' => admin_url( 'admin-ajax.php' ),
);
wp_localize_script( 'mstaxsync', '_mstaxsync', $translation_arr );
// Enqueued script with localized data.
wp_enqueue_script( 'mstaxsync' );
}
/**
* define
*
* This function will safely define a constant
*
* @since 1.0.0
* @param $name (string)
* @param $value (string)
* @return N/A
*/
public function define( $name, $value = true ) {
if ( ! defined( $name ) ) {
define( $name, $value );
}
}
/**
* load_plugin_textdomain
*
* This function will load the textdomain file
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
private function load_plugin_textdomain() {
// vars
$domain = 'mstaxsync';
$locale = apply_filters( 'plugin_locale', mstaxsync_get_locale(), $domain );
$mofile = $domain . '-' . $locale . '.mo';
// load from the languages directory first
load_textdomain( $domain, WP_LANG_DIR . '/plugins/' . $mofile );
// load from plugin lang folder
load_textdomain( $domain, mstaxsync_get_path( 'lang/' . $mofile ) );
}
/**
* has_setting
*
* This function will return true if has setting
*
* @since 1.0.0
* @param $name (string)
* @return (boolean)
*/
public function has_setting( $name ) {
// return
return isset( $this->settings[ $name ] );
}
/**
* get_setting
*
* This function will return a setting value
*
* @since 1.0.0
* @param $name (string)
* @return (mixed)
*/
public function get_setting( $name ) {
// return
return isset( $this->settings[ $name ] ) ? $this->settings[ $name ] : null;
}
/**
* update_setting
*
* This function will update a setting value
*
* @since 1.0.0
* @param $name (string)
* @param $value (mixed)
* @return N/A
*/
public function update_setting( $name, $value ) {
$this->settings[ $name ] = $value;
// return
return true;
}
/**
* mstaxsync_activate
*
* Actions perform on activation of plugin
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function mstaxsync_activate() {
// taxonomy terms core
mstaxsync_include( 'includes/classes/class-mstaxsync-tt-core.php' );
}
/**
* mstaxsync_deactivate
*
* Actions perform on deactivation of plugin
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
public function mstaxsync_deactivate() {}
/**
* check_multisite
*
* This function will check if multisite support is enabled
*
* @since 1.0.0
* @param N/A
* @return (boolean)
*/
private function check_multisite() {
// vars
$basename = $this->settings[ 'basename' ];
if ( ! is_multisite() ) {
if ( is_plugin_active( $basename ) ) {
deactivate_plugins( $basename );
add_action( 'admin_notices', array( $this, 'admin_multisite_notices_error' ) );
if ( isset( $_GET[ 'activate' ] ) ) {
unset( $_GET[ 'activate' ] );
}
}
// return
return false;
}
// return
return true;
}
/**
* check_required_plugins
*
* This function will check if required plugins are activated.
* A backup sanity check, in case the plugin is activated in a weird way,
* or one of required plugins has been deactivated
*
* @since 1.0.0
* @param N/A
* @return (boolean)
*/
private function check_required_plugins() {
// vars
$basename = $this->settings[ 'basename' ];
if ( ! $this->has_required_plugins() ) {
if ( is_plugin_active( $basename ) ) {
deactivate_plugins( $basename );
add_action( 'admin_notices', array( $this, 'admin_required_plugins_notices_error' ) );
if ( isset( $_GET[ 'activate' ] ) ) {
unset( $_GET[ 'activate' ] );
}
}
// return
return false;
}
// return
return true;
}
/**
* has_required_plugins
*
* This function will check if required plugins are activated
*
* @since 1.0.0
* @param N/A
* @return (boolean)
*/
private function has_required_plugins() {
// vars
$required = $this->required_plugins;
if ( empty( $required ) )
// return
return true;
$active_plugins = (array) get_option( 'active_plugins', array() );
if ( is_multisite() ) {
$active_plugins = array_merge( $active_plugins, get_site_option( 'active_sitewide_plugins', array() ) );
}
foreach ( $required as $key => $plugin ) {
$plugin = ( ! is_numeric( $key ) ) ? "{$key}/{$plugin}.php" : "{$plugin}/{$plugin}.php";
if ( ! in_array( $plugin, $active_plugins ) && ! array_key_exists( $plugin, $active_plugins ) )
// return
return false;
}
// return
return true;
}
/**
* admin_multisite_notices_error
*
* This function will add admin error notice
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
private function admin_multisite_notices_error() {
// vars
$msg = sprintf( __( "<strong>%s</strong> plugin can't be activated.<br />Multisite support must be enabled.", 'mstaxsync' ), $this->settings[ 'name' ] );
$this->admin_notices_error( $msg );
}
/**
* admin_required_plugins_notices_error
*
* This function will add admin error notice
*
* @since 1.0.0
* @param N/A
* @return N/A
*/
private function admin_required_plugins_notices_error() {
// vars
$required = $this->required_plugins;
$msg = sprintf( __( "<strong>%s</strong> plugin can't be activated.<br />The following plugins should be installed and activated first:<br />", 'mstaxsync' ), $this->settings[ 'name' ] );
foreach ( $required as $key => $plugin ) {
$path = ( ! is_numeric( $key ) ) ? "{$key}/{$plugin}.php" : "{$plugin}/{$plugin}.php";
if ( file_exists( plugin_dir_path( __DIR__ ) . $path ) ) {
$name = get_plugin_data( plugin_dir_path( __DIR__ ) . $path )[ 'Name' ];
}
else {
$name = $plugin;
}
$msg .= "<br />• {$name}";
}
$this->admin_notices_error( $msg );
}
/**
* admin_notices_error
*
* This function will display admin error notice
*
* @since 1.0.0
* @param $msg (string)
* @return N/A
*/
private function admin_notices_error( $msg ) {
// vars
$class = 'notice notice-error is-dismissible';
printf( '<div class="%1$s"><p>%2$s</p></div>', $class, $msg );
}
}
/**
* mstaxsync
*
* The main function responsible for returning the one true mstaxsync instance
*
* @since 1.0.0
* @param N/A
* @return (object)
*/
function mstaxsync() {
// globals
global $mstaxsync;
// initialize
if( ! isset( $mstaxsync ) ) {
$mstaxsync = new MSTaxSync();
$mstaxsync->initialize();
}
// return
return $mstaxsync;
}
// initialize
mstaxsync();
endif; // class_exists check