WPSEO_Admin_Asset_Manager

This class registers all the necessary styles and scripts.

Defined (1)

The class is defined in the following location(s).

/admin/class-admin-asset-manager.php  
  1. class WPSEO_Admin_Asset_Manager { 
  2.  
  3. /** 
  4. * Prefix for naming the assets. 
  5. */ 
  6. const PREFIX = 'yoast-seo-'; 
  7.  
  8. /** 
  9. * Enqueues scripts. 
  10. * @param string $script The name of the script to enqueue. 
  11. */ 
  12. public function enqueue_script( $script ) { 
  13. wp_enqueue_script( self::PREFIX . $script ); 
  14.  
  15. /** 
  16. * Enqueues styles. 
  17. * @param string $style The name of the style to enqueue. 
  18. */ 
  19. public function enqueue_style( $style ) { 
  20. wp_enqueue_style( self::PREFIX . $style ); 
  21.  
  22. /** 
  23. * Registers scripts based on it's parameters. 
  24. * @param WPSEO_Admin_Asset $script The script to register. 
  25. */ 
  26. public function register_script( WPSEO_Admin_Asset $script ) { 
  27. wp_register_script( 
  28. self::PREFIX . $script->get_name(),  
  29. $script->get_url( WPSEO_Admin_Asset::TYPE_JS, WPSEO_FILE ),  
  30. $script->get_deps(),  
  31. $script->get_version(),  
  32. $script->is_in_footer() 
  33. ); 
  34.  
  35. /** 
  36. * Registers styles based on it's parameters. 
  37. * @param WPSEO_Admin_Asset $style The style to register. 
  38. */ 
  39. public function register_style( WPSEO_Admin_Asset $style ) { 
  40. wp_register_style( 
  41. self::PREFIX . $style->get_name(),  
  42. $style->get_url( WPSEO_Admin_Asset::TYPE_CSS, WPSEO_FILE ),  
  43. $style->get_deps(),  
  44. $style->get_version(),  
  45. $style->get_media() 
  46. ); 
  47.  
  48. /** 
  49. * Calls the functions that register scripts and styles with the scripts and styles to be registered as arguments. 
  50. */ 
  51. public function register_assets() { 
  52. $this->register_scripts( $this->scripts_to_be_registered() ); 
  53. $this->register_styles( $this->styles_to_be_registered() ); 
  54.  
  55. /** 
  56. * Registers all the scripts passed to it. 
  57. * @param array $scripts The scripts passed to it. 
  58. */ 
  59. public function register_scripts( $scripts ) { 
  60. foreach ( $scripts as $script ) { 
  61. $script = new WPSEO_Admin_Asset( $script ); 
  62. $this->register_script( $script ); 
  63.  
  64. /** 
  65. * Registers all the styles it recieves. 
  66. * @param array $styles Styles that need to be registerd. 
  67. */ 
  68. public function register_styles( $styles ) { 
  69. foreach ( $styles as $style ) { 
  70. $style = new WPSEO_Admin_Asset( $style ); 
  71. $this->register_style( $style ); 
  72.  
  73. /** 
  74. * A list of styles that shouldn't be registered but are needed in other locations in the plugin. 
  75. * @return array 
  76. */ 
  77. public function special_styles() { 
  78. $flat_version = $this->flatten_version( WPSEO_VERSION ); 
  79.  
  80. return array( 
  81. 'inside-editor' => new WPSEO_Admin_Asset( array( 
  82. 'name' => 'inside-editor',  
  83. 'src' => 'inside-editor-' . $flat_version,  
  84. ) ),  
  85. ); 
  86.  
  87. /** 
  88. * Flattens a version number for use in a filename 
  89. * @param string $version The original version number. 
  90. * @return string The flattened version number. 
  91. */ 
  92. public function flatten_version( $version ) { 
  93. $parts = explode( '.', $version ); 
  94.  
  95. if ( count( $parts ) === 2 ) { 
  96. $parts[] = '0'; 
  97.  
  98. return implode( '', $parts ); 
  99.  
  100. /** 
  101. * Returns the scripts that need to be registered. 
  102. * @TODO data format is not self-documenting. Needs explanation inline. R. 
  103. * @return array scripts that need to be registered. 
  104. */ 
  105. private function scripts_to_be_registered() { 
  106.  
  107. $select2_language = 'en'; 
  108. $user_locale = WPSEO_Utils::get_user_locale(); 
  109. $language = WPSEO_Utils::get_language( $user_locale ); 
  110.  
  111. if ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$user_locale}.js" ) ) { 
  112. $select2_language = $user_locale; // Chinese and some others use full locale. 
  113. elseif ( file_exists( WPSEO_PATH . "js/dist/select2/i18n/{$language}.js" ) ) { 
  114. $select2_language = $language; 
  115.  
  116. $flat_version = $this->flatten_version( WPSEO_VERSION ); 
  117.  
  118. return array( 
  119. array( 
  120. 'name' => 'admin-script',  
  121. 'src' => 'wp-seo-admin-' . $flat_version,  
  122. 'deps' => array( 
  123. 'jquery',  
  124. 'jquery-ui-core',  
  125. 'jquery-ui-progressbar',  
  126. self::PREFIX . 'select2',  
  127. self::PREFIX . 'select2-translations',  
  128. ),  
  129. ),  
  130. array( 
  131. 'name' => 'admin-media',  
  132. 'src' => 'wp-seo-admin-media-' . $flat_version,  
  133. 'deps' => array( 
  134. 'jquery',  
  135. 'jquery-ui-core',  
  136. ),  
  137. ),  
  138. array( 
  139. 'name' => 'bulk-editor',  
  140. 'src' => 'wp-seo-bulk-editor-' . $flat_version,  
  141. 'deps' => array( 'jquery' ),  
  142. ),  
  143. array( 
  144. 'name' => 'dismissible',  
  145. 'src' => 'wp-seo-dismissible-' . $flat_version,  
  146. 'deps' => array( 'jquery' ),  
  147. ),  
  148. array( 
  149. 'name' => 'admin-global-script',  
  150. 'src' => 'wp-seo-admin-global-' . $flat_version,  
  151. 'deps' => array( 'jquery' ),  
  152. ),  
  153. array( 
  154. 'name' => 'metabox',  
  155. 'src' => 'wp-seo-metabox-' . $flat_version,  
  156. 'deps' => array( 
  157. 'jquery',  
  158. 'jquery-ui-core',  
  159. 'jquery-ui-autocomplete',  
  160. self::PREFIX . 'select2',  
  161. self::PREFIX . 'select2-translations',  
  162. ),  
  163. 'in_footer' => false,  
  164. ),  
  165. array( 
  166. 'name' => 'featured-image',  
  167. 'src' => 'wp-seo-featured-image-' . $flat_version,  
  168. 'deps' => array( 
  169. 'jquery' 
  170. ),  
  171. ),  
  172. array( 
  173. 'name' => 'admin-gsc',  
  174. 'src' => 'wp-seo-admin-gsc-' . $flat_version,  
  175. 'deps' => array(),  
  176. 'in_footer' => false,  
  177. ),  
  178. array( 
  179. 'name' => 'post-scraper',  
  180. 'src' => 'wp-seo-post-scraper-' . $flat_version,  
  181. 'deps' => array( 
  182. self::PREFIX . 'replacevar-plugin',  
  183. self::PREFIX . 'shortcode-plugin',  
  184. 'wp-util',  
  185. ),  
  186. ),  
  187. array( 
  188. 'name' => 'term-scraper',  
  189. 'src' => 'wp-seo-term-scraper-' . $flat_version,  
  190. 'deps' => array( 
  191. self::PREFIX . 'replacevar-plugin',  
  192. ),  
  193. ),  
  194. array( 
  195. 'name' => 'replacevar-plugin',  
  196. 'src' => 'wp-seo-replacevar-plugin-' . $flat_version,  
  197. ),  
  198. array( 
  199. 'name' => 'shortcode-plugin',  
  200. 'src' => 'wp-seo-shortcode-plugin-' . $flat_version,  
  201. ),  
  202. array( 
  203. 'name' => 'recalculate',  
  204. 'src' => 'wp-seo-recalculate-' . $flat_version,  
  205. 'deps' => array( 
  206. 'jquery',  
  207. 'jquery-ui-core',  
  208. 'jquery-ui-progressbar',  
  209. ),  
  210. ),  
  211. array( 
  212. 'name' => 'primary-category',  
  213. 'src' => 'wp-seo-metabox-category-' . $flat_version,  
  214. 'deps' => array( 
  215. 'jquery',  
  216. 'wp-util',  
  217. ),  
  218. ),  
  219. array( 
  220. 'name' => 'select2',  
  221. 'src' => 'select2/select2.full',  
  222. 'suffix' => '.min',  
  223. 'deps' => array( 
  224. 'jquery',  
  225. ),  
  226. 'version' => '4.0.3',  
  227. ),  
  228. array( 
  229. 'name' => 'select2-translations',  
  230. 'src' => 'select2/i18n/' . $select2_language,  
  231. 'deps' => array( 
  232. 'jquery',  
  233. self::PREFIX . 'select2',  
  234. ),  
  235. 'version' => '4.0.3',  
  236. 'suffix' => '',  
  237. ),  
  238. array( 
  239. 'name' => 'configuration-wizard',  
  240. 'src' => 'configuration-wizard-' . $flat_version,  
  241. 'deps' => array( 
  242. 'jquery',  
  243. ),  
  244. ),  
  245. ); 
  246.  
  247. /** 
  248. * Returns the styles that need to be registered. 
  249. * @TODO data format is not self-documenting. Needs explanation inline. R. 
  250. * @return array styles that need to be registered. 
  251. */ 
  252. private function styles_to_be_registered() { 
  253. $flat_version = $this->flatten_version( WPSEO_VERSION ); 
  254.  
  255. return array( 
  256. array( 
  257. 'name' => 'admin-css',  
  258. 'src' => 'yst_plugin_tools-' . $flat_version,  
  259. 'deps' => array( self::PREFIX . 'toggle-switch' ),  
  260. ),  
  261. array( 
  262. 'name' => 'toggle-switch',  
  263. 'src' => 'toggle-switch-' . $flat_version,  
  264. ),  
  265. array( 
  266. 'name' => 'dismissible',  
  267. 'src' => 'wpseo-dismissible-' . $flat_version,  
  268. ),  
  269. array( 
  270. 'name' => 'alerts',  
  271. 'src' => 'alerts-' . $flat_version,  
  272. ),  
  273. array( 
  274. 'name' => 'edit-page',  
  275. 'src' => 'edit-page-' . $flat_version,  
  276. ),  
  277. array( 
  278. 'name' => 'featured-image',  
  279. 'src' => 'featured-image-' . $flat_version,  
  280. ),  
  281. array( 
  282. 'name' => 'metabox-css',  
  283. 'src' => 'metabox-' . $flat_version,  
  284. 'deps' => array( 
  285. self::PREFIX . 'select2',  
  286. ),  
  287. ),  
  288. array( 
  289. 'name' => 'wp-dashboard',  
  290. 'src' => 'dashboard-' . $flat_version,  
  291. ),  
  292. array( 
  293. 'name' => 'scoring',  
  294. 'src' => 'yst_seo_score-' . $flat_version,  
  295. ),  
  296. array( 
  297. 'name' => 'snippet',  
  298. 'src' => 'snippet-' . $flat_version,  
  299. ),  
  300. array( 
  301. 'name' => 'adminbar',  
  302. 'src' => 'adminbar-' . $flat_version,  
  303. ),  
  304. array( 
  305. 'name' => 'primary-category',  
  306. 'src' => 'metabox-primary-category-' . $flat_version,  
  307. ),  
  308. array( 
  309. 'name' => 'select2',  
  310. 'src' => 'dist/select2/select2',  
  311. 'suffix' => '.min',  
  312. 'version' => '4.0.1',  
  313. 'rtl' => false,  
  314. ),  
  315. array( 
  316. 'name' => 'kb-search',  
  317. 'src' => 'kb-search-' . $flat_version,  
  318. ),  
  319. array( 
  320. 'name' => 'help-center',  
  321. 'src' => 'help-center-' . $flat_version,  
  322. ),  
  323. array( 
  324. 'name' => 'admin-global',  
  325. 'src' => 'admin-global-' . $flat_version,  
  326. ),  
  327. array( 
  328. 'name' => 'yoast-components',  
  329. 'src' => 'yoast-components-' . $flat_version,  
  330. ),  
  331. array( 
  332. 'name' => 'extensions',  
  333. 'src' => 'yoast-extensions-' . $flat_version,  
  334. ),  
  335. );