/wp-content/plugins/beaver-builder-lite-version/classes/class-fl-builder-icons.php

https://bitbucket.org/madiha303/clickitplugins-web · PHP · 397 lines · 222 code · 60 blank · 115 comment · 50 complexity · 9e02e9cdc072834e0ac3b09ffa9e159b MD5 · raw file

  1. <?php
  2. /**
  3. * Helper class for working with icons.
  4. *
  5. * @since 1.4.6
  6. */
  7. final class FLBuilderIcons {
  8. /**
  9. * An array of data for each icon set.
  10. *
  11. * @since 1.4.6
  12. * @access private
  13. * @var array $sets
  14. */
  15. static private $sets = null;
  16. /**
  17. * Gets an array of data for core and custom icon sets.
  18. *
  19. * @since 1.4.6
  20. * @return array An array of data for each icon set.
  21. */
  22. static public function get_sets() {
  23. // Return the sets if already registered.
  24. if ( self::$sets ) {
  25. return self::$sets;
  26. }
  27. // Check to see if we should pull sets from the main site.
  28. if ( is_multisite() ) {
  29. $blog_id = defined( 'BLOG_ID_CURRENT_SITE' ) ? BLOG_ID_CURRENT_SITE : 1;
  30. $enabled_icons = get_option( '_fl_builder_enabled_icons' );
  31. if ( empty( $enabled_icons ) ) {
  32. switch_to_blog( $blog_id );
  33. }
  34. }
  35. // Register the icon sets.
  36. self::register_custom_sets();
  37. self::register_core_sets();
  38. // Revert to the current site if we pulled from the main site.
  39. if ( is_multisite() && empty( $enabled_icons ) ) {
  40. restore_current_blog();
  41. }
  42. // Filter the sets
  43. self::$sets = apply_filters( 'fl_builder_icon_sets', self::$sets );
  44. // Return the sets.
  45. return self::$sets;
  46. }
  47. /**
  48. * Gets an array of data for icon sets of the current
  49. * site on a multisite install.
  50. *
  51. * @since 1.4.6
  52. * @return array An array of data for each icon set.
  53. */
  54. static public function get_sets_for_current_site() {
  55. if ( ! is_multisite() ) {
  56. return self::get_sets();
  57. }
  58. // Store the original sets.
  59. $original_sets = self::$sets;
  60. // Register the icon sets.
  61. self::register_custom_sets();
  62. self::register_core_sets();
  63. // Get the new sets.
  64. $sets = self::$sets;
  65. // Revert to the original sets.
  66. self::$sets = $original_sets;
  67. // Return the sets.
  68. return $sets;
  69. }
  70. /**
  71. * Remove an icon set from the internal sets array.
  72. *
  73. * @since 1.4.6
  74. * @param string $key The key for the set to remove.
  75. * @return void
  76. */
  77. static public function remove_set( $key ) {
  78. if ( self::$sets && isset( self::$sets[ $key ] ) ) {
  79. unset( self::$sets[ $key ] );
  80. }
  81. }
  82. /**
  83. * Get the key for an icon set from the path to an icon set stylesheet.
  84. *
  85. * @since 1.4.6
  86. * @param string $path The path to retrieve a key for.
  87. * @return string The icon set key.
  88. */
  89. static public function get_key_from_path( $path ) {
  90. $sets = self::get_sets();
  91. foreach ( $sets as $key => $set ) {
  92. if ( $path == $set['path'] ) {
  93. return $key;
  94. }
  95. }
  96. }
  97. /**
  98. * Register core icon set data in the internal sets array.
  99. *
  100. * @since 1.4.6
  101. * @access private
  102. * @return void
  103. */
  104. static private function register_core_sets() {
  105. $enabled_icons = FLBuilderModel::get_enabled_icons();
  106. $core_sets = apply_filters( 'fl_builder_core_icon_sets', array(
  107. 'font-awesome' => array(
  108. 'name' => 'Font Awesome',
  109. 'prefix' => 'fa',
  110. ),
  111. 'foundation-icons' => array(
  112. 'name' => 'Foundation Icons',
  113. 'prefix' => '',
  114. ),
  115. 'dashicons' => array(
  116. 'name' => 'WordPress Dashicons',
  117. 'prefix' => 'dashicons dashicons-before',
  118. ),
  119. ) );
  120. // Add the core sets.
  121. foreach ( $core_sets as $set_key => $set_data ) {
  122. if ( is_admin() || in_array( $set_key, $enabled_icons ) ) {
  123. self::$sets[ $set_key ] = array(
  124. 'name' => $set_data['name'],
  125. 'prefix' => $set_data['prefix'],
  126. 'type' => 'core',
  127. );
  128. }
  129. }
  130. // Loop through core sets and add icons.
  131. foreach ( self::$sets as $set_key => $set_data ) {
  132. if ( 'core' == $set_data['type'] ) {
  133. $config_path = apply_filters( 'fl_builder_core_icon_set_config', FL_BUILDER_DIR . 'json/' . $set_key . '.json', $set_data );
  134. $icons = json_decode( file_get_contents( $config_path ) );
  135. self::$sets[ $set_key ]['icons'] = $icons;
  136. }
  137. }
  138. }
  139. /**
  140. * Register custom icon set data in the internal sets array.
  141. *
  142. * @since 1.4.6
  143. * @access private
  144. * @return void
  145. */
  146. static private function register_custom_sets() {
  147. // Get uploaded sets.
  148. $enabled_icons = FLBuilderModel::get_enabled_icons();
  149. $upload_info = FLBuilderModel::get_cache_dir( 'icons' );
  150. $folders = glob( $upload_info['path'] . '*' );
  151. // Make sure we have an array.
  152. if ( ! is_array( $folders ) ) {
  153. return;
  154. }
  155. // Loop through uploaded sets.
  156. foreach ( $folders as $folder ) {
  157. // Make sure we have a directory.
  158. if ( ! is_dir( $folder ) ) {
  159. continue;
  160. }
  161. $folder = trailingslashit( $folder );
  162. // This is an Icomoon font.
  163. if ( file_exists( $folder . 'selection.json' ) ) {
  164. $data = json_decode( file_get_contents( $folder . 'selection.json' ) );
  165. $key = basename( $folder );
  166. $url = str_ireplace( $upload_info['path'], $upload_info['url'], $folder );
  167. if ( isset( $data->icons ) ) {
  168. if ( is_admin() || in_array( $key, $enabled_icons ) ) {
  169. self::$sets[ $key ] = array(
  170. 'name' => $data->metadata->name,
  171. 'prefix' => '',
  172. 'type' => 'icomoon',
  173. 'path' => $folder,
  174. 'url' => $url,
  175. 'stylesheet' => $url . 'style.css',
  176. 'icons' => array(),
  177. );
  178. foreach ( $data->icons as $icon ) {
  179. $prefs = $data->preferences->fontPref;
  180. $postfix = isset( $prefs->postfix ) ? $prefs->postfix : '';
  181. if ( isset( $prefs->selector ) && 'class' == $prefs->selector ) {
  182. // @codingStandardsIgnoreLine
  183. $selector = trim( str_replace( '.', ' ', $prefs->classSelector ) ) . ' ';
  184. } else {
  185. $selector = '';
  186. }
  187. self::$sets[ $key ]['icons'][] = $selector . $prefs->prefix . $icon->properties->name . $postfix;
  188. }
  189. }
  190. }
  191. } // End if().
  192. elseif ( file_exists( $folder . 'config.json' ) ) {
  193. $data = json_decode( file_get_contents( $folder . 'config.json' ) );
  194. $key = basename( $folder );
  195. $name = empty( $data->name ) ? 'Fontello' : $data->name;
  196. $url = str_ireplace( $upload_info['path'], $upload_info['url'], $folder );
  197. $style = empty( $data->name ) ? 'fontello' : $data->name;
  198. // Append the date to the name?
  199. if ( empty( $data->name ) ) {
  200. $time = str_replace( 'icon-', '', $key );
  201. $date_format = get_option( 'date_format' );
  202. $time_format = get_option( 'time_format' );
  203. $date = date( $date_format . ' ' . $time_format );
  204. $name .= ' (' . $date . ')';
  205. }
  206. if ( isset( $data->glyphs ) ) {
  207. if ( is_admin() || in_array( $key, $enabled_icons ) ) {
  208. self::$sets[ $key ] = array(
  209. 'name' => $name,
  210. 'prefix' => '',
  211. 'type' => 'fontello',
  212. 'path' => $folder,
  213. 'url' => $url,
  214. 'stylesheet' => $url . 'css/' . $style . '.css',
  215. 'icons' => array(),
  216. );
  217. foreach ( $data->glyphs as $icon ) {
  218. if ( $data->css_use_suffix ) {
  219. self::$sets[ $key ]['icons'][] = $icon->css . $data->css_prefix_text;
  220. } else {
  221. self::$sets[ $key ]['icons'][] = $data->css_prefix_text . $icon->css;
  222. }
  223. }
  224. }
  225. }
  226. }
  227. }// End foreach().
  228. }
  229. /**
  230. * Enqueue the stylesheets for all icon sets.
  231. *
  232. * @since 1.4.6
  233. * @return void
  234. */
  235. static public function enqueue_all_custom_icons_styles() {
  236. $sets = self::get_sets();
  237. foreach ( $sets as $key => $data ) {
  238. // Don't enqueue core icons.
  239. if ( 'core' == $data['type'] ) {
  240. continue;
  241. }
  242. // Enqueue the custom icon styles.
  243. self::enqueue_custom_styles_by_key( $key );
  244. }
  245. }
  246. /**
  247. * Enqueue the stylesheet(s) for icons in a module.
  248. *
  249. * @since 1.4.6
  250. * @param object $module The module to enqueue for.
  251. * @return void
  252. */
  253. static public function enqueue_styles_for_module( $module ) {
  254. $fields = FLBuilderModel::get_settings_form_fields( $module->form );
  255. foreach ( $fields as $name => $field ) {
  256. if ( isset( $field['form'] ) ) {
  257. $form = FLBuilderModel::$settings_forms[ $field['form'] ];
  258. self::enqueue_styles_for_nested_module_form( $module, $form['tabs'], $name );
  259. } elseif ( 'icon' == $field['type'] && isset( $module->settings->$name ) ) {
  260. self::enqueue_styles_for_icon( $module->settings->$name );
  261. }
  262. }
  263. }
  264. /**
  265. * Enqueue the stylesheet(s) for icons in a nested form field.
  266. *
  267. * @since 1.4.6
  268. * @access private
  269. * @param object $module The module to enqueue for.
  270. * @param array $form The nested form.
  271. * @param string $setting The nested form setting key.
  272. * @return void
  273. */
  274. static private function enqueue_styles_for_nested_module_form( $module, $form, $setting ) {
  275. $fields = FLBuilderModel::get_settings_form_fields( $form );
  276. foreach ( $fields as $name => $field ) {
  277. if ( 'icon' == $field['type'] && ! empty( $module->settings->$setting ) ) {
  278. foreach ( $module->settings->$setting as $key => $val ) {
  279. if ( isset( $val->$name ) ) {
  280. self::enqueue_styles_for_icon( $val->$name );
  281. } elseif ( $name == $key && ! empty( $val ) ) {
  282. self::enqueue_styles_for_icon( $val );
  283. }
  284. }
  285. }
  286. }
  287. }
  288. /**
  289. * Enqueue the stylesheet for an icon.
  290. *
  291. * @since 1.4.6
  292. * @access private
  293. * @param string $icon The icon CSS classname.
  294. * @return void
  295. */
  296. static private function enqueue_styles_for_icon( $icon ) {
  297. do_action( 'fl_builder_enqueue_styles_for_icon', $icon );
  298. // Is this a core icon?
  299. if ( stristr( $icon, 'fa-' ) ) {
  300. wp_enqueue_style( 'font-awesome' );
  301. } elseif ( stristr( $icon, 'fi-' ) ) {
  302. wp_enqueue_style( 'foundation-icons' );
  303. } elseif ( stristr( $icon, 'dashicon' ) ) {
  304. wp_enqueue_style( 'dashicons' );
  305. } // End if().
  306. else {
  307. $sets = self::get_sets();
  308. foreach ( $sets as $key => $data ) {
  309. if ( in_array( $icon, $data['icons'] ) ) {
  310. self::enqueue_custom_styles_by_key( $key );
  311. }
  312. }
  313. }
  314. }
  315. /**
  316. * Enqueue the stylesheet for an icon set by key.
  317. *
  318. * @since 1.4.6
  319. * @access private
  320. * @param string $key The icon set key.
  321. * @return void
  322. */
  323. static private function enqueue_custom_styles_by_key( $key ) {
  324. if ( apply_filters( 'fl_builder_enqueue_custom_styles_by_key', true, $key ) ) {
  325. $sets = self::get_sets();
  326. if ( isset( $sets[ $key ] ) ) {
  327. $set = $sets[ $key ];
  328. if ( 'icomoon' == $set['type'] ) {
  329. wp_enqueue_style( $key, $set['stylesheet'], array(), FL_BUILDER_VERSION );
  330. }
  331. if ( 'fontello' == $set['type'] ) {
  332. wp_enqueue_style( $key, $set['stylesheet'], array(), FL_BUILDER_VERSION );
  333. }
  334. }
  335. }
  336. }
  337. }