module-manager.php
26.5 KB
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
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
<?php
class blcModuleManager {
/* @var blcConfigurationManager */
var $plugin_conf;
var $module_dir = '';
var $_module_cache;
var $_category_cache;
var $_category_cache_active;
var $_virtual_modules = array();
var $loaded;
var $instances;
var $default_active_modules;
/**
* Class "constructor".
*
* @param array $default_active_modules An array of module ids specifying which modules are active by default.
* @return void
*/
function init( $default_active_modules = null ) {
$this->module_dir = realpath( dirname( __FILE__ ) . '/../modules' );
$this->plugin_conf = blc_get_configuration();
$this->default_active_modules = $default_active_modules;
$this->loaded = array();
$this->instances = array();
add_filter( 'extra_plugin_headers', array( &$this, 'inject_module_headers' ) );
}
/**
* Get an instance of the module manager.
*
* @param array|null $default_active_modules
* @return blcModuleManager
*/
//phpcs:ignore WordPress.NamingConventions.ValidFunctionName.MethodNameInvalid
static function getInstance( $default_active_modules = null ) {
static $instance = null;
if ( is_null( $instance ) ) {
$instance = new blcModuleManager();
$instance->init( $default_active_modules );
}
return $instance;
}
/**
* Retrieve a list of all installed BLC modules.
*
* This is essentially a slightly modified copy of get_plugins().
*
* @return array An associative array of module headers indexed by module ID.
*/
function get_modules() {
if ( ! isset( $this->_module_cache ) ) {
//Refresh the module cache.
$relative_path = '/' . plugin_basename( $this->module_dir );
if ( ! function_exists( 'get_plugins' ) ) {
//BUG: Potentional security flaw/bug. plugin.php is not meant to be loaded outside admin panel.
require_once( ABSPATH . 'wp-admin/includes/plugin.php' );
}
$modules = get_plugins( $relative_path );
$this->_module_cache = array();
foreach ( $modules as $module_filename => $module_header ) {
//Figure out the module ID. If not specified, it is equal to module's filename (sans the .php)
if ( ! empty( $module_header['ModuleID'] ) ) {
$module_id = strtolower( trim( $module_header['ModuleID'] ) );
} else {
$module_id = strtolower( basename( $module_filename, '.php' ) );
}
$module_header = $this->normalize_module_header( $module_header, $module_id, $module_filename );
$this->_module_cache[ $module_id ] = $module_header;
}
$this->_category_cache = null;
}
return array_merge( $this->_module_cache, $this->_virtual_modules );
}
/**
* Retrieve modules that match a specific category, or all modules sorted by categories.
*
* If a category ID is specified, this method returns the modules that have the "ModuleCategory:"
* file header set to that value, or an empty array if no modules match that category. The
* return array is indexed by module id :
* [module_id1 => module1_data, module_id1 => module2_data, ...]
*
* If category is omitted, this method returns a list of all categories plus the modules
* they contain. Only categories that have at least one module will be included. The return
* value is an array of arrays, indexed by category ID :
* [category1 => [module1_id => module1_data, module2_id => module2_data, ...], ...]
*
*
* @param string $category Category id, e.g. "parser" or "container". Optional.
* @param bool $markup Apply markup to module headers. Not implemented.
* @param bool $translate Translate module headers. Defaults to false.
* @return array An array of categories or module data.
*/
function get_modules_by_category( $category = '', $markup = false, $translate = false ) {
if ( ! isset( $this->_category_cache ) ) {
$this->_category_cache = $this->sort_into_categories( $this->get_modules() );
}
if ( empty( $category ) ) {
if ( $markup || $translate ) {
//Translate/apply markup to module headers
$processed = array();
foreach ( $this->_category_cache as $category_id => $modules ) {
$processed[ $category_id ] = array();
foreach ( $modules as $module_id => $module_data ) {
if ( $translate ) {
$module_data['Name'] = _x( $module_data['Name'], 'module name', 'broken-link-checker' ); //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
}
$processed[ $category_id ][ $module_id ] = $module_data;
}
}
return $processed;
} else {
return $this->_category_cache;
}
} else {
if ( isset( $this->_category_cache[ $category ] ) ) {
if ( $markup || $translate ) {
//Translate/apply markup to module headers
$processed = array();
foreach ( $this->_category_cache[ $category ] as $module_id => $module_data ) {
if ( $translate ) {
$module_data['Name'] = _x( $module_data['Name'], 'module name', 'broken-link-checker' ); //phpcs:ignore WordPress.WP.I18n.NonSingularStringLiteralText
}
$processed[ $module_id ] = $module_data;
}
return $processed;
} else {
return $this->_category_cache[ $category ];
}
} else {
return array();
}
}
}
/**
* Retrieve active modules that match a specific category, or all active modules sorted by categories.
*
* @see blcModuleManager::get_modules_by_category()
*
* @param string $category Category id. Optional.
* @return array An associative array of categories or module data.
*/
function get_active_by_category( $category = '' ) {
if ( ! isset( $this->_category_cache_active ) ) {
$this->_category_cache_active = $this->sort_into_categories( $this->get_active_modules() );
}
if ( empty( $category ) ) {
return $this->_category_cache_active;
} else {
if ( isset( $this->_category_cache_active[ $category ] ) ) {
return $this->_category_cache_active[ $category ];
} else {
return array();
}
}
}
/**
* Get the module ids of all active modules that belong to a specific category,
* quoted and ready for use in SQL.
*
* @param string $category Category ID. If not specified, a list of all active modules will be returned.
* @return string A comma separated list of single-quoted module ids, e.g. 'module-foo','module-bar','modbaz'
*/
function get_escaped_ids( $category = '' ) {
global $wpdb;
if ( empty( $category ) ) {
$modules = $this->get_active_modules();
} else {
$modules = $this->get_active_by_category( $category );
}
$modules = array_map( 'esc_sql', array_keys( $modules ) );
$modules = "'" . implode( "', '", $modules ) . "'";
return $modules;
}
/**
* Sort a list of modules into categories. Inside each category, modules are sorted by priority (descending).
*
* @access private
*
* @param array $modules
* @return array
*/
function sort_into_categories( $modules ) {
$categories = array();
foreach ( $modules as $module_id => $module_data ) {
$cat = $module_data['ModuleCategory'];
if ( isset( $categories[ $cat ] ) ) {
$categories[ $cat ][ $module_id ] = $module_data;
} else {
$categories[ $cat ] = array( $module_id => $module_data );
}
}
foreach ( $categories as $cat => $cat_modules ) {
uasort( $categories[ $cat ], array( &$this, 'compare_priorities' ) );
}
return $categories;
}
/**
* Callback for sorting modules by priority.
*
* @access private
*
* @param array $a First module header.
* @param array $b Second module header.
* @return int
*/
function compare_priorities( $a, $b ) {
return $b['ModulePriority'] - $a['ModulePriority'];
}
/**
* Retrieve a reference to an active module.
*
* Each module is instantiated only once, so if the module was already loaded you'll get back
* a reference to the existing module object. If the module isn't loaded or instantiated yet,
* the function will do it automatically (but only for active modules).
*
* @param string $module_id Module ID.
* @param bool $autoload Optional. Whether to load the module file if it's not loaded yet. Defaults to TRUE.
* @param string $category Optional. Return the module only if it's in a specific category. Categories are ignored by default.
* @return blcModule A reference to a module object, or NULL on error.
*/
function get_module( $module_id, $autoload = true, $category = '' ) {
$no_result = null;
if ( ! is_string( $module_id ) ) {
//$backtrace = debug_backtrace();
//FB::error($backtrace, "get_module called with a non-string argument");
return $no_result;
}
if ( empty( $this->loaded[ $module_id ] ) ) {
if ( $autoload && $this->is_active( $module_id ) ) {
if ( ! $this->load_module( $module_id ) ) {
return $no_result;
}
} else {
return $no_result;
}
}
if ( ! empty( $category ) ) {
$data = $this->get_module_data( $module_id );
if ( $data['ModuleCategory'] != $category ) {
return $no_result;
}
}
$module = $this->init_module( $module_id );
return $module;
}
/**
* Retrieve the header data of a specific module.
* Uses cached module info if available.
*
* @param string $module_id
* @param bool $use_active_cache Check the active module cache before the general one. Defaults to true.
* @return array Associative array of module data, or FALSE if the specified module was not found.
*/
function get_module_data( $module_id, $use_active_cache = true ) {
//Check virtual modules
if ( isset( $this->_virtual_modules[ $module_id ] ) ) {
return $this->_virtual_modules[ $module_id ];
}
//Check active modules
if ( $use_active_cache && isset( $this->plugin_conf->options['active_modules'][ $module_id ] ) ) {
return $this->plugin_conf->options['active_modules'][ $module_id ];
}
//Otherwise, use the general module cache
if ( ! isset( $this->_module_cache ) ) {
$this->get_modules(); //Populates the cache
}
if ( isset( $this->_module_cache[ $module_id ] ) ) {
return $this->_module_cache[ $module_id ];
} else {
return false;
}
}
/**
* Retrieve a list of active modules.
*
* The list of active modules is stored in the 'active_modules' key of the
* plugin configuration object. If this key is not set, this function will
* create it and populate it using the list of default active modules passed
* to the module manager's constructor.
*
* @return array Associative array of module data indexed by module ID.
*/
function get_active_modules() {
if ( isset( $this->plugin_conf->options['active_modules'] ) ) {
return $this->plugin_conf->options['active_modules'];
}
$active = array();
$modules = $this->get_modules();
if ( is_array( $this->default_active_modules ) ) {
foreach ( $this->default_active_modules as $module_id ) {
if ( array_key_exists( $module_id, $modules ) ) {
$active[ $module_id ] = $modules[ $module_id ];
}
}
}
$this->plugin_conf->options['active_modules'] = $active;
$this->plugin_conf->save_options();
return $this->plugin_conf->options['active_modules'];
}
/**
* Determine if module is active.
*
* @param string $module_id
* @return bool
*/
function is_active( $module_id ) {
return array_key_exists( $module_id, $this->get_active_modules() );
}
/**
* Activate a module.
* Does nothing if the module is already active.
*
* @param string $module_id
* @return bool True if module was activated successfully, false otherwise.
*/
function activate( $module_id ) {
if ( $this->is_active( $module_id ) ) {
return true;
}
//Retrieve the module header
$module_data = $this->get_module_data( $module_id, false );
if ( ! $module_data ) {
return false;
}
//Attempt to load the module
if ( $this->load_module( $module_id, $module_data ) ) {
//Okay, if it loads, we can assume it works.
$this->plugin_conf->options['active_modules'][ $module_id ] = $module_data;
$this->plugin_conf->save_options();
//Invalidate the per-category active module cache
$this->_category_cache_active = null;
//Notify the module that it's been activated
$module = $this->get_module( $module_id );
if ( $module ) {
$module->activated();
}
return true;
} else {
return false;
}
}
/**
* Deactivate a module.
* Does nothing if the module is already inactive.
*
* @param string $module_id
* @return bool
*/
function deactivate( $module_id ) {
if ( ! $this->is_active( $module_id ) ) {
return true;
}
//Some modules are supposed to be always active and thus can't be deactivated
$module_data = $this->get_module_data( $module_id, false );
if ( isset( $module_data['ModuleAlwaysActive'] ) && $module_data['ModuleAlwaysActive'] ) {
return false;
}
//Notify the module that it's being deactivated
$module = $this->get_module( $module_id );
if ( $module ) {
$module->deactivated();
}
unset( $this->plugin_conf->options['active_modules'][ $module_id ] );
//Keep track of when each module was last deactivated. Used for parser resynchronization.
if ( isset( $this->plugin_conf->options['module_deactivated_when'] ) ) {
$this->plugin_conf->options['module_deactivated_when'][ $module_id ] = current_time( 'timestamp' );
} else {
$this->plugin_conf->options['module_deactivated_when'] = array(
$module_id => current_time( 'timestamp' ),
);
}
$this->plugin_conf->save_options();
$this->_category_cache_active = null; //Invalidate the by-category cache since we just changed something
return true;
}
/**
* Determine when a module was last deactivated.
*
* @param string $module_id Module ID.
* @return int Timestamp of last deactivation, or 0 if the module has never been deactivated.
*/
function get_last_deactivation_time( $module_id ) {
if ( isset( $this->plugin_conf->options['module_deactivated_when'][ $module_id ] ) ) {
return $this->plugin_conf->options['module_deactivated_when'][ $module_id ];
} else {
return 0;
}
}
/**
* Set the current list of active modules. If any of the modules are not currently active,
* they will be activated. Any currently active modules that are not on the new list will
* be deactivated.
*
* @param array $ids An array of module IDs.
* @return void
*/
function set_active_modules( $ids ) {
$current_active = array_keys( $this->get_active_modules() );
$activate = array_diff( $ids, $current_active );
$deactivate = array_diff( $current_active, $ids );
//Deactivate any modules not present in the new list
foreach ( $deactivate as $module_id ) {
$this->deactivate( $module_id );
}
//Activate modules present in the new list but not in the old list
foreach ( $activate as $module_id ) {
$this->activate( $module_id );
}
//Ensure all active modules have the latest headers
$this->refresh_active_module_cache();
//Invalidate the per-category active module cache
$this->_category_cache_active = null;
}
/**
* Send the activation message to all currently active plugins when the plugin is activated.
*
* @return void
*/
function plugin_activated() {
global $blclog;
//Ensure all active modules have the latest headers
$blclog->log( '... Updating module cache' );
$start = microtime( true );
$this->refresh_active_module_cache();
$blclog->info( sprintf( '... Cache refresh took %.3f seconds', microtime( true ) - $start ) );
//Notify them that we've been activated
$blclog->log( '... Loading modules' );
$start = microtime( true );
$this->load_modules();
$blclog->info( sprintf( '... %d modules loaded in %.3f seconds', count( $this->loaded ), microtime( true ) - $start ) );
$active = $this->get_active_modules();
foreach ( $active as $module_id => $module_data ) {
$blclog->log( sprintf( '... Notifying module "%s"', $module_id ) );
$module = $this->get_module( $module_id );
if ( $module ) {
$module->plugin_activated();
} else {
$blclog->warn( sprintf( '... Module "%s" failed to load!', $module_id ) );
}
}
}
/**
* Refresh the cached data of all active modules.
*
* @return array An updated list of active modules.
*/
function refresh_active_module_cache() {
$modules = $this->get_modules();
foreach ( $this->plugin_conf->options['active_modules'] as $module_id => $module_header ) {
if ( array_key_exists( $module_id, $modules ) ) {
$this->plugin_conf->options['active_modules'][ $module_id ] = $modules[ $module_id ];
}
}
$this->plugin_conf->save_options();
$this->_category_cache_active = null; //Invalidate the by-category active module cache
return $this->plugin_conf->options['active_modules'];
}
/**
* Load active modules.
*
* @param string $context Optional. If specified, only the modules that match this context (or the "all" context) will be loaded.
* @return void
*/
function load_modules( $context = '' ) {
$active = $this->get_active_modules();
//Avoid trying to load a virtual module before the module that registered it has been loaded.
$active = $this->put_virtual_last( $active );
foreach ( $active as $module_id => $module_data ) {
//Load the module
$should_load = ( 'all' == $module_data['ModuleContext'] ) || ( ! empty( $context ) && $module_data['ModuleContext'] == $context );
if ( $should_load ) {
$this->load_module( $module_id, $module_data );
}
}
}
/**
* Load and possibly instantiate a specific module.
*
* @access private
*
* @param string $module_id
* @param array $module_data
* @return bool True if the module was successfully loaded, false otherwise.
*/
function load_module( $module_id, $module_data = null ) {
//Only load each module once.
if ( ! empty( $this->loaded[ $module_id ] ) ) {
return true;
}
if ( ! isset( $module_data ) ) {
$module_data = $this->get_module_data( $module_id );
if ( empty( $module_data ) ) {
return false;
}
}
//Load a normal module
if ( empty( $module_data['virtual'] ) ) {
//Skip invalid and missing modules
if ( empty( $module_data['file'] ) ) {
return false;
}
//Get the full path to the module file
$filename = $this->module_dir . '/' . $module_data['file'];
if ( ! file_exists( $filename ) ) {
return false;
}
//Load it
include $filename;
$this->loaded[ $module_id ] = true;
} else {
//Virtual modules don't need to be explicitly loaded, but they must
//be registered.
if ( ! array_key_exists( $module_id, $this->_virtual_modules ) ) {
return false;
}
$this->loaded[ $module_id ] = true;
}
//Instantiate the main module class unless lazy init is on (default is off)
if ( ! array_key_exists( $module_id, $this->instances ) ) { //Only try to instantiate once
if ( ! $module_data['ModuleLazyInit'] ) {
$this->init_module( $module_id, $module_data );
}
}
return true;
}
/**
* Instantiate a certain module.
*
* @param string $module_id
* @param array $module_data Optional. The header data of the module that needs to be instantiated. If not specified, it will be retrieved automatically.
* @return object The newly instantiated module object (extends blcModule), or NULL on error.
*/
function init_module( $module_id, $module_data = null ) {
//Each module is only instantiated once.
if ( isset( $this->instances[ $module_id ] ) ) {
return $this->instances[ $module_id ];
}
if ( ! isset( $module_data ) ) {
$module_data = $this->get_module_data( $module_id );
if ( empty( $module_data ) ) {
return null;
}
}
if ( ! empty( $module_data['ModuleClassName'] ) && class_exists( $module_data['ModuleClassName'] ) ) {
$className = $module_data['ModuleClassName'];
$this->instances[ $module_id ] = new $className(
$module_id,
$module_data,
$this->plugin_conf,
$this
);
return $this->instances[ $module_id ];
};
return null;
}
function is_instantiated( $module_id ) {
return ! empty( $this->instances[ $module_id ] );
}
/**
* Register a virtual module.
*
* Virtual modules are the same as normal modules, except that they can be added
* on the fly, e.g. by other modules.
*
* @param string $module_id Module Id.
* @param string $module_data Associative array of module data. All module header fields are allowed, except ModuleID.
* @return void
*/
function register_virtual_module( $module_id, $module_data ) {
$module_data = $this->normalize_module_header( $module_data, $module_id );
$module_data['virtual'] = true;
$this->_virtual_modules[ $module_id ] = $module_data;
}
/**
* Sort an array of modules so that all virtual modules are placed at its end.
*
* @param array $modules Input array, [module_id => module_data, ...].
* @return array Sorted array.
*/
function put_virtual_last( $modules ) {
uasort( $modules, array( &$this, 'compare_virtual_flags' ) );
return $modules;
}
/**
* Callback function for sorting modules by the state of their 'virtual' flag.
*
* @param array $a Associative array of module A data
* @param array $b Associative array of module B data
* @return int
*/
function compare_virtual_flags( $a, $b ) {
if ( empty( $a['virtual'] ) ) {
return empty( $b['virtual'] ) ? 0 : -1;
} else {
return empty( $b['virtual'] ) ? 1 : 0;
}
}
/**
* Validate active modules.
*
* Validates all active modules, deactivates invalid ones and returns
* an array of deactivated modules.
*
* @return array
*/
function validate_active_modules() {
$active = $this->get_active_modules();
if ( empty( $active ) ) {
return array();
}
$invalid = array();
foreach ( $active as $module_id => $module_data ) {
$rez = $this->validate_module( $module_data );
if ( is_wp_error( $rez ) ) {
$invalid[ $module_id ] = $rez;
$this->deactivate( $module_id );
}
}
return $invalid;
}
/**
* Validate module data.
*
* Checks that the module file exists or that the module
* is a currently registered virtual module.
*
* @param array $module_data Associative array of module data.
* @return bool|WP_Error True on success, an error object if the module fails validation
*/
function validate_module( $module_data ) {
if ( empty( $module_data['ModuleID'] ) ) {
return new WP_Error( 'invalid_cached_header', 'The cached module header is invalid' );
}
if ( empty( $module_data['virtual'] ) ) {
//Normal modules must have a valid filename
if ( empty( $module_data['file'] ) ) {
return new WP_Error( 'module_not_found', 'Invalid module file' );
}
$filename = $this->module_dir . '/' . $module_data['file'];
if ( ! file_exists( $filename ) ) {
return new WP_Error( 'module_not_found', 'Module file not found' );
}
//The module file header must be in the proper format. While $module_data comes
//from cache and can be assumed to be correct, get_modules() will attempt to load
//the current headers and only return modules with semi-valid headers.
$installed = $this->get_modules();
if ( ! array_key_exists( $module_data['ModuleID'], $installed ) ) {
return new WP_Error( 'invalid_module_header', 'Invalid module header' );
}
} else {
//Virtual modules need to be currently registered
if ( ! array_key_exists( $module_data['ModuleID'], $this->_virtual_modules ) ) {
return new WP_Error( 'module_not_registered', 'The virtual module is not registered' );
}
}
return true;
}
/**
* Add BLC-module specific headers to the list of allowed plugin headers. This
* lets us use get_plugins() to retrieve the list of BLC modules.
*
* @param array $headers Currently known plugin headers.
* @return array New plugin headers.
*/
function inject_module_headers( $headers ) {
$module_headers = array(
'ModuleID',
'ModuleCategory',
'ModuleContext',
'ModuleLazyInit',
'ModuleClassName',
'ModulePriority',
'ModuleCheckerUrlPattern',
'ModuleHidden', //Don't show the module in the Settings page
'ModuleAlwaysActive', //Module can't be deactivated.
'ModuleRequiresPro', //Can only be activated in the Pro version
);
return array_merge( $headers, $module_headers );
}
/**
* Normalize a module header, using defaults where necessary.
*
* @param array $module_header Module header, as read from the module's .php file.
* @param string $module_id Module ID.
* @param string $module_filename Module filename. Optional.
* @return array Normalized module header.
*/
function normalize_module_header( $module_header, $module_id, $module_filename = '' ) {
//Default values for optional module header fields
$defaults = array(
'ModuleContext' => 'all',
'ModuleCategory' => 'other',
'ModuleLazyInit' => 'false',
'ModulePriority' => '0',
'ModuleHidden' => 'false',
'ModuleAlwaysActive' => 'false',
'ModuleRequiresPro' => 'false',
'TextDomain' => 'broken-link-checker', //For translating module headers
);
$module_header['ModuleID'] = $module_id; //Just for consistency
$module_header['file'] = $module_filename; //Used later to load the module
//Apply defaults
foreach ( $defaults as $field => $default_value ) {
if ( empty( $module_header[ $field ] ) ) {
$module_header[ $field ] = $default_value;
}
}
//Convert bool/int fields from strings to native datatypes
$module_header['ModuleLazyInit'] = $this->str_to_bool( $module_header['ModuleLazyInit'] );
$module_header['ModuleHidden'] = $this->str_to_bool( $module_header['ModuleHidden'] );
$module_header['ModuleAlwaysActive'] = $this->str_to_bool( $module_header['ModuleAlwaysActive'] );
$module_header['ModuleRequiresPro'] = $this->str_to_bool( $module_header['ModuleRequiresPro'] );
$module_header['ModulePriority'] = intval( $module_header['ModulePriority'] );
return $module_header;
}
/**
* Converts the strings "true" and "false" to boolean TRUE and FALSE, respectively.
* Any other string will yield FALSE.
*
* @param string $value "true" or "false", case-insensitive.
* @return bool
*/
function str_to_bool( $value ) {
$value = trim( strtolower( $value ) );
return 'true' == $value;
}
/**
* Generates a PHP script that calls the __() i18n function with
* the name and description of each available module. The generated
* script is used to make module headers show up in the .POT file.
*
* @access private
*
* @return string
*/
function _build_header_translation_code() {
$this->_module_cache = null; //Clear the cache
$modules = $this->get_modules();
$strings = array();
foreach ( $modules as $module_id => $module_header ) {
if ( $module_header['ModuleHidden'] || ( 'write-module-placeholders' == $module_id ) ) {
continue;
}
if ( ! empty( $module_header['Name'] ) ) {
$strings[] = sprintf(
'_x("%s", "module name", "broken-link-checker");',
str_replace( '"', '\"', $module_header['Name'] )
);
}
}
return "<?php\n" . implode( "\n", $strings ) . "\n";
}
}