Source: includes/frontend/class-enqueue.php

  1. <?php
  2. namespace um\frontend;
  3. if ( ! defined( 'ABSPATH' ) ) {
  4. exit;
  5. }
  6. /**
  7. * Class Enqueue.
  8. *
  9. * @package um\frontend
  10. */
  11. final class Enqueue extends \um\common\Enqueue {
  12. /**
  13. * @var string
  14. * @deprecated 2.8.0
  15. */
  16. public $js_baseurl = '';
  17. /**
  18. * @var string
  19. * @deprecated 2.8.0
  20. */
  21. public $css_baseurl = '';
  22. /**
  23. * Enqueue constructor.
  24. */
  25. public function __construct() {
  26. parent::__construct();
  27. add_action( 'init', array( &$this, 'scripts_enqueue_priority' ) );
  28. }
  29. /**
  30. * @since 2.1.3
  31. */
  32. public function scripts_enqueue_priority() {
  33. add_action( 'wp_enqueue_scripts', array( &$this, 'wp_enqueue_scripts' ), $this->get_priority() );
  34. }
  35. /**
  36. * @since 2.1.3
  37. * @return int
  38. */
  39. public function get_priority() {
  40. /**
  41. * Filters Ultimate Member frontend scripts enqueue priority.
  42. *
  43. * @since 1.3.x
  44. * @hook um_core_enqueue_priority
  45. *
  46. * @param {int} $priority Ultimate Member frontend scripts enqueue priority.
  47. *
  48. * @return {int} Ultimate Member frontend scripts enqueue priority.
  49. *
  50. * @example <caption>Change Ultimate Member frontend enqueue scripts priority.</caption>
  51. * function custom_um_core_enqueue_priority( $priority ) {
  52. * $priority = 101;
  53. * return $priority;
  54. * }
  55. * add_filter( 'um_core_enqueue_priority', 'custom_um_core_enqueue_priority' );
  56. */
  57. return apply_filters( 'um_core_enqueue_priority', 100 );
  58. }
  59. /**
  60. * Register JS scripts.
  61. *
  62. * @since 2.0.30
  63. */
  64. public function register_scripts() {
  65. $suffix = self::get_suffix();
  66. $libs_url = self::get_url( 'libs' );
  67. $js_url = self::get_url( 'js' );
  68. // Cropper.js
  69. wp_register_script( 'um_crop', $libs_url . 'cropper/cropper' . $suffix . '.js', array( 'jquery' ), '1.6.1', true );
  70. wp_register_style( 'um_crop', $libs_url . 'cropper/cropper' . $suffix . '.css', array(), '1.6.1' );
  71. wp_register_script( 'um_frontend_common', $js_url . 'common-frontend' . $suffix . '.js', array( 'um_common', 'um_crop' ), UM_VERSION, true );
  72. $um_common_variables = array();
  73. /**
  74. * Filters data array for localize frontend common scripts.
  75. *
  76. * @since 2.8.0
  77. * @hook um_frontend_common_js_variables
  78. *
  79. * @param {array} $variables Data to localize.
  80. *
  81. * @return {array} Data to localize.
  82. *
  83. * @example <caption>Add `my_custom_variable` to common frontend scripts to be callable via `um_frontend_common_variables.my_custom_variable` in JS.</caption>
  84. * function um_custom_frontend_common_js_variables( $variables ) {
  85. * $variables['{my_custom_variable}'] = '{my_custom_variable_value}';
  86. * return $variables;
  87. * }
  88. * add_filter( 'um_frontend_common_js_variables', 'um_custom_frontend_common_js_variables' );
  89. */
  90. $um_common_variables = apply_filters( 'um_frontend_common_js_variables', $um_common_variables );
  91. wp_localize_script( 'um_frontend_common', 'um_frontend_common_variables', $um_common_variables );
  92. // uploadFiles scripts + UM custom styles for uploader.
  93. wp_register_script( 'um_jquery_form', $libs_url . 'jquery-form/jquery-form' . $suffix . '.js', array( 'jquery' ), UM_VERSION, true );
  94. wp_register_script( 'um_fileupload', $libs_url . 'fileupload/fileupload.js', array( 'um_jquery_form' ), UM_VERSION, true );
  95. wp_register_script( 'um_functions', $js_url . 'um-functions' . $suffix . '.js', array( 'um_frontend_common', 'um_fileupload' ), UM_VERSION, true );
  96. wp_register_script( 'um_modal', $js_url . 'um-modal' . $suffix . '.js', array( 'um_frontend_common' ), UM_VERSION, true );
  97. wp_register_script( 'um_functions', $js_url . 'um-functions' . $suffix . '.js', array( 'um_frontend_common', 'jquery-masonry' ), UM_VERSION, true );
  98. wp_register_script( 'um_responsive', $js_url . 'um-responsive' . $suffix . '.js', array( 'um_functions' ), UM_VERSION, true );
  99. wp_register_script( 'um-gdpr', $js_url . 'um-gdpr' . $suffix . '.js', array( 'jquery' ), UM_VERSION, false );
  100. wp_register_script( 'um_conditional', $js_url . 'um-conditional' . $suffix . '.js', array( 'jquery', 'wp-hooks' ), UM_VERSION, true );
  101. wp_register_script( 'um_scripts', $js_url . 'um-scripts' . $suffix . '.js', array( 'um_frontend_common', 'um_conditional', self::$select2_handle, 'um_raty' ), UM_VERSION, true );
  102. $max_upload_size = wp_max_upload_size();
  103. if ( ! $max_upload_size ) {
  104. $max_upload_size = 0;
  105. }
  106. $localize_data = array(
  107. 'max_upload_size' => $max_upload_size,
  108. 'nonce' => wp_create_nonce( 'um-frontend-nonce' ),
  109. );
  110. /**
  111. * Filters data array for localize frontend scripts.
  112. *
  113. * @param {array} $variables Data to localize.
  114. *
  115. * @return {array} Data to localize.
  116. *
  117. * @since 2.0.0
  118. * @hook um_enqueue_localize_data
  119. *
  120. * @example <caption>Extend UM localized data.</caption>
  121. * function my_enqueue_localize_data( $variables ) {
  122. * // your code here
  123. * return $variables;
  124. * }
  125. * add_filter( 'um_enqueue_localize_data', 'my_enqueue_localize_data' );
  126. */
  127. $localize_data = apply_filters( 'um_enqueue_localize_data', $localize_data );
  128. wp_localize_script( 'um_scripts', 'um_scripts', $localize_data );
  129. wp_register_script( 'um_dropdown', $js_url . 'dropdown' . $suffix . '.js', array( 'jquery' ), UM_VERSION, true );
  130. wp_register_script( 'um_members', $js_url . 'um-members' . $suffix . '.js', array( 'jquery', 'wp-util', 'jquery-ui-slider', 'um_dropdown', 'wp-hooks', 'jquery-masonry', 'um_scripts' ), UM_VERSION, true );
  131. wp_register_script( 'um_profile', $js_url . 'um-profile' . $suffix . '.js', array( 'jquery', 'wp-util', 'wp-i18n', 'um_scripts' ), UM_VERSION, true );
  132. wp_set_script_translations( 'um_profile', 'ultimate-member' );
  133. /**
  134. * Filters account script dependencies.
  135. *
  136. * @since 2.1.8
  137. * @hook um_account_scripts_dependencies
  138. *
  139. * @param {array} $deps JS script dependencies.
  140. *
  141. * @return {array} JS script dependencies.
  142. *
  143. * @example <caption>Add `wp-util` as a dependencies script.</caption>
  144. * function um_custom_account_scripts_dependencies( $deps ) {
  145. * $deps[] = 'wp-util';
  146. * return $deps;
  147. * }
  148. * add_filter( 'um_account_scripts_dependencies', 'um_custom_account_scripts_dependencies' );
  149. */
  150. $account_deps = apply_filters( 'um_account_scripts_dependencies', array( 'jquery', 'wp-hooks', 'um_scripts' ) );
  151. wp_register_script( 'um_account', $js_url . 'um-account' . $suffix . '.js', $account_deps, UM_VERSION, true );
  152. }
  153. /**
  154. * Register styles.
  155. *
  156. * @since 2.0.30
  157. */
  158. public function register_styles() {
  159. $suffix = self::get_suffix();
  160. $css_url = self::get_url( 'css' );
  161. wp_register_style( 'um_fileupload', $css_url . 'um-fileupload' . $suffix . '.css', array(), UM_VERSION );
  162. //FontAwesome and FontIcons styles
  163. wp_register_style( 'um_rtl', $css_url . 'um.rtl' . $suffix . '.css', array(), UM_VERSION );
  164. wp_register_style( 'um_default_css', $css_url . 'um-old-default' . $suffix . '.css', array(), UM_VERSION );
  165. wp_register_style( 'um_modal', $css_url . 'um-modal' . $suffix . '.css', array(), UM_VERSION );
  166. wp_register_style( 'um_responsive', $css_url . 'um-responsive' . $suffix . '.css', array(), UM_VERSION );
  167. // Workaround when select2 deregistered (e.g. Woo + Impreza theme activated).
  168. $this->register_select2();
  169. $deps = array_merge( array( 'um_ui', 'um_tipsy', 'um_raty', 'select2', 'um_fileupload', 'um_common', 'um_responsive', 'um_modal' ), self::$fonticons_handlers );
  170. wp_register_style( 'um_styles', $css_url . 'um-styles' . $suffix . '.css', $deps, UM_VERSION );
  171. wp_register_style( 'um_members', $css_url . 'um-members' . $suffix . '.css', array( 'um_styles' ), UM_VERSION );
  172. // RTL styles.
  173. if ( is_rtl() ) {
  174. wp_style_add_data( 'um_members', 'rtl', true );
  175. wp_style_add_data( 'um_members', 'suffix', $suffix );
  176. }
  177. wp_register_style( 'um_profile', $css_url . 'um-profile' . $suffix . '.css', array( 'um_styles', 'um_crop' ), UM_VERSION );
  178. wp_register_style( 'um_account', $css_url . 'um-account' . $suffix . '.css', array( 'um_styles' ), UM_VERSION );
  179. wp_register_style( 'um_misc', $css_url . 'um-misc' . $suffix . '.css', array( 'um_styles' ), UM_VERSION );
  180. }
  181. /**
  182. * Enqueue scripts and styles.
  183. *
  184. * @since 2.0.0
  185. */
  186. public function wp_enqueue_scripts() {
  187. $this->register_scripts();
  188. $this->register_styles();
  189. $this->load_original();
  190. // rtl style
  191. if ( is_rtl() ) {
  192. wp_enqueue_style( 'um_rtl' );
  193. }
  194. global $post;
  195. if ( is_object( $post ) && has_shortcode( $post->post_content, 'ultimatemember' ) ) {
  196. wp_dequeue_script( 'jquery-form' );
  197. }
  198. //old settings before UM 2.0 CSS
  199. wp_enqueue_style( 'um_default_css' );
  200. $this->old_css_settings();
  201. }
  202. /**
  203. * @since 2.0.30
  204. */
  205. public function old_css_settings() {
  206. $uploads = wp_upload_dir();
  207. $upload_dir = $uploads['basedir'] . DIRECTORY_SEPARATOR . 'ultimatemember' . DIRECTORY_SEPARATOR;
  208. if ( file_exists( $upload_dir . 'um_old_settings.css' ) ) {
  209. wp_register_style( 'um_old_css', UM_URL . '../../uploads/ultimatemember/um_old_settings.css', array(), '2.0.0' );
  210. wp_enqueue_style( 'um_old_css' );
  211. }
  212. }
  213. /**
  214. * This will load original files (not minified)
  215. *
  216. * @since 2.0.0
  217. */
  218. public function load_original() {
  219. $this->load_modal();
  220. $this->load_css();
  221. $this->load_functions();
  222. $this->load_responsive();
  223. $this->load_customjs();
  224. }
  225. /**
  226. * Load plugin CSS
  227. *
  228. * @since 2.0.0
  229. */
  230. public function load_css() {
  231. wp_enqueue_style( 'um_styles' );
  232. wp_enqueue_style( 'um_profile' );
  233. wp_enqueue_style( 'um_account' );
  234. wp_enqueue_style( 'um_misc' );
  235. }
  236. /**
  237. * Load JS functions.
  238. *
  239. * @since 2.0.0
  240. */
  241. public function load_functions() {
  242. wp_enqueue_script( 'um_functions' );
  243. wp_enqueue_script( 'um-gdpr' );
  244. }
  245. /**
  246. * Load custom JS.
  247. *
  248. * @since 2.0.0
  249. */
  250. public function load_customjs() {
  251. wp_enqueue_script( 'um_conditional' );
  252. wp_enqueue_script( 'um_scripts' );
  253. wp_enqueue_script( 'um_profile' );
  254. wp_enqueue_script( 'um_account' );
  255. }
  256. /**
  257. * Load modal.
  258. *
  259. * @since 2.0.0
  260. */
  261. public function load_modal() {
  262. wp_enqueue_script( 'um_modal' );
  263. wp_enqueue_style( 'um_modal' );
  264. }
  265. /**
  266. * Load responsive styles.
  267. *
  268. * @since 2.0.0
  269. */
  270. public function load_responsive() {
  271. wp_enqueue_script( 'um_responsive' );
  272. wp_enqueue_style( 'um_responsive' );
  273. }
  274. /**
  275. * Include Google charts
  276. * @deprecated 2.8.0
  277. */
  278. public function load_google_charts() {
  279. }
  280. /**
  281. * Load fileupload JS
  282. * @deprecated 2.8.0
  283. */
  284. public function load_fileupload() {
  285. }
  286. /**
  287. * Load date & time picker
  288. * @deprecated 2.8.0
  289. */
  290. public function load_datetimepicker() {
  291. }
  292. /**
  293. * Load scrollbar
  294. * @deprecated 2.8.0
  295. */
  296. public function load_scrollbar() {
  297. }
  298. /**
  299. * Load crop script
  300. * @deprecated 2.8.0
  301. */
  302. public function load_imagecrop() {
  303. }
  304. }