PluginProbe
پارسی دیت – Parsi Date / 6.3
پارسی دیت – Parsi Date v6.3
6.3 6.2.1 6.2 6.1 5.1.6 5.1.7 5.1.8 5.1.8.2 6.0 trunk 1.0 1.1 1.2 1.3 1.3.1 1.3.2 1.3.3 1.3.4 1.3.5 2.0.0-alpha 2.1 2.1.1 2.1.2 2.1.3 2.1.5 All 49 releases
wp-parsidate / inc / Addons / Addon.php
Addon.php
305 lines 10.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace WPParsidate\Addons;
4
5 defined( 'ABSPATH' ) || exit;
6
7 use WPParsidate\Admin\{AdminPages, AdminSettings};
8 use WPParsidate\Helper\{Assets, Cache};
9 use WPParsidate\Settings\Settings;
10
11 abstract class Addon {
12 public string $addonID;
13
14 public string $currentTab = '';
15
16 public string $currentSection = '';
17
18 public function __construct() {
19 add_filter( 'wp_parsidate_addons', [ $this, 'registerAddon' ] );
20
21 add_action( 'wp_parsidate_admin_init', [ $this, 'registerMenu' ] );
22 add_filter( 'wp_parsidate_settings', [ $this, 'allSettings' ] );
23
24 if ( $this->addonID ) {
25 //add_filter( 'wp_parsidate_' . $this->addonID . '_tab_display_notice', '__return_false' );
26 //add_filter( 'wp_parsidate_' . $this->addonID . '_tab_content_display_notice', '__return_true' );
27 if ( $this->currentTab ) {
28 add_filter( 'wp_parsidate_dashboard_addon_links', [ $this, 'addDashboardLink' ] );
29 }
30 }
31
32 // Register Plugin hooks
33 if ( $this->currentTab ) {
34 add_filter( 'wp_parsidate_' . $this->currentTab . '_settings_sections', [
35 $this,
36 'registerAddSectionSettings'
37 ] );
38 }
39
40 // Register WordPress hooks
41 add_action( 'init', [ $this, 'registerInitM1Action' ], - 1 );
42 add_action( 'init', [ $this, 'registerInitAction' ], 9 );
43 add_action( 'admin_init', [ $this, 'registerAdminInitAction' ] );
44 add_action( 'template_redirect', [ $this, 'registerTemplateRedirectAction' ] );
45 add_action( 'wp', [ $this, 'registerWpAction' ] );
46 add_action( 'wp_enqueue_scripts', [ $this, 'registerWpEnqueueScriptsAction' ] );
47 add_action( 'admin_enqueue_scripts', [ $this, 'registerAdminEnqueueScriptsAction' ] );
48 add_action( 'wp_footer', [ $this, 'registerWpFooterAction' ] );
49 add_action( 'wp_body_open', [ $this, 'registerWpBodyOpenAction' ], 0 );
50
51 add_filter( 'query_vars', [ $this, 'registerQueryVarsFilter' ] );
52 add_filter( 'woocommerce_account_menu_items', [ $this, 'registerWooAccountMenuItemsFilter' ] );
53 add_filter( 'admin_body_class', [ $this, 'registerAdminBodyClassFilter' ] );
54 }
55
56 public function registerAddSectionSettings( $sections ): array {
57 if ( method_exists( $this, 'addSectionSettings' ) && $this->isActivated() ) {
58 return $this->addSectionSettings( $sections );
59 }
60
61 return $sections;
62 }
63
64 public function registerQueryVarsFilter( $vars ) {
65 if ( method_exists( $this, 'queryVarsFilter' ) && $this->isActivated() ) {
66 return $this->queryVarsFilter( $vars );
67 }
68
69 return $vars;
70 }
71
72 public function registerWooAccountMenuItemsFilter( $items ) {
73 if ( method_exists( $this, 'wooAccountMenuItemsFilter' ) && $this->isActivated() ) {
74 return $this->wooAccountMenuItemsFilter( $items );
75 }
76
77 return $items;
78 }
79
80 public function registerAdminBodyClassFilter( $classes ) {
81 if ( method_exists( $this, 'adminBodyClassFilter' ) && $this->isActivated() ) {
82 return $this->adminBodyClassFilter( $classes );
83 }
84
85 return $classes;
86 }
87
88 public function registerWpFooterAction(): void {
89 if ( method_exists( $this, 'wpFooterAction' ) && $this->isActivated() ) {
90 $this->wpFooterAction();
91 }
92 }
93
94 public function registerWpBodyOpenAction(): void {
95 if ( method_exists( $this, 'wpBodyOpenAction' ) && $this->isActivated() ) {
96 $this->wpBodyOpenAction();
97 }
98 }
99
100 public function registerAdminEnqueueScriptsAction(): void {
101 if ( method_exists( $this, 'adminEnqueueScriptsAction' ) && $this->isActivated() ) {
102 $this->adminEnqueueScriptsAction();
103 }
104 }
105
106 public function registerWpEnqueueScriptsAction(): void {
107 if ( method_exists( $this, 'wpEnqueueScriptsAction' ) && $this->isActivated() ) {
108 $this->wpEnqueueScriptsAction();
109 }
110 }
111
112 public function registerAdminInitAction(): void {
113 if ( method_exists( $this, 'adminInitAction' ) && $this->isActivated() ) {
114 $this->adminInitAction();
115 }
116 }
117
118 public function registerInitM1Action(): void {
119 if ( method_exists( $this, 'initM1Action' ) && $this->isActivated() ) {
120 $this->initM1Action();
121 }
122 }
123
124 public function registerInitAction(): void {
125 if ( method_exists( $this, 'initAction' ) && $this->isActivated() ) {
126 $this->initAction();
127 }
128 }
129
130 public function registerTemplateRedirectAction(): void {
131 if ( method_exists( $this, 'templateRedirectAction' ) && $this->isActivated() ) {
132 $this->templateRedirectAction();
133 }
134 }
135
136 public function registerWpAction(): void {
137 if ( method_exists( $this, 'wpAction' ) && $this->isActivated() ) {
138 $this->wpAction();
139 }
140 }
141
142 public function registerMenu(): void {
143 if ( $this->getInfo( 'has_page', false ) && $this->isActivated() ) {
144 add_filter( 'wp_parsidate_menus', [ $this, 'addMenu' ], 12 );
145
146 if ( $this->getInfo( 'content_header', false ) ) {
147 add_action( 'wp_parsidate_' . $this->addonID . '_tab_header',
148 [ $this, 'displayContentHeader' ], - 10 );
149 }
150
151 if ( method_exists( $this, 'content' ) ) {
152 add_action( 'wp_parsidate_' . $this->addonID . '_tab_content', [ $this, 'content' ] );
153 }
154
155 if ( method_exists( $this, 'settings' ) ) {
156 add_filter( 'wp_parsidate_' . $this->addonID . '_settings', [ $this, 'settings' ] );
157 }
158 }
159 }
160
161 public function displayContentHeader(): void {
162 if ( $this->getInfo( 'content_header', false ) ) {
163 AdminSettings::headerSettings( $this->addonID, $this->getInfo() );
164 }
165 }
166
167 public function allSettings( $settings ): array {
168 if ( method_exists( $this, 'settings' ) ) {
169 $settings[ $this->addonID ] = $this->settings();
170 }
171
172 return $settings;
173 }
174
175 public function addMenu( $menus ) {
176 $addon = $this->getInfo();
177
178 $menus[ $this->addonID ] = array(
179 'title' => $addon['menu_title'] ?? ( $addon['name'] ?? $addon['title'] ),
180 'icon' => $addon['menu_icon'] ?? ( $addon['icon'] ?? '' )
181 );
182
183 return $menus;
184 }
185
186 public function addDashboardLink( $addons ): array {
187 if ( ! $this->isActivated() ) {
188 return $addons;
189 }
190
191 $addon = $this->getInfo();
192 $addonCats = Addons::getAddonCats();
193 $cat = empty( $addon['cat'] ) || ! array_key_exists( $addon['cat'], $addonCats ) ? 'other' : $addon['cat'];
194 $icon = ! empty( $addon['icon'] ) && Assets::isSvgImageString( $addon['icon'] ) ? Assets::setSvgDimensions( $addon['icon'], 50 ) : '';
195
196 if ( $this->getInfo( 'has_page', false ) ) {
197 $link = AdminPages::link( [
198 'tab' => $this->addonID
199 ] );
200 } else {
201 $link = AdminPages::link( [
202 'tab' => $this->currentTab,
203 'section' => empty( $this->currentSection ) ? $this->addonID : $this->currentSection,
204 ] );
205 }
206
207 $addons[ $cat ][] = [
208 'title' => $addon['name'] ?? $addon['title'],
209 'desc' => $addon['desc'] ?? '',
210 'link' => $link,
211 'icon' => $icon,
212 'type' => 'addon'
213 ];
214
215 return $addons;
216 }
217
218 public function registerAddon( $addons ) {
219 if ( $this->canDisplay() ) {
220 $addons[] = $this->getInfo();
221 }
222
223 return $addons;
224 }
225
226 private function canDisplay(): bool {
227 return ! $this->getInfo( 'is_demo', false ) || ! $this->isActivated();
228 }
229
230 private function getInfo( $key = null, $default = null ) {
231 $addon = Cache::get( $this->addonID . '_internal_addon_info', false );
232
233 if ( ! is_array( $addon ) ) {
234 $addon = $this->info();
235 Cache::set( $this->addonID . '_internal_addon_info', $addon );
236 }
237
238 if ( $key !== null ) {
239 return $addon[ $key ] ?? $default;
240 }
241
242 return $addon;
243 }
244
245 public function isActivated(): bool {
246 if ( ! $this->getInfo( 'force_enable', false ) && ! Settings::get( 'internal_addon_' . $this->addonID, false ) ) {
247 return false;
248 }
249
250 $requiresPlugins = $this->getInfo( 'requires_plugins', [] );
251 $canActivate = empty( $requiresPlugins );
252
253 if ( ! $canActivate && ! empty( $requiresPlugins ) && is_array( $requiresPlugins ) ) {
254 $requirePluginsActive = Cache::get( $this->addonID . '_requires_plugins_count', false );
255
256 if ( $requirePluginsActive === false ) {
257 $requirePluginsActive = 0;
258 foreach ( $requiresPlugins as $requirePluginPath => $requirePlugin ) {
259 if (
260 ( file_exists( WP_PLUGIN_DIR . '/' . $requirePluginPath ) && is_plugin_active( $requirePluginPath ) ) ||
261 ( ! empty( $requirePlugin['function_check'] ) && function_exists( $requirePlugin['function_check'] ) ) ||
262 ( ! empty( $requirePlugin['class_check'] ) && class_exists( $requirePlugin['class_check'] ) ) ||
263 ( ! empty( $requirePlugin['define_check'] ) && defined( $requirePlugin['define_check'] ) )
264 ) {
265 $requirePluginsActive ++;
266 }
267 }
268 Cache::set( $this->addonID . '_requires_plugins_count', $requirePluginsActive );
269 }
270
271 $canActivate = $requirePluginsActive > 0 && $requirePluginsActive === count( $requiresPlugins );
272 }
273
274 return $canActivate;
275 }
276
277 public function getSettingsKey() {
278 return $this->getInfo( 'settings_key' );
279 }
280
281 public function getSetting( $key = null, $default = null, $useCache = true ) {
282 return Settings::get( $key, $default, $this->getSettingsKey(), $useCache );
283 }
284
285 public function saveSetting( $key, $value ): bool {
286 return Settings::save( $key, $value, $this->getSettingsKey() );
287 }
288
289 public function savesSetting( $options ): bool {
290 return Settings::saves( $options, $this->getSettingsKey() );
291 }
292
293 public function deleteSetting( $key ): bool {
294 return Settings::delete( $key, $this->getSettingsKey() );
295 }
296
297 public function addToArraySetting( $key, $value, $reverse = false ): bool {
298 return Settings::addToArray( $key, $value, $this->getSettingsKey(), $reverse );
299 }
300
301 public function deleteFromArraySetting( $key, $index ): bool {
302 return Settings::deleteFromArray( $key, $index, $this->getSettingsKey() );
303 }
304 }
305