| @@ -1,85 +1,94 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace WPDeveloper\BetterDocs\Utils; |
| 3 | 3 | |
| 4 | 4 | class Enqueue extends Base { |
| 5 | - private $plugin_url; | |
| 6 | - private $plugin_path; | |
| 7 | - private $version; | |
| 5 | + private $plugin_url; | |
| 6 | + private $plugin_path; | |
| 7 | + private $version; | |
| 8 | 8 | |
| 9 | - public function __construct( $plugin_url, $plugin_path, $version ) { | |
| 10 | - $this->plugin_url = $plugin_url; | |
| 11 | - $this->plugin_path = $plugin_path; | |
| 12 | - $this->version = $version; | |
| 13 | - } | |
| 9 | + public function __construct( $plugin_url, $plugin_path, $version ) { | |
| 10 | + $this->plugin_url = $plugin_url; | |
| 11 | + $this->plugin_path = $plugin_path; | |
| 12 | + $this->version = $version; | |
| 13 | + } | |
| 14 | 14 | |
| 15 | - public function enqueue( $handle, $filename, $dependencies = [], $args = null ) { | |
| 16 | - $config = $this->asset_config( $filename, $dependencies, $args ); | |
| 17 | - $this->call_wp_func( 'wp_enqueue', $handle, $config ); | |
| 18 | - } | |
| 15 | + public function enqueue( $handle, $filename, $dependencies = [], $args = null ) { | |
| 16 | + $config = $this->asset_config( $filename, $dependencies, $args ); | |
| 17 | + $this->call_wp_func( 'wp_enqueue', $handle, $config ); | |
| 18 | + } | |
| 19 | 19 | |
| 20 | - public function register( $handle, $filename, $dependencies = [], $args = null ) { | |
| 21 | - $config = $this->asset_config( $filename, $dependencies, $args ); | |
| 22 | - $this->call_wp_func( 'wp_register', $handle, $config ); | |
| 23 | - } | |
| 20 | + public function register( $handle, $filename, $dependencies = [], $args = null ) { | |
| 21 | + $config = $this->asset_config( $filename, $dependencies, $args ); | |
| 22 | + $this->call_wp_func( 'wp_register', $handle, $config ); | |
| 23 | + } | |
| 24 | 24 | |
| 25 | - public function localize( $handle, $name, $args ) { | |
| 26 | - wp_localize_script( $handle, $name, $args ); | |
| 27 | - } | |
| 25 | + public function localize( $handle, $name, $args ) { | |
| 26 | + wp_localize_script( $handle, $name, $args ); | |
| 27 | + } | |
| 28 | 28 | |
| 29 | - private function call_wp_func( $action, $handle, $config ) { | |
| 30 | - call_user_func( | |
| 31 | - $action . '_' . $config['type'], | |
| 32 | - $handle, | |
| 33 | - $config['url'], | |
| 34 | - $config['dependencies'], | |
| 35 | - $config['version'], | |
| 36 | - $config['args'] | |
| 37 | - ); | |
| 29 | + private function call_wp_func( $action, $handle, $config ) { | |
| 30 | + call_user_func( | |
| 31 | + $action . '_' . $config['type'], | |
| 32 | + $handle, | |
| 33 | + $config['url'], | |
| 34 | + $config['dependencies'], | |
| 35 | + $config['version'], | |
| 36 | + $config['args'] | |
| 37 | + ); | |
| 38 | 38 | |
| 39 | - // if ( 'script' === $config['type'] && in_array( 'wp-i18n', $config['dependencies'], true ) ) { | |
| 40 | - // wp_set_script_translations( $handle, 'betterdocs' ); | |
| 41 | - // } | |
| 42 | - } | |
| 39 | + if ( 'script' === $config['type'] && in_array( 'wp-i18n', $config['dependencies'], true ) ) { | |
| 40 | + $url = isset( $config['url'] ) ? $config['url'] : ''; | |
| 41 | + preg_match( '~/plugins/([^/]+)~', $url, $matches ); | |
| 42 | + $url_portion = isset( $matches[1] ) ? $matches[1] : ''; | |
| 43 | + if ( $url_portion == 'betterdocs-pro' ) { | |
| 44 | + wp_set_script_translations( $handle, 'betterdocs-pro' ); | |
| 45 | + } elseif ( $url_portion == 'betterdocs-ai-chatbot' ) { | |
| 46 | + wp_set_script_translations( $handle, 'betterdocs-ai-chatbot' ); | |
| 47 | + } elseif ( $url_portion == 'betterdocs' ) { | |
| 48 | + wp_set_script_translations( $handle, 'betterdocs' ); | |
| 49 | + } | |
| 50 | + } | |
| 51 | + } | |
| 43 | 52 | |
| 44 | - public function asset_config( $filename, $dependencies = [], $args = null ) { | |
| 45 | - $is_js = preg_match( '/\.js$/', $filename ); | |
| 46 | - $basename = preg_replace( '/\.\w+$/', '', $filename ); | |
| 47 | - $url = $this->asset_url( $filename ); | |
| 48 | - $version = $this->version; | |
| 49 | - $asset_config_path = $this->dist_path( $basename . '.asset.php' ); | |
| 53 | + public function asset_config( $filename, $dependencies = [], $args = null ) { | |
| 54 | + $is_js = preg_match( '/\.js$/', $filename ); | |
| 55 | + $basename = preg_replace( '/\.\w+$/', '', $filename ); | |
| 56 | + $url = $this->asset_url( $filename ); | |
| 57 | + $version = $this->version; | |
| 58 | + $asset_config_path = $this->dist_path( $basename . '.asset.php' ); | |
| 50 | 59 | |
| 51 | - if ( file_exists( $asset_config_path ) ) { | |
| 52 | - $asset_config = require $asset_config_path; | |
| 60 | + if ( file_exists( $asset_config_path ) ) { | |
| 61 | + $asset_config = require $asset_config_path; | |
| 53 | 62 | |
| 54 | - if ( $is_js ) { | |
| 55 | - $dependencies = array_unique( array_merge( $asset_config['dependencies'], $dependencies ) ); | |
| 56 | - } | |
| 57 | - $version = $asset_config['version']; | |
| 58 | - } | |
| 63 | + if ( $is_js ) { | |
| 64 | + $dependencies = array_unique( array_merge( $asset_config['dependencies'], $dependencies ) ); | |
| 65 | + } | |
| 66 | + $version = $asset_config['version']; | |
| 67 | + } | |
| 59 | 68 | |
| 60 | - return [ | |
| 61 | - 'url' => $url, | |
| 62 | - 'dependencies' => $dependencies, | |
| 63 | - 'version' => $version, | |
| 64 | - 'type' => $is_js ? 'script' : 'style', | |
| 65 | - 'args' => null !== $args ? $args : ( $is_js ? true : 'all' ) | |
| 66 | - ]; | |
| 67 | - } | |
| 69 | + return [ | |
| 70 | + 'url' => $url, | |
| 71 | + 'dependencies' => $dependencies, | |
| 72 | + 'version' => $version, | |
| 73 | + 'type' => $is_js ? 'script' : 'style', | |
| 74 | + 'args' => null !== $args ? $args : ( $is_js ? true : 'all' ) | |
| 75 | + ]; | |
| 76 | + } | |
| 68 | 77 | |
| 69 | - public function asset_url( $filename ) { | |
| 70 | - if ( filter_var( $filename, FILTER_VALIDATE_URL ) ) { | |
| 71 | - return $filename; | |
| 72 | - } | |
| 78 | + public function asset_url( $filename ) { | |
| 79 | + if ( filter_var( $filename, FILTER_VALIDATE_URL ) ) { | |
| 80 | + return $filename; | |
| 81 | + } | |
| 73 | 82 | |
| 74 | - return esc_url( $this->plugin_url . 'assets/' . $filename ); | |
| 75 | - } | |
| 83 | + return esc_url( $this->plugin_url . 'assets/' . $filename ); | |
| 84 | + } | |
| 76 | 85 | |
| 77 | - public function dist_path( $file ) { | |
| 78 | - return path_join( $this->plugin_path, 'assets/' . $file ); | |
| 79 | - } | |
| 86 | + public function dist_path( $file ) { | |
| 87 | + return path_join( $this->plugin_path, 'assets/' . $file ); | |
| 88 | + } | |
| 80 | 89 | |
| 81 | - public function icon( $name, $is_admin = false ) { | |
| 82 | - $_path = $is_admin ? 'assets/admin/images/' : 'assets/images/'; | |
| 83 | - return esc_url( $this->plugin_url . $_path . $name . '?v=' . $this->version ); | |
| 84 | - } | |
| 90 | + public function icon( $name, $is_admin = false ) { | |
| 91 | + $_path = $is_admin ? 'assets/admin/images/' : 'assets/images/'; | |
| 92 | + return esc_url( $this->plugin_url . $_path . $name . '?v=' . $this->version ); | |
| 93 | + } | |
| 85 | 94 | } |