| 1 |
<?php |
| 2 |
|
| 3 |
namespace WeDevs\WeDocs; |
| 4 |
|
| 5 |
/** |
| 6 |
* Scripts and Styles Class |
| 7 |
*/ |
| 8 |
class Assets { |
| 9 |
|
| 10 |
/** |
| 11 |
* Assets constructor. |
| 12 |
*/ |
| 13 |
public function __construct() { |
| 14 |
add_action( 'init', array( $this, 'register' ) ); |
| 15 |
add_action( 'init', array( $this, 'register_translations' ) ); |
| 16 |
add_action( 'admin_enqueue_scripts', array( $this, 'admin_enqueue' ) ); |
| 17 |
add_action( 'wedocs_load_faq_page', array( $this, 'enqueue_faq_assets' ) ); |
| 18 |
} |
| 19 |
|
| 20 |
/** |
| 21 |
* Register plugin assets. |
| 22 |
* |
| 23 |
* @since 2.0.2 |
| 24 |
* |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public function register() { |
| 28 |
$assets_url = wedocs()->plugin_url() . '/assets'; |
| 29 |
$assets_path = wedocs()->plugin_path() . '/assets'; |
| 30 |
|
| 31 |
// Register admin scripts. |
| 32 |
wp_register_script( |
| 33 |
'wedocs-admin-script', |
| 34 |
$assets_url . '/js/admin-script.js', |
| 35 |
array( 'jquery' ), |
| 36 |
filemtime( $assets_path . '/js/admin-script.js' ), |
| 37 |
true |
| 38 |
); |
| 39 |
|
| 40 |
wp_localize_script( |
| 41 |
'wedocs-admin-script', |
| 42 |
'weDocsAdminScriptVars', |
| 43 |
array( |
| 44 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 45 |
'nonce' => wp_create_nonce( 'wedocs-ajax' ), |
| 46 |
'assetsUrl' => WEDOCS_ASSETS, |
| 47 |
'isPro' => wedocs_is_pro_active(), |
| 48 |
), |
| 49 |
); |
| 50 |
|
| 51 |
if ( file_exists( WEDOCS_PATH . '/assets/build/index.asset.php' ) ) { |
| 52 |
$react_dependencies = require WEDOCS_PATH . '/assets/build/index.asset.php'; |
| 53 |
|
| 54 |
// Adding wedocs necessary assets. |
| 55 |
wp_register_style( |
| 56 |
'wedocs-app-style', |
| 57 |
$assets_url . '/build/index.css', |
| 58 |
array(), |
| 59 |
$react_dependencies['version'], |
| 60 |
); |
| 61 |
|
| 62 |
wp_register_script( |
| 63 |
'wedocs-app-script', |
| 64 |
$assets_url . '/build/index.js', |
| 65 |
$react_dependencies['dependencies'], |
| 66 |
$react_dependencies['version'], |
| 67 |
true |
| 68 |
); |
| 69 |
|
| 70 |
wp_localize_script( |
| 71 |
'wedocs-app-script', |
| 72 |
'weDocsAdminVars', |
| 73 |
array( |
| 74 |
'adminUrl' => admin_url(), |
| 75 |
'hasManageCap' => current_user_can( 'manage_options' ), |
| 76 |
'aiProviderConfigs' => wedocs_get_ai_provider_configs(), |
| 77 |
'adminUrl' => admin_url(), |
| 78 |
'hasManageCap' => current_user_can( 'manage_options' ), |
| 79 |
'migrationNonce' => wp_create_nonce( 'wedocs-migration' ), |
| 80 |
'weDocsUrl' => admin_url( 'admin.php?page=wedocs#/' ), |
| 81 |
'pro_active' => wedocs_is_pro_active(), |
| 82 |
'dokan_active' => is_plugin_active( 'dokan-lite/dokan.php' ), |
| 83 |
'upgradePopupContent' => wedocs_get_upgrade_popup_content(), |
| 84 |
'siteUrl' => home_url( '/' ), |
| 85 |
), |
| 86 |
); |
| 87 |
} |
| 88 |
|
| 89 |
if ( file_exists( WEDOCS_PATH . '/assets/build/block.asset.php' ) ) { |
| 90 |
$block_dependencies = require WEDOCS_PATH . '/assets/build/block.asset.php'; |
| 91 |
// wp_register_style( |
| 92 |
// 'wedocs-block-style', |
| 93 |
// $assets_url . '/build/style-block.css', |
| 94 |
// $block_dependencies['version'] |
| 95 |
// ); |
| 96 |
|
| 97 |
wp_register_script( |
| 98 |
'wedocs-block-script', |
| 99 |
$assets_url . '/build/block.js', |
| 100 |
$block_dependencies['dependencies'], |
| 101 |
$block_dependencies['version'], |
| 102 |
true |
| 103 |
); |
| 104 |
|
| 105 |
wp_localize_script( |
| 106 |
'wedocs-block-script', |
| 107 |
'weDocsBlockVars', |
| 108 |
array( 'siteUrl' => site_url() ), |
| 109 |
); |
| 110 |
} |
| 111 |
|
| 112 |
// Register editor scripts for AI Doc Writer |
| 113 |
if ( file_exists( WEDOCS_PATH . '/assets/build/editor.asset.php' ) ) { |
| 114 |
$editor_dependencies = require WEDOCS_PATH . '/assets/build/editor.asset.php'; |
| 115 |
|
| 116 |
wp_register_script( |
| 117 |
'wedocs-editor-script', |
| 118 |
$assets_url . '/build/editor.js', |
| 119 |
$editor_dependencies['dependencies'], |
| 120 |
$editor_dependencies['version'], |
| 121 |
true |
| 122 |
); |
| 123 |
|
| 124 |
wp_localize_script( |
| 125 |
'wedocs-editor-script', |
| 126 |
'weDocsEditorVars', |
| 127 |
array( |
| 128 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 129 |
'nonce' => wp_create_nonce( 'wp_rest' ), |
| 130 |
'aiProviderConfigs' => wedocs_get_ai_provider_configs(), |
| 131 |
'aiSettings' => wedocs_get_ai_settings_for_frontend(), |
| 132 |
), |
| 133 |
); |
| 134 |
} |
| 135 |
|
| 136 |
// Register FAQ assets. |
| 137 |
if ( file_exists( WEDOCS_PATH . '/assets/build/faq.asset.php' ) ) { |
| 138 |
$faq_dependencies = require WEDOCS_PATH . '/assets/build/faq.asset.php'; |
| 139 |
|
| 140 |
wp_register_style( |
| 141 |
'wedocs-faq-style', |
| 142 |
$assets_url . '/build/faq.css', |
| 143 |
array(), |
| 144 |
$faq_dependencies['version'], |
| 145 |
); |
| 146 |
|
| 147 |
wp_register_script( |
| 148 |
'wedocs-faq-script', |
| 149 |
$assets_url . '/build/faq.js', |
| 150 |
$faq_dependencies['dependencies'], |
| 151 |
$faq_dependencies['version'], |
| 152 |
true |
| 153 |
); |
| 154 |
|
| 155 |
wp_localize_script( |
| 156 |
'wedocs-faq-script', |
| 157 |
'weDocsFaqVars', |
| 158 |
array( |
| 159 |
'restNonce' => wp_create_nonce( 'wp_rest' ), |
| 160 |
), |
| 161 |
); |
| 162 |
} |
| 163 |
|
| 164 |
wp_enqueue_style( 'wedocs-block-style' ); |
| 165 |
} |
| 166 |
|
| 167 |
/** |
| 168 |
* Register script translations. |
| 169 |
* |
| 170 |
* @since 2.0.2 |
| 171 |
* |
| 172 |
* @return void |
| 173 |
*/ |
| 174 |
public function register_translations() { |
| 175 |
wp_set_script_translations( |
| 176 |
'wedocs-app-script', |
| 177 |
'wedocs', |
| 178 |
plugin_dir_path( WEDOCS_FILE ) . 'languages' |
| 179 |
); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* Enqueue admin scripts. |
| 184 |
* |
| 185 |
* @since 2.0.2 |
| 186 |
* |
| 187 |
* @return void |
| 188 |
*/ |
| 189 |
public function admin_enqueue() { |
| 190 |
wp_enqueue_style( 'wedocs-block-style' ); |
| 191 |
|
| 192 |
// Enqueue admin script. |
| 193 |
wp_enqueue_script( 'wedocs-admin-script' ); |
| 194 |
wp_enqueue_script( 'wedocs-block-script' ); |
| 195 |
|
| 196 |
// Enqueue admin app scripts. |
| 197 |
if ( 'toplevel_page_wedocs' === get_current_screen()->id ) { |
| 198 |
wp_enqueue_style( 'wedocs-app-style' ); |
| 199 |
wp_enqueue_script( 'wedocs-app-script' ); |
| 200 |
} |
| 201 |
|
| 202 |
// Enqueue editor scripts for docs post type |
| 203 |
$screen = get_current_screen(); |
| 204 |
if ( $screen && ( 'post' === $screen->base && 'docs' === $screen->post_type ) ) { |
| 205 |
wp_enqueue_script( 'wedocs-editor-script' ); |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* Enqueue FAQ page assets. |
| 211 |
* |
| 212 |
* @since WEDOCS_SINCE |
| 213 |
* |
| 214 |
* @return void |
| 215 |
*/ |
| 216 |
public function enqueue_faq_assets() { |
| 217 |
wp_enqueue_media(); |
| 218 |
// Shared Tailwind build, so the FAQ stylesheet only carries its own rules. |
| 219 |
wp_enqueue_style( 'wedocs-app-style' ); |
| 220 |
wp_enqueue_style( 'wedocs-faq-style' ); |
| 221 |
wp_enqueue_script( 'wedocs-faq-script' ); |
| 222 |
} |
| 223 |
} |
| 224 |
|