/wp-content/plugins/wordpress-seo/admin/class-admin-asset-manager.php

https://gitlab.com/najomie/fit-hippie · PHP · 352 lines · 276 code · 15 blank · 61 comment · 1 complexity · 47ba782bd60d34fdbf29e6a5393f96a4 MD5 · raw file

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