class-fl-builder-icons.php 11 KB

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