-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpercolate-sync.php
385 lines (335 loc) · 12.7 KB
/
percolate-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
<?php
/**
* @package Percolate_Importer
*/
/*
Plugin Name: WP Percolate Importer
Plugin URI: https://github.com/percolate/wordpress
Description: Percolate integration for Wordpress, which includes the ability to sync posts, media library elements and custom creative templates.
Author: Percolate Industries, Inc.
Version: 4.x-1.3.0
Author URI: http://percolate.com
*/
require_once(__DIR__ . '/api/vendor/autoload.php');
require_once(__DIR__ . '/api/models/percolate-acf-model.php');
require_once(__DIR__ . '/api/models/percolate-metabox-model.php');
require_once(__DIR__ . '/api/models/percolate-messages-model.php');
require_once(__DIR__ . '/api/models/percolate-queue-model.php');
require_once(__DIR__ . '/api/models/percolate-wp-model.php');
require_once(__DIR__ . '/api/models/percolate-wpml-model.php');
require_once(__DIR__ . '/api/models/percolate-post-model.php');
require_once(__DIR__ . '/api/helpers/percolate-helpers.php');
require_once(__DIR__ . '/api/services/percolate-api-service.php');
require_once(__DIR__ . '/api/services/percolate-ajax-service.php');
require_once(__DIR__ . '/api/services/percolate-media-service.php');
require_once(__DIR__ . '/api/services/percolate-importer-service.php');
require_once(__DIR__ . '/api/services/percolate-sync-service.php');
require_once(__DIR__ . '/api/services/percolate-updater-service.php');
require_once(__DIR__ . '/api/services/percolate-log-service.php');
class Percolate_Setup
{
/**
* Class constructor
*/
public function __construct(
Percolate_Log $percolate_Log,
Percolate_Media $Percolate_Media,
Percolate_Importer_Service $percolate_Importer_Service,
Percolate_Sync_Service $percolate_Sync_Service,
Percolate_AJAX_Service $Percolate_AJAX_Service
) {
$this->Post = $percolate_Importer_Service;
// GitHub updater
new Percolate_GitHubPluginUpdater( __FILE__, 'percolate', 'wordpress' );
// WP Plugin methods
register_activation_hook(__FILE__, array($this, '__activation'));
register_deactivation_hook(__FILE__, array($this, '__deactivation'));
// Add settings page
add_action('admin_menu', array($this, 'register_settings_page'));
// Add admin scripts
add_action('admin_enqueue_scripts', array( $this, 'addAdminScripts' ));
// Add Angular's tags to header
add_action('wp_head', array( $this, 'setupHeader' ));
// Add custom Cron schedules
add_filter('cron_schedules', array( $this, 'cron_update_schedules' ));
if(!get_option('PercV4hasAcceptMessage') ) {
if(empty($_GET['hidePercV4Message'])) {
function show_plugin_alert_activation_percolate()
{
$url = $_SERVER['REQUEST_URI'];
$query = parse_url($url, PHP_URL_QUERY);
if ($query) {
$url .= '&hidePercV4Message=1';
} else {
$url .= '?hidePercV4Message=1';
}
?>
<div class="error notice">
<p>Percolate plugin has been updated to new version. You have to map <strong>all of taxonomy
AGAIN!</strong></p>
<p><a href="<?php echo $url; ?>">dismiss</a></p>
</div>
<?php
}
add_action('admin_notices', 'show_plugin_alert_activation_percolate');
} else {
function dismiss_notice_percov4()
{
if (isset($_GET['hidePercV4Message']))
update_option('PercV4hasAcceptMessage', 1);
}
add_action('admin_init', 'dismiss_notice_percov4');
}
}
}
/**
* Plugin activation logic
*/
public function __activation() {
Percolate_Log::log('Percolate Importer plugin activated.');
// Activate the WP Cron task for importing posts
$this->Post->activateCron();
update_option('PercV4hasAcceptMessage', 1);
if($this->check_old_taxonomy()) {
update_option('PercV4hasAcceptMessage', 0);
}
}
/**
* Plugin activation logic
*/
public function __deactivation() {
Percolate_Log::log('Percolate Importer plugin deactivated.');
// Dectivate the WP Cron task for importing posts
$this->Post->deactivateCron();
}
/**
* Settings page registration logic
*/
public function register_settings_page () {
if (current_user_can('manage_options')) { // admin management options
add_menu_page(
'Percolate WordPress Importer',
'Percolate',
'administrator', // or 'manage_options'
'percolate-settings',
array($this, 'renderSettings'),
plugin_dir_url( __FILE__ ) . '/frontend/images/percolate-icon.png',
81
);
}
}
public function renderSettings () {
include_once(__DIR__ . '/frontend/views/settings/index.php');
}
/**
* Check taxonomy version of plugin
*/
public function check_old_taxonomy() {
$option = json_decode( $this->Post->Wp->getData() );
$showAlert = false;
//in case of old version (taxonomy fixing)
foreach($option->channels as $channel) {
if(isset($channel->taxonomyMapping)) {
$newTaxonomyMapping = [];
foreach($channel->taxonomyMapping as $taxonomy) {
if(isset($taxonomy->taxonomyPercoKey)) {
array_push($newTaxonomyMapping, $taxonomy);
} else {
$showAlert = true;
}
}
if(count($newTaxonomyMapping) > 0) {
$channel->taxonomyMapping = $newTaxonomyMapping;
} else {
unset($channel->taxonomyMapping);
}
}
}
if($showAlert) {
wp_cache_delete ( 'alloptions', 'options' );
$success = update_option( 'PercV4Opt', json_encode($option) );
if($success) {
Percolate_Log::log('Percolate database successfully update taxonomy.');
} else {
Percolate_Log::log('Error - Percolate plugin cant update database!');
}
}
return $showAlert;
}
/**
* Register all scripts for admin page
*/
public function addAdminScripts () {
$scripts = array();
$scripts[] = array(
'handle' => 'underscore',
'src' => plugins_url( '/frontend/vendor/underscore-1.8.3/underscore-min.js', __FILE__ ),
'deps' => null,
'version' => '1.8.3',
'footer' => true
);
$scripts[] = array(
'handle' => 'velocity',
'src' => plugins_url( '/frontend/vendor/velocity-1.2.3/velocity.min.js', __FILE__ ),
'deps' => array('jquery'),
'version' => '1.2.3',
'footer' => true
);
$scripts[] = array(
'handle' => 'bootstrap',
'src' => plugins_url( '/frontend/vendor/bootstrap-sass-3.3.6/assets/javascripts/bootstrap.min.js', __FILE__ ),
'deps' => array('jquery'),
'version' => '3.3.6',
'footer' => true
);
$scripts[] = array(
'handle' => 'angular',
'src' => plugins_url( '/frontend/vendor/angular-1.4.8/angular.min.js', __FILE__ ),
'deps' => array('jquery'),
'version' => '1.4.8',
'footer' => true
);
$scripts[] = array(
'handle' => 'ngAnimate',
'src' => plugins_url( '/frontend/vendor/angular-1.4.8/angular-animate.min.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1.4.8',
'footer' => true
);
$scripts[] = array(
'handle' => 'ui-router',
'src' => plugins_url( '/frontend/vendor/ui-router-0.2.15/angular-ui-router.min.js', __FILE__ ),
'deps' => array('angular'),
'version' => '0.2.15',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-App',
'src' => plugins_url( '/frontend/scripts/settings/app.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-Api',
'src' => plugins_url( '/frontend/scripts/api/api.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-Percolate',
'src' => plugins_url( '/frontend/scripts/api/percolate.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-UuidSrv',
'src' => plugins_url( '/frontend/scripts/settings/services/uuid.service.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-PaginationSrv',
'src' => plugins_url( '/frontend/scripts/settings/services/pagination.service.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-MainCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/main.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-IndexCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/index.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-AddCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/add.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-AddSetupCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/add.setup.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-AddTopicsCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/add.topics.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-AddTemplatesCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/add.templates.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-SettingsCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/settings.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-LogCtr',
'src' => plugins_url( '/frontend/scripts/settings/controllers/log.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
$scripts[] = array(
'handle' => 'PerolcateWP-LoaderDir',
'src' => plugins_url( '/frontend/scripts/settings/directives/loader.js', __FILE__ ),
'deps' => array('angular'),
'version' => '1',
'footer' => true
);
if ( is_admin() ) {
// Only load scripts and styles in the admin
foreach( $scripts as $script ) {
wp_enqueue_script( $script['handle'], $script['src'], $script['deps'], $script['version'], $script['footer']);
}
wp_localize_script( 'PerolcateWP-Api', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
wp_localize_script( 'PerolcateWP-Percolate', 'ajax_object',
array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );
// ---------
// Styles
wp_enqueue_style( 'percolate-styles', plugins_url( '/frontend/styles/css/percolate-settings.css', __FILE__ ), null, '1', 'all' );
}
}
public function setupHeader()
{
echo '<base href="/">';
}
public function cron_update_schedules()
{
return array(
'every_30_min' => array('interval' => 1800, 'display' => 'Once in 30 minutes'),
'every_15_min' => array('interval' => 900, 'display' => 'Once in 15 minutes'),
'every_5_min' => array('interval' => 300, 'display' => 'Once in 5 minutes'),
'every_min' => array('interval' => 60, 'display' => 'In every minute')
);
}
}
// Bootstrap the plugin with PHP-DI
$container = DI\ContainerBuilder::buildDevContainer();
$container->get('Percolate_Setup');
?>