1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Flynt\Features\Acf; |
4
|
|
|
|
5
|
|
|
use Flynt\Features\AdminNotices\AdminNoticeManager; |
6
|
|
|
use Flynt\Utils\ArrayHelpers; |
7
|
|
|
use Flynt\Utils\Asset; |
8
|
|
|
|
9
|
|
|
class Loader |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
protected static $helpers = []; |
13
|
|
|
protected static $requirements = []; |
14
|
|
|
protected static $requirementsMet = false; |
15
|
|
|
|
16
|
|
|
public static function setup($helpers) |
17
|
|
|
{ |
18
|
|
|
if (empty($helpers)) { |
19
|
|
|
return; |
20
|
|
|
} |
21
|
|
|
|
22
|
|
|
self::$helpers = ArrayHelpers::indexedValuesToAssocKeys($helpers); |
23
|
|
|
self::$requirementsMet = !in_array(false, self::checkRequirements(), true); |
24
|
|
|
|
25
|
|
|
if (self::$requirementsMet) { |
26
|
|
|
self::setupHelpers(); |
27
|
|
|
|
28
|
|
|
// add styles for admin area |
29
|
|
|
add_action('admin_enqueue_scripts', function () { |
30
|
|
|
Asset::enqueue([ |
31
|
|
|
'type' => 'style', |
32
|
|
|
'name' => 'Flynt/Features/Acf/AdminCss', |
33
|
|
|
'path' => 'Features/Acf/admin.css' |
34
|
|
|
]); |
35
|
|
|
|
36
|
|
|
Asset::enqueue([ |
37
|
|
|
'type' => 'script', |
38
|
|
|
'name' => 'Flynt/Features/Acf/AdminJs', |
39
|
|
|
'path' => 'Features/Acf/admin.js', |
40
|
|
|
'dependencies' => ['jquery'] |
41
|
|
|
]); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
public static function init() |
47
|
|
|
{ |
48
|
|
|
if (true === self::$requirementsMet) { |
49
|
|
|
self::initHelpers(); |
50
|
|
|
} elseif (class_exists('Flynt\Features\AdminNotices\AdminNoticeManager')) { |
51
|
|
|
self::showAdminNotice(); |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
protected static function checkRequirements() |
56
|
|
|
{ |
57
|
|
|
self::$requirements = [ |
58
|
|
|
'acfEnabled' => class_exists('acf'), |
59
|
|
|
'acfFunctionsExist' => function_exists('acf_add_options_page') && function_exists('acf_add_options_sub_page'), |
60
|
|
|
'acfComposerEnabled' => class_exists('ACFComposer\ACFComposer'), |
61
|
|
|
]; |
62
|
|
|
return self::$requirements; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
View Code Duplication |
protected static function setupHelpers() |
|
|
|
|
66
|
|
|
{ |
67
|
|
|
$namespacePrefix = 'Flynt\Features\Acf'; |
68
|
|
|
foreach (self::$helpers as $helperName => $helperOptions) { |
69
|
|
|
$className = "{$namespacePrefix}\\$helperName"; |
70
|
|
|
if (class_exists($className) && method_exists($className, 'setup')) { |
71
|
|
|
$className::setup($helperOptions); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
View Code Duplication |
protected static function initHelpers() |
|
|
|
|
77
|
|
|
{ |
78
|
|
|
$namespacePrefix = 'Flynt\Features\Acf'; |
79
|
|
|
foreach (self::$helpers as $helperName => $helperOptions) { |
80
|
|
|
$className = "{$namespacePrefix}\\$helperName"; |
81
|
|
|
if (class_exists($className) && method_exists($className, 'init')) { |
82
|
|
|
$className::init($helperOptions); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
protected static function showAdminNotice() |
88
|
|
|
{ |
89
|
|
|
$messages = []; |
90
|
|
|
|
91
|
|
|
if (!self::$requirements['acfEnabled']) { |
92
|
|
|
$messages[] = 'Advanced Custom Fields Plugin not installed or activated. Make sure you <a href="' |
93
|
|
|
. esc_url(admin_url('plugins.php')) . '">install or activate the plugin</a>.'; |
94
|
|
|
} elseif (!self::$requirements['acfFunctionsExist']) { |
95
|
|
|
$messages[] = 'Advanced Custom Fields Plugin Functions not found! Please make sure you are using' |
96
|
|
|
. ' the latest version of ACF.'; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
if (!self::$requirements['acfComposerEnabled']) { |
100
|
|
|
$messages[] = 'ACF Composer Plugin not installed or activated. Make sure you <a href="' |
101
|
|
|
. esc_url(admin_url('plugins.php')) . '">install or activate the plugin</a>.'; |
102
|
|
|
} |
103
|
|
|
|
104
|
|
|
$manager = AdminNoticeManager::getInstance(); |
105
|
|
|
$manager->addNotice($messages, [ |
106
|
|
|
'title' => 'Could not initialize ACF Helpers (' . implode(', ', array_keys(self::$helpers)) . ')', |
107
|
|
|
'type' => 'warning', |
108
|
|
|
'dismissible' => true, |
109
|
|
|
'filenames' => basename(__DIR__) |
110
|
|
|
]); |
111
|
|
|
} |
112
|
|
|
} |
113
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.