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