Convert
3 weeks ago
Core
3 weeks ago
Integration
3 weeks ago
Tools
3 weeks ago
App.php
3 weeks ago
AppAssets.php
3 weeks ago
AppAssets.php
244 lines
| 1 | <?php |
| 2 | /** |
| 3 | * Plugin assets |
| 4 | * |
| 5 | * Used for fix admin and editor style |
| 6 | */ |
| 7 | |
| 8 | namespace WPParsidate\App; |
| 9 | |
| 10 | defined( 'ABSPATH' ) || exit; |
| 11 | |
| 12 | use WPParsidate\Core\Names; |
| 13 | use WPParsidate\Helper\{Assets, Param}; |
| 14 | use WPParsidate\Settings\Settings; |
| 15 | |
| 16 | class AppAssets { |
| 17 | public function __construct() { |
| 18 | add_action( 'admin_enqueue_scripts', array( $this, 'adminEnqueueScripts' ) ); |
| 19 | add_filter( 'admin_init', [ $this, 'fixTinyMceFont' ], PHP_INT_MAX ); |
| 20 | //add_filter( 'wp_theme_json_data_theme', [ $this, 'addFontToThemeJson' ] ); |
| 21 | add_action( 'admin_print_styles-plugin-editor.php', [ $this, 'fixCodeEditor' ] ); |
| 22 | add_action( 'admin_print_styles-theme-editor.php', [ $this, 'fixCodeEditor' ] ); |
| 23 | add_action( 'wpp_jalali_datepicker_enqueued', [ $this, 'localizeMonthsName' ] ); |
| 24 | add_action( 'enqueue_block_editor_assets', [ $this, 'blockEditorAssets' ] ); |
| 25 | } |
| 26 | |
| 27 | /** |
| 28 | * Enqueue Gutenberg Jalali Calendar assets for backend editor. |
| 29 | * |
| 30 | * @uses {wp-plugins} |
| 31 | * @uses {wp-i18n} to internationalize the block's text. |
| 32 | * @uses {wp-compose} |
| 33 | * @uses {wp-components} |
| 34 | * @uses {wp-element} for WP Element abstraction — structure of blocks. |
| 35 | * @uses {wp-editor} for WP editor styles. |
| 36 | * @uses {wp-edit-post} to internationalize the block's text. |
| 37 | * @uses {wp-data} |
| 38 | * @uses {wp-date} |
| 39 | * @since 3.0.0 |
| 40 | * @author Alireza Dabiri Nejad / Alirdn |
| 41 | */ |
| 42 | public function blockEditorAssets(): void { |
| 43 | if ( ! Settings::get( 'persian_date', false ) || ! version_compare( get_bloginfo( 'version' ), '5.0.0', '>=' ) ) { |
| 44 | return; |
| 45 | } |
| 46 | |
| 47 | $pluginVersion = Assets::getVersion(); |
| 48 | |
| 49 | wp_enqueue_script( 'wpp_gutenberg_jalali_calendar_editor_scripts', |
| 50 | Assets::url( 'js-admin/gutenberg-jalali-calendar.build.js' ), |
| 51 | array( |
| 52 | 'wp-plugins', |
| 53 | 'wp-i18n', |
| 54 | 'wp-compose', |
| 55 | 'wp-components', |
| 56 | 'wp-element', |
| 57 | 'wp-editor', |
| 58 | 'wp-edit-post', |
| 59 | 'wp-data', |
| 60 | 'wp-date' |
| 61 | ), |
| 62 | $pluginVersion, |
| 63 | [ 'in_footer' => true ] |
| 64 | ); |
| 65 | |
| 66 | wp_enqueue_style( |
| 67 | 'wpp_gutenberg_jalali_calendar_editor_styles', |
| 68 | Assets::url( 'css-admin/gutenberg-jalali-calendar.build.css' ), |
| 69 | array( 'wp-edit-blocks' ), $pluginVersion |
| 70 | ); |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Localize name of months after date picker enqueued |
| 75 | * |
| 76 | * @since 4.0.1 |
| 77 | */ |
| 78 | public function localizeMonthsName(): void { |
| 79 | $months_name = Names::getMonths(); |
| 80 | |
| 81 | // Remove first item (null string) from name of months array |
| 82 | array_shift( $months_name ); |
| 83 | |
| 84 | wp_localize_script( 'wpp_jalali_datepicker', 'WPP_I18N', |
| 85 | array( |
| 86 | 'months' => $months_name |
| 87 | ) |
| 88 | ); |
| 89 | } |
| 90 | |
| 91 | /** |
| 92 | * Fixes themes and plugins RTL style, they should be LTR |
| 93 | * |
| 94 | * @return void |
| 95 | * @since 2.0 |
| 96 | */ |
| 97 | public function fixCodeEditor(): void { |
| 98 | $pluginVersion = Assets::getVersion(); |
| 99 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 100 | |
| 101 | wp_enqueue_style( WP_PARSI_KEY_SLUG . '-admin-fix', Assets::url( 'css-admin/admin-fix' . $debugName . '.css' ) |
| 102 | , false, $pluginVersion ); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Change theme JSON data for global styles and settings. |
| 107 | * |
| 108 | * @param \WP_Theme_JSON_Data $themeJson Class to access and update the underlying data. |
| 109 | * |
| 110 | * @return \WP_Theme_JSON_Data |
| 111 | * |
| 112 | * @since 6.2 |
| 113 | */ |
| 114 | public function addFontToThemeJson( $themeJson ): \WP_Theme_JSON_Data { |
| 115 | if ( ! Settings::get( 'enable_fonts', false ) ) { |
| 116 | return $themeJson; |
| 117 | } |
| 118 | |
| 119 | $data = $themeJson->get_data(); |
| 120 | |
| 121 | $fontFamilies = $data['settings']['typography']['fontFamilies']['theme'] ?? []; |
| 122 | $newFont = array( |
| 123 | "name" => "Vazirmatn", |
| 124 | "slug" => "vazirmatn", |
| 125 | "fontFamily" => "Vazirmatn, sans-serif", |
| 126 | "fontFace" => [ |
| 127 | [ |
| 128 | "src" => [ |
| 129 | Assets::url( 'fonts/Vazirmatn-VariableFont_wght.woff2' ) |
| 130 | ], |
| 131 | "fontWeight" => "100 900", |
| 132 | "fontStyle" => "normal", |
| 133 | 'fontDisplay' => 'swap', |
| 134 | "fontFamily" => "Vazirmatn" |
| 135 | ] |
| 136 | ] |
| 137 | ); |
| 138 | |
| 139 | // Add new font |
| 140 | $data['settings']['typography']['fontFamilies']['theme'] = array_merge( $fontFamilies, array( $newFont ) ); |
| 141 | |
| 142 | $variableVazirmatnFont = "var:preset|font-family|vazirmatn"; |
| 143 | |
| 144 | // Body font family |
| 145 | if ( isset( $data['styles']['typography']['fontFamily'] ) ) { |
| 146 | $data['styles']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 147 | } else { |
| 148 | $data['styles']['typography'] = array( |
| 149 | "fontFamily" => $variableVazirmatnFont, |
| 150 | "fontSize" => "1.2rem", |
| 151 | "lineHeight" => "1.7", |
| 152 | "fontStyle" => "normal", |
| 153 | "fontWeight" => "400" |
| 154 | ); |
| 155 | } |
| 156 | |
| 157 | // Site title |
| 158 | if ( isset( $data['styles']['blocks']['core/site-title']['typography']['fontFamily'] ) ) { |
| 159 | $data['styles']['blocks']['core/site-title']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 160 | } else { |
| 161 | $data['styles']['blocks']['core/site-title'] = array( |
| 162 | "fontFamily" => $variableVazirmatnFont, |
| 163 | "fontSize" => "1.2rem", |
| 164 | "fontStyle" => "normal", |
| 165 | "fontWeight" => "600" |
| 166 | ); |
| 167 | } |
| 168 | |
| 169 | // Heading font family |
| 170 | if ( isset( $data['styles']['elements']['heading']['typography']['fontFamily'] ) ) { |
| 171 | $data['styles']['elements']['heading']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 172 | } else { |
| 173 | $data['styles']['elements']['heading'] = array( |
| 174 | "fontFamily" => $variableVazirmatnFont, |
| 175 | "fontSize" => "1.2rem", |
| 176 | "fontStyle" => "normal", |
| 177 | "fontWeight" => "600" |
| 178 | ); |
| 179 | } |
| 180 | |
| 181 | // Quote font family |
| 182 | if ( isset( $data['styles']['blocks']['core/quote']['typography']['fontFamily'] ) ) { |
| 183 | $data['styles']['blocks']['core/quote']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 184 | } |
| 185 | if ( isset( $data['styles']['blocks']['core/quote']['variations']['plain']['typography']['fontFamily'] ) ) { |
| 186 | $data['styles']['blocks']['core/quote']['variations']['plain']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 187 | } |
| 188 | if ( isset( $data['styles']['blocks']['core/pullquote']['typography']['fontFamily'] ) ) { |
| 189 | $data['styles']['blocks']['core/pullquote']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 190 | } |
| 191 | if ( isset( $data['styles']['blocks']['core/pullquote']['elements']['cite']['typography']['fontFamily'] ) ) { |
| 192 | $data['styles']['blocks']['core/pullquote']['elements']['cite']['typography']['fontFamily'] = $variableVazirmatnFont; |
| 193 | } |
| 194 | |
| 195 | return $themeJson->update_with( $data ); |
| 196 | } |
| 197 | |
| 198 | /** |
| 199 | * Fixes TinyMCE font |
| 200 | * |
| 201 | * @return void |
| 202 | * @since 2.0 |
| 203 | */ |
| 204 | public function fixTinyMceFont(): void { |
| 205 | if ( Settings::get( 'enable_fonts', false ) ) { |
| 206 | //$pluginVersion = Assets::getVersion(); |
| 207 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 208 | |
| 209 | // add_editor_style may fail when used with a URL, especially if the site is running on localhost. |
| 210 | // Reference: /wp-includes/block-editor.php, get_block_editor_theme_styles function, wp_remote_get |
| 211 | // add_editor_style( Assets::url( 'css-admin/tinymce-editor' . $debugName . '.css?v=' . $pluginVersion ) ); |
| 212 | |
| 213 | add_editor_style( '../../plugins/wp-parsidate/assets/css-admin/tinymce-editor' . $debugName . '.css' ); |
| 214 | } |
| 215 | } |
| 216 | |
| 217 | public function adminEnqueueScripts(): void { |
| 218 | global $pagenow; |
| 219 | $pluginVersion = Assets::getVersion(); |
| 220 | $debugName = WP_PARSI_DEBUG_MODE ? '' : '.min'; |
| 221 | |
| 222 | if ( Settings::get( 'enable_fonts', false ) ) { |
| 223 | wp_enqueue_style( WP_PARSI_KEY_SLUG . '-vazir-font', |
| 224 | Assets::url( 'css-admin/vazir-font' . $debugName . '.css' ), false, $pluginVersion ); |
| 225 | } |
| 226 | |
| 227 | wp_enqueue_script( WP_PARSI_KEY_SLUG . '-admin', Assets::url( 'js-admin/admin' . $debugName . '.js' ), false, |
| 228 | $pluginVersion, [ 'in_footer' => true ] ); |
| 229 | wp_localize_script( |
| 230 | WP_PARSI_KEY_SLUG . '-admin', |
| 231 | 'WPP_I18N', |
| 232 | array( 'months' => Names::getMonths() ) |
| 233 | ); |
| 234 | |
| 235 | if ( $pagenow == 'edit.php' ) { |
| 236 | $postType = Param::get( 'post_type', 'post' ); |
| 237 | |
| 238 | if ( ! apply_filters( 'disable_months_dropdown', false, $postType ) ) { |
| 239 | wp_add_inline_script( WP_PARSI_KEY_SLUG . '-admin', "jQuery(document).ready(function ($) {\$('select[name=m]').hide()})" ); |
| 240 | } |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 |