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
|
<?php
namespace TotalPoll; ! defined( 'ABSPATH' ) && exit();
use TotalPollVendors\TotalCore\Helpers\Misc;
/** * TotalPoll Plugin. * @package TotalPoll */ class Plugin extends \TotalPollVendors\TotalCore\Foundation\Plugin {
public function registerProviders() { // Bootstrap $this->container->share( 'bootstrap', function () { return new Bootstrap(); } );
// Custom implementation of modules repository $this->container->share( 'modules.repository', function () { return new Modules\Repository( $this->container->get( 'env' ), $this->container->get( 'admin.activation' ), $this->container->get( 'admin.account' ) ); } );
// Poll post type $this->container->share( 'polls.cpt', function () { return new Poll\PostType(); } );
// Poll post defaults $this->container->add( 'polls.defaults', function () { return [ 'id' => get_the_ID(), 'questions' => [], 'fields' => [], 'vote' => [ 'limitations' => [ 'region' => [ 'rules' => [], ], ], 'frequency' => [ 'cookies' => [ 'enabled' => true ], 'ip' => [ 'enabled' => false ], 'user' => [ 'enabled' => false ], 'perSession' => 1, 'perUser' => 1, 'perIP' => 1, 'timeout' => 3600, ], ], 'choices' => [ 'sort' => [ 'field' => 'position', 'direction' => 'ASC', ], ], 'results' => [ 'sort' => [ 'field' => 'votes', 'direction' => 'DESC', ], 'visibility' => 'all', 'untilReaching' => null, 'format' => '{{votesPercentage}}', ], 'design' => [ 'template' => 'basic-template', 'text' => [ 'fontFamily' => 'inherit', 'fontWeight' => 'inherit', 'fontSize' => 'inherit', 'lineHeight' => 'inherit', 'align' => 'inherit', 'transform' => 'none', ], 'colors' => [ 'primary' => '#2196f3', 'primaryContrast' => '#ffffff', 'primaryLighter' => '#64b5f6', 'primaryLight' => '#42a5f5', 'primaryDark' => '#1e88e5', 'primaryDarker' => '#1976d2', 'secondary' => '#4caf50', 'secondaryContrast' => '#ffffff', 'secondaryLighter' => '#a5d6a7', 'secondaryLight' => '#81c784', 'secondaryDark' => '#43a047', 'secondaryDarker' => '#388e3c', 'accent' => '#ffc107', 'accentContrast' => '#ffffff', 'accentLighter' => '#ffd54f', 'accentLight' => '#ffca28', 'accentDark' => '#ffb300', 'accentDarker' => '#ffa000', 'dark' => '#333333', 'gray' => '#dddddd', 'grayContrast' => '#333333', 'grayLighter' => '#fafafa', 'grayLight' => '#eeeeee', 'grayDark' => '#aaaaaa', 'grayDarker' => '#999999', ], 'layout' => [ 'choicesPerRow' => 1, 'questionsPerRow' => 1, 'maxWidth' => '100%', 'gutter' => '1em', 'radius' => '0', ], 'custom' => null, 'behaviours' => [ 'ajax' => true, 'scrollUp' => true, ], 'effects' => [ 'transition' => 'fade', 'duration' => '500', ], ], 'notifications' => [ 'email' => [ 'recipient' => (string) get_option( 'admin_email' ), 'on' => [ 'newVote' => false ] ], ], 'meta' => [ 'schema' => '1.0', ], ]; } );
// Poll shortcode $this->container->add( 'polls.shortcode', function ( $attributes, $content = null ) { return new Shortcodes\Poll( $attributes, $content ); } );
// Poll form $this->container->add( 'polls.form', function ( $poll ) { return new Poll\Form( $this->container->get( 'http.request' ), $this->container->get( 'form.factory' ), $poll ); } );
// Poll repository $this->container->share( 'polls.repository', function () { return new Poll\Repository( $this->container->get( 'http.request' ), $this->container->get( 'database' ), $this->container->get( 'env' ) ); } );
// Poll controller $this->container->share( 'poll.controller', function () { return new Poll\Controller( $this->container->get( 'http.request' ), $this->container->get( 'polls.repository' ) ); } );
// Poll render $this->container->add( 'polls.renderer', function ( $poll ) { return new Poll\Renderer( $poll, $this->container->get( 'modules.repository' ), $this->container->get( 'filesystem' ), $this->container->get( 'env' ) ); } );
// Poll commands - count vote $this->container->add( 'polls.commands.vote.count', function ( $poll ) { return new Poll\Commands\CountVote( $poll, $this->container->get( 'http.request' ), $this->container->get( 'polls.repository' ) ); } );
// Poll commands - log vote $this->container->add( 'polls.commands.vote.log', function ( $poll ) { return new Poll\Commands\LogVote( $poll, $this->container->get( 'log.repository' ) ); } );
// Poll commands - save entry $this->container->add( 'polls.commands.vote.entry', function ( $poll ) { return new Poll\Commands\SaveEntry( $poll, $this->container->get( 'entries.repository' ) ); } );
// Poll commands - notify $this->container->add( 'polls.commands.vote.notify', function ( $poll ) { return new Poll\Commands\SendNotifications( $poll ); } );
// Log repository $this->container->share( 'log.repository', function () { return new Log\Repository( $this->container->get( 'http.request' ), $this->container->get( 'database' ), $this->container->get( 'env' ) ); } );
// Entry repository $this->container->share( 'entries.repository', function () { return new Entry\Repository( $this->container->get( 'http.request' ), $this->container->get( 'database' ), $this->container->get( 'env' ) ); } );
// Admin bootstrap $this->container->share( 'admin.bootstrap', function () { return new Admin\Bootstrap( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Admin ajax bootstrap $this->container->share( 'admin.ajax', function () { return new Admin\Ajax\Bootstrap(); } );
// Admin ajax (dashboard) $this->container->share( 'admin.ajax.dashboard', function () { return new Admin\Ajax\Dashboard( $this->container->get( 'http.request' ), $this->container->get( 'admin.activation' ), $this->container->get( 'admin.account' ), $this->container->get( 'polls.repository' ), $this->container->get( 'entries.repository' ) ); } );
// Admin ajax (log) $this->container->share( 'admin.ajax.log', function () { return new Admin\Ajax\Log( $this->container->get( 'http.request' ), $this->container->get( 'log.repository' ) ); } );
// Admin ajax (entries) $this->container->share( 'admin.ajax.entries', function () { return new Admin\Ajax\Entries( $this->container->get( 'http.request' ), $this->container->get( 'entries.repository' ) ); } );
// Admin ajax (insights) $this->container->share( 'admin.ajax.insights', function () { return new Admin\Ajax\Insights( $this->container->get( 'http.request' ), $this->container->get( 'log.repository' ), $this->container->get( 'polls.repository' ) ); } );
// Admin ajax (modules) $this->container->share( 'admin.ajax.modules', function () { return new Admin\Ajax\Modules( $this->container->get( 'http.request' ), $this->container->get( 'modules.manager' ) ); } );
// Admin ajax (options) $this->container->share( 'admin.ajax.options', function () { return new Admin\Ajax\Options( $this->container->get( 'http.request' ), $this->container->get( 'migrations.migrators' ) ); } );
// Admin ajax (polls) $this->container->share( 'admin.ajax.polls', function () { return new Admin\Ajax\Polls( $this->container->get( 'http.request' ) ); } );
// Admin ajax (templates) $this->container->share( 'admin.ajax.templates', function () { return new Admin\Ajax\Templates( $this->container->get( 'http.request' ), $this->container->get( 'modules.repository' ) ); } );
// Poll editor $this->container->share( 'admin.poll.editor', function () { return new Admin\Poll\Editor( $this->container->get( 'env' ), $this->container->get( 'filesystem' ), $this->container->get( 'polls.repository' ), $this->container->get( 'modules.repository' ) ); } );
// Poll listing $this->container->share( 'admin.poll.listing', function () { return new Admin\Poll\Listing( $this->container->get( 'polls.repository' ), $this->container->get( 'entries.repository' ), $this->container->get( 'log.repository' ) ); } );
// Entries $this->container->share( 'admin.pages.entries', function () { return new Admin\Entries\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Insights $this->container->share( 'admin.pages.insights', function () { return new Admin\Insights\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Log $this->container->share( 'admin.pages.log', function () { return new Admin\Log\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Modules $this->container->share( 'admin.pages.modules', function () { return new Admin\Modules\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Templates $this->container->share( 'admin.pages.templates', function () { return new Admin\Modules\Templates\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Extensions $this->container->share( 'admin.pages.extensions', function () { return new Admin\Modules\Extensions\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ) ); } );
// Options $this->container->share( 'admin.pages.options', function () { return new Admin\Options\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ), $this->container->get( 'migrations.migrators' ) ); } );
// Dashboard $this->container->share( 'admin.pages.dashboard', function () { return new Admin\Dashboard\Page( $this->container->get( 'http.request' ), $this->container->get( 'env' ), $this->container->get( 'admin.activation' ), $this->container->get( 'admin.account' ) ); } );
$this->container->share( 'admin.privacy', function () { return new Admin\Privacy\Policy( $this->container->get( 'env' ), $this->container->get( 'entries.repository' ), $this->container->get( 'log.repository' ) ); } );
// Schema migration $this->container->share( 'migrations.schema', function () { require_once ABSPATH . 'wp-admin/includes/upgrade.php';
return new Migrations\Schema\Migrator( $this->container->get( 'env' ), $this->container->get( 'database' ) ); } );
// Polls migration $this->container->share( 'migrations.totalpoll', function () { return new Migrations\Polls\TotalPoll\Migrator( $this->container->get( 'env' ) ); } );
$this->container->share( 'migrations.yop', function () { return new Migrations\Polls\YOP\Migrator( $this->container->get( 'env' ) ); } );
$this->container->share( 'migrations.wp-polls', function () { return new Migrations\Polls\WPPolls\Migrator( $this->container->get( 'env' ) ); } );
$this->container->share( 'migrations.migrators', function () { return [ 'totalpoll-3' => $this->container->get( 'migrations.totalpoll' ), 'yop' => $this->container->get( 'migrations.yop' ), 'wp-polls' => $this->container->get( 'migrations.wp-polls' ), ]; } );
$this->container->share( 'decorators.structuredData', function () { return new Decorators\StructuredData(); } );
$this->container->add( 'utils.create.cache', function () { wp_mkdir_p( $this->application->env( 'cache.path' ) . 'css/' ); } );
$this->container->add( 'utils.purge.cache', function () { $this->container->get( 'filesystem' )->rmdir( $this->application->env( 'cache.path' ), true ); } );
$this->container->add( 'utils.purge.store', function () { delete_transient( $this->application->env( 'slug' ) . '_modules_store_response' ); } );
}
public function registerWidgets() { register_widget( '\TotalPoll\Widgets\Poll' ); register_widget( '\TotalPoll\Widgets\LatestPoll' ); }
public function registerShortCodes() { $callback = function ( $attributes, $content = null ) { return (string) $this->container->get( 'polls.shortcode', [ $attributes, $content ] ); }; add_shortcode( 'tp-poll', $callback ); add_shortcode( 'totalpoll', $callback ); }
public function registerCustomPostTypes() { $this->container->get( 'polls.cpt' ); }
public function registerTaxonomies() {
}
public function loadTextDomain() { $locale = get_locale(); $localeFallback = substr( $locale, 0, 2 ); $mofile = "totalpoll-{$locale}.mo"; $mofileFallback = "totalpoll-{$localeFallback}.mo"; $path = $this->application->env( 'path' );
$loaded = load_textdomain( 'totalpoll', "{$path}languages/{$mofile}" ); if ( ! $loaded ): $loaded = load_textdomain( 'totalpoll', "{$path}languages/{$mofileFallback}" ); endif;
if ( ! is_admin() || Misc::isDoingAjax() ): // Customized expressions $expressions = (array) $this->application->option( 'expressions', [] );
if ( ! empty( $expressions ) ): if ( isset( $GLOBALS['l10n']['totalpoll'] ) ): $domain = $GLOBALS['l10n']['totalpoll']; else: $domain = $GLOBALS['l10n']['totalpoll'] = new \MO(); endif;
foreach ( $expressions as $expression => $expressionContent ): if ( empty( $expressionContent['translations'] ) || empty( $expressionContent['translations'][0] ) ): continue; endif; if ( empty( $domain->entries[ $expression ] ) ): $entry = new \Translation_Entry( [ 'singular' => $expression, 'translations' => $expressionContent['translations'], ] ); $domain->add_entry( $entry ); else: $domain->entries[ $expression ]->translations = $expressionContent['translations']; endif; endforeach; endif; endif; }
public function onActivation() { // Migrate the database $this->container->get( 'migrations.schema' )->migrate();
// Register post types & flush rewrite rules $this->registerCustomPostTypes();
// Purge previous cache $this->container->get( 'utils.purge.cache' ); $this->container->get( 'utils.purge.store' );
// Create cache directories $this->container->get( 'utils.create.cache' );
// Reactivate current license, if any $this->container->get( 'admin.activation' )->reactivateLicense();
// Trigger action do_action( 'totalpoll/actions/activated' ); }
public function onDeactivation() { // Flush rewrite rules flush_rewrite_rules();
// Flush cache wp_cache_flush();
// Trigger action do_action( 'totalpoll/actions/deactivated' ); }
public static function onUninstall() { // Flush rewrite rules flush_rewrite_rules();
$userConsent = TotalPoll()->option( 'advanced.uninstallAll' ); if ( $userConsent ): // Delete tables $tables = TotalPoll()->env( 'db.tables' ); foreach ( $tables as $table ): $query = "DROP TABLE IF EXISTS {$table}"; TotalPoll( 'database' )->query( $query ); endforeach; // Delete polls $query = new \WP_Query( [ 'post_type' => 'poll', 'post_status' => 'any', 'fields' => 'ids' ] ); $polls = $query->get_posts(); foreach ( $polls as $id ) { wp_delete_post( $id, true ); } // Delete files TotalPoll( 'utils.purge.cache' ); TotalPoll( 'utils.purge.store' ); // Delete options TotalPoll( 'options' )->deleteOptions(); endif;
// Trigger action do_action( 'totalpoll/actions/uninstalled', $userConsent ); }
/** * Bootstrap plugin. */ public function bootstrap() { /** * Fires before bootstrapping TotalPoll. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 3 */ do_action( 'totalpoll/actions/before/bootstrap', $this );
$this->container->get( 'bootstrap' );
/** * Fires after bootstrapping TotalPoll. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 5 */ do_action( 'totalpoll/actions/after/bootstrap', $this ); }
/** * Bootstrap AJAX. */ public function bootstrapAjax() { /** * Fires before bootstrapping AJAX handler. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 6 */ do_action( 'totalpoll/actions/before/bootstrap-ajax', $this );
$this->container->get( 'admin.ajax' );
/** * Fires after bootstrapping AJAX handler. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 8 */ do_action( 'totalpoll/actions/after/bootstrap-ajax', $this ); }
/** * Bootstrap admin. */ public function bootstrapAdmin() { /** * Fires before bootstrapping admin. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 9 */ do_action( 'totalpoll/actions/before/bootstrap-admin', $this );
$this->container->get( 'admin.bootstrap' );
/** * Fires after bootstrapping admin. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 11 */ do_action( 'totalpoll/actions/after/bootstrap-admin', $this ); }
/** * Bootstrap extensions. * @throws \Exception */ public function bootstrapExtensions() { /** * Fires before bootstrapping extensions. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 1 */ do_action( 'totalpoll/actions/before/bootstrap-extensions', $this );
$activatedExtension = $this->container->get( 'modules.repository' )->getActiveWhere( [ 'type' => 'extension' ] ); /** * Filters the list of activated extensions. * * @param array $activatedExtension Array of extensions information. * * @return array * @since 4.0.0 */ $activatedExtension = apply_filters( 'totalpoll/filters/extensions/activated', $activatedExtension );
// @TODO: Improve error reporting for this part. try { foreach ( $activatedExtension as $extension ): if ( $this->container->get( 'filesystem' )->exists( $extension['dirName'] . '/Extension.php' ) && class_exists( $extension['class'] ) ): ( new $extension['class'] )->run(); else: throw new \RuntimeException( "Please check that \"{$extension['id']}\" extension file uses the correct namespace." ); endif; endforeach; } catch ( \Exception $exception ) { if ( Misc::isDevelopmentMode() ): trigger_error( $exception->getMessage(), E_USER_WARNING ); else: TotalPoll( 'modules.repository' )->setInactive( $extension['id'] ); endif; }
/** * Fires after bootstrapping extensions. * * @param Plugin $this Plugin instance. * * @since 4.0.0 * @order 2 */ do_action( 'totalpoll/actions/after/bootstrap-extensions', $this ); } }
|