| 1 |
<?php |
| 2 |
/** |
| 3 |
* Scripts class. |
| 4 |
* |
| 5 |
* @since 1.3.0 |
| 6 |
* @package QuillForms |
| 7 |
*/ |
| 8 |
|
| 9 |
namespace QuillForms\Addon; |
| 10 |
|
| 11 |
use QuillForms\Admin\Admin_Loader; |
| 12 |
|
| 13 |
/** |
| 14 |
* Abstract scripts class for plugin extensions. |
| 15 |
* |
| 16 |
* @since 1.3.0 |
| 17 |
*/ |
| 18 |
abstract class Scripts { |
| 19 |
|
| 20 |
/** |
| 21 |
* Addon |
| 22 |
* |
| 23 |
* @var Addon |
| 24 |
*/ |
| 25 |
protected $addon; |
| 26 |
|
| 27 |
/** |
| 28 |
* Scripts to register. |
| 29 |
* 'handle' => [ 'path' => 'relative/to/plugin', 'eneuque' => [ 'admin', 'renderer' ] ] |
| 30 |
* |
| 31 |
* @var array |
| 32 |
* |
| 33 |
* @since 1.3.0 |
| 34 |
*/ |
| 35 |
protected $scripts = array(); |
| 36 |
|
| 37 |
/** |
| 38 |
* Styles to register. |
| 39 |
* 'handle' => [ 'path' => 'relative/to/plugin', 'dependencies' => [], 'eneuque' => [ 'admin', 'renderer' ] ] |
| 40 |
* |
| 41 |
* @var array |
| 42 |
* |
| 43 |
* @since 1.3.0 |
| 44 |
*/ |
| 45 |
protected $styles = array(); |
| 46 |
|
| 47 |
/** |
| 48 |
* Constructor. |
| 49 |
* |
| 50 |
* @since 1.3.0 |
| 51 |
* |
| 52 |
* @param Addon $addon Addon. |
| 53 |
*/ |
| 54 |
public function __construct( $addon ) { |
| 55 |
$this->addon = $addon; |
| 56 |
|
| 57 |
// register. |
| 58 |
add_action( 'wp_default_scripts', array( $this, 'register_scripts' ) ); |
| 59 |
add_action( 'wp_default_styles', array( $this, 'register_styles' ) ); |
| 60 |
// enqueue. |
| 61 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ), 9999999 ); |
| 62 |
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_renderer_scripts' ), 9999999 ); |
| 63 |
// localize. |
| 64 |
add_action( 'admin_enqueue_scripts', array( $this, 'localize_scripts' ), 9999999 ); |
| 65 |
add_action( 'wp_enqueue_scripts', array( $this, 'localize_scripts' ), 9999999 ); |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register scripts |
| 70 |
* |
| 71 |
* @since 1.3.0 |
| 72 |
* |
| 73 |
* @param WP_Scripts $scripts WordPress scripts. |
| 74 |
* @return void |
| 75 |
*/ |
| 76 |
public function register_scripts( $scripts ) { |
| 77 |
foreach ( $this->scripts as $handle => $script ) { |
| 78 |
$filepath = $this->addon->plugin_dir . $script['path']; |
| 79 |
$asset_file = substr( $filepath, 0, -3 ) . '.asset.php'; |
| 80 |
$asset = file_exists( $asset_file ) ? require $asset_file : null; |
| 81 |
$dependencies = isset( $asset['dependencies'] ) ? $asset['dependencies'] : array(); |
| 82 |
$version = isset( $asset['version'] ) ? $asset['version'] : filemtime( $filepath ); |
| 83 |
$script_url = $this->addon->plugin_url . $script['path']; |
| 84 |
quillforms_override_script( |
| 85 |
$scripts, |
| 86 |
$handle, |
| 87 |
$script_url, |
| 88 |
$dependencies, |
| 89 |
$version, |
| 90 |
true, |
| 91 |
$this->addon->textdomain, |
| 92 |
$this->addon->plugin_dir . 'languages' |
| 93 |
); |
| 94 |
} |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Register styles |
| 99 |
* |
| 100 |
* @since 1.3.0 |
| 101 |
* |
| 102 |
* @param WP_Styles $styles WordPress Styles. |
| 103 |
* @return void |
| 104 |
*/ |
| 105 |
public function register_styles( $styles ) { |
| 106 |
foreach ( $this->styles as $handle => $style ) { |
| 107 |
quillforms_override_style( |
| 108 |
$styles, |
| 109 |
$handle, |
| 110 |
$this->addon->plugin_url . $style['path'], |
| 111 |
$style['dependencies'] ?? array(), |
| 112 |
filemtime( $this->addon->plugin_dir . $style['path'] ) |
| 113 |
); |
| 114 |
$styles->add_data( $handle, 'rtl', 'replace' ); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* Enqueue admin scripts and styles. |
| 120 |
* |
| 121 |
* @since 1.3.0 |
| 122 |
* |
| 123 |
* @return void |
| 124 |
*/ |
| 125 |
public function enqueue_admin_scripts() { |
| 126 |
if ( Admin_Loader::is_admin_page() ) { |
| 127 |
foreach ( $this->scripts as $handle => $script ) { |
| 128 |
if ( in_array( 'admin', $script['enqueue'], true ) ) { |
| 129 |
wp_enqueue_script( $handle ); |
| 130 |
} |
| 131 |
} |
| 132 |
foreach ( $this->styles as $handle => $style ) { |
| 133 |
if ( in_array( 'admin', $style['enqueue'], true ) ) { |
| 134 |
wp_enqueue_style( $handle ); |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* Enqueue renderer scripts and styles. |
| 142 |
* |
| 143 |
* @since 1.3.0 |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
public function enqueue_renderer_scripts() { |
| 148 |
if ( $this->is_form_page() ) { |
| 149 |
foreach ( $this->scripts as $handle => $script ) { |
| 150 |
if ( in_array( 'renderer', $script['enqueue'], true ) ) { |
| 151 |
wp_enqueue_script( $handle ); |
| 152 |
} |
| 153 |
} |
| 154 |
foreach ( $this->styles as $handle => $style ) { |
| 155 |
if ( in_array( 'renderer', $style['enqueue'], true ) ) { |
| 156 |
wp_enqueue_style( $handle ); |
| 157 |
} |
| 158 |
} |
| 159 |
} |
| 160 |
} |
| 161 |
|
| 162 |
/** |
| 163 |
* Localize scripts. |
| 164 |
* |
| 165 |
* @since 1.3.0 |
| 166 |
* |
| 167 |
* @return void |
| 168 |
*/ |
| 169 |
public function localize_scripts() {} |
| 170 |
|
| 171 |
/** |
| 172 |
* Is current page is quill_forms post type. |
| 173 |
* |
| 174 |
* @since 1.3.0 |
| 175 |
* |
| 176 |
* @return boolean |
| 177 |
*/ |
| 178 |
protected function is_form_page() { |
| 179 |
return is_singular( 'quill_forms' ); |
| 180 |
} |
| 181 |
|
| 182 |
} |
| 183 |
|