class-fl-builder-icons.php 9.9 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
<?php

/**
 * Helper class for working with icons.
 *
 * @since 1.4.6
 */

final class FLBuilderIcons {

	/**
	 * An array of data for each icon set.
	 *
	 * @since 1.4.6
	 * @access private
	 * @var array $sets
	 */
	static private $sets = null;

	/**
	 * Gets an array of data for core and custom icon sets.
	 *
	 * @since 1.4.6
	 * @return array An array of data for each icon set.
	 */
	static public function get_sets()
	{
		// Return the sets if already registered.
		if ( self::$sets ) {
			return self::$sets;
		}

		// Check to see if we should pull sets from the main site.
		if ( is_multisite()) {

			$blog_id		= defined( 'BLOG_ID_CURRENT_SITE' ) ? BLOG_ID_CURRENT_SITE : 1;
			$enabled_icons	= get_option( '_fl_builder_enabled_icons' );

			if ( empty( $enabled_icons ) ) {
				switch_to_blog( $blog_id );
			}
		}

		// Register the icon sets.
		self::register_custom_sets();
		self::register_core_sets();

		// Revert to the current site if we pulled from the main site.
		if ( is_multisite() && empty( $enabled_icons ) ) {
			restore_current_blog();
		}

		// Return the sets.
		return self::$sets;
	}

	/**
	 * Gets an array of data for icon sets of the current
	 * site on a multisite install.
	 *
	 * @since 1.4.6
	 * @return array An array of data for each icon set.
	 */
	static public function get_sets_for_current_site()
	{
		if ( ! is_multisite() ) {
			return self::get_sets();
		}

		// Store the original sets.
		$original_sets = self::$sets;

		// Register the icon sets.
		self::register_custom_sets();
		self::register_core_sets();

		// Get the new sets.
		$sets = self::$sets;

		// Revert to the original sets.
		self::$sets = $original_sets;

		// Return the sets.
		return $sets;
	}

	/**
	 * Remove an icon set from the internal sets array.
	 *
	 * @since 1.4.6
	 * @param string $key The key for the set to remove.
	 * @return void
	 */
	static public function remove_set( $key )
	{
		if ( self::$sets && isset( self::$sets[ $key ] ) ) {
			unset( self::$sets[ $key ] );
		}
	}

	/**
	 * Get the key for an icon set from the path to an icon set stylesheet.
	 *
	 * @since 1.4.6
	 * @param string $path The path to retrieve a key for.
	 * @return string The icon set key.
	 */
	static public function get_key_from_path( $path )
	{
		$sets = self::get_sets();

		foreach ( $sets as $key => $set ) {
			if ( $path == $set['path'] ) {
				return $key;
			}
		}
	}

	/**
	 * Register core icon set data in the internal sets array.
	 *
	 * @since 1.4.6
	 * @access private
	 * @return void
	 */
	static private function register_core_sets()
	{
		$enabled_icons = FLBuilderModel::get_enabled_icons();
		$core_sets = apply_filters( 'fl_builder_core_icon_sets', array(
			'font-awesome' => array(
				'name'	 => 'Font Awesome',
				'prefix' => 'fa'
			),
			'foundation-icons' => array(
				'name'	 => 'Foundation Icons',
				'prefix' => ''
			),
			'dashicons' => array(
				'name'	 => 'WordPress Dashicons',
				'prefix' => 'dashicons dashicons-before'
			)
		) );

		// Add the core sets.
		foreach ( $core_sets as $set_key => $set_data ) {
			if ( is_admin() || in_array( $set_key, $enabled_icons ) ) {
				self::$sets[ $set_key ] = array(
					'name'	 => $set_data['name'],
					'prefix' => $set_data['prefix'],
					'type'	 => 'core'
				);
			}
		}

		// Loop through core sets and add icons.
		foreach ( self::$sets as $set_key => $set_data ) {
			if ( 'core' == $set_data['type'] ) {
				$icons = json_decode( file_get_contents( FL_BUILDER_DIR . 'json/' . $set_key . '.json' ) );
				self::$sets[ $set_key ]['icons'] = $icons;
			}
		}
	}

	/**
	 * Register custom icon set data in the internal sets array.
	 *
	 * @since 1.4.6
	 * @access private
	 * @return void
	 */
	static private function register_custom_sets()
	{
		// Get uploaded sets.
		$enabled_icons = FLBuilderModel::get_enabled_icons();
		$upload_info   = FLBuilderModel::get_cache_dir( 'icons' );
		$folders	   = glob( $upload_info['path'] . '*' );

		// Make sure we have an array.
		if( ! is_array( $folders ) ) {
			return;
		}

		// Loop through uploaded sets.
		foreach ( $folders as $folder ) {

			// Make sure we have a directory.
			if ( ! is_dir( $folder ) ) {
				continue;
			}

			$folder = trailingslashit( $folder );

			// This is an Icomoon font.
			if ( file_exists( $folder . 'selection.json' ) ) {

				$data = json_decode( file_get_contents( $folder . 'selection.json' ) );
				$key  = basename( $folder );
				$url  = str_ireplace( $upload_info['path'], $upload_info['url'], $folder );

				if ( isset( $data->icons ) ) {

					if ( is_admin() || in_array( $key, $enabled_icons ) ) {

						self::$sets[ $key ] = array(
							'name'		 => $data->metadata->name,
							'prefix'	 => '',
							'type'		 => 'icomoon',
							'path'		 => $folder,
							'url'		 => $url,
							'stylesheet' => $url . 'style.css',
							'icons'		 => array()
						);

						foreach ( $data->icons as $icon ) {

							$prefs	 = $data->preferences->fontPref;
							$postfix = isset( $prefs->postfix ) ? $prefs->postfix : '';

							if ( isset( $prefs->selector ) && 'class' == $prefs->selector ) {
								$selector = trim( str_replace( '.', ' ', $prefs->classSelector ) ) . ' ';
							}
							else {
								$selector = '';
							}

							self::$sets[ $key ]['icons'][] = $selector . $prefs->prefix . $icon->properties->name . $postfix;
						}
					}
				}
			}
			// This is a Fontello font.
			else if ( file_exists( $folder . 'config.json' ) ) {

				$data  = json_decode( file_get_contents( $folder . 'config.json' ) );
				$key   = basename( $folder );
				$name  = empty( $data->name ) ? 'Fontello' : $data->name;
				$url   = str_ireplace( $upload_info['path'], $upload_info['url'], $folder );
				$style = empty( $data->name ) ? 'fontello' : $data->name;

				// Append the date to the name?
				if ( empty( $data->name ) ) {
					$time			= str_replace( 'icon-', '', $key );
					$date_format	= get_option( 'date_format' );
					$time_format	= get_option( 'time_format' );
					$date			= date( $date_format . ' ' . $time_format );
					$name		   .= ' (' . $date . ')';
				}

				if ( isset( $data->glyphs ) ) {

					if ( is_admin() || in_array( $key, $enabled_icons ) ) {

						self::$sets[ $key ] = array(
							'name'		 => $name,
							'prefix'	 => '',
							'type'		 => 'fontello',
							'path'		 => $folder,
							'url'		 => $url,
							'stylesheet' => $url . 'css/' . $style . '.css',
							'icons'		 => array()
						);

						foreach ( $data->glyphs as $icon ) {
							if ( $data->css_use_suffix ) {
								self::$sets[ $key ]['icons'][] = $icon->css . $data->css_prefix_text;
							}
							else {
								self::$sets[ $key ]['icons'][] = $data->css_prefix_text . $icon->css;
							}
						}
					}
				}
			}
		}
	}

	/**
	 * Enqueue the stylesheets for all icon sets.
	 *
	 * @since 1.4.6
	 * @return void
	 */
	static public function enqueue_all_custom_icons_styles()
	{
		$sets = self::get_sets();

		foreach ( $sets as $key => $data ) {

			// Don't enqueue core icons.
			if ( 'core' == $data['type'] ) {
				continue;
			}

			// Enqueue the custom icon styles.
			self::enqueue_custom_styles_by_key( $key );
		}
	}

	/**
	 * Enqueue the stylesheet(s) for icons in a module.
	 *
	 * @since 1.4.6
	 * @param object $module The module to enqueue for.
	 * @return void
	 */
	static public function enqueue_styles_for_module( $module )
	{
		$fields = FLBuilderModel::get_settings_form_fields( $module->form );

		foreach ( $fields as $name => $field ) {
			if ( isset( $field['form'] ) ) {
				$form = FLBuilderModel::$settings_forms[ $field['form'] ];
				self::enqueue_styles_for_nested_module_form( $module, $form['tabs'], $name );
			}
			else if ( $field['type'] == 'icon' && isset( $module->settings->$name ) ) {
				self::enqueue_styles_for_icon( $module->settings->$name );
			}
		}
	}

	/**
	 * Enqueue the stylesheet(s) for icons in a nested form field.
	 *
	 * @since 1.4.6
	 * @access private
	 * @param object $module The module to enqueue for.
	 * @param array $form The nested form.
	 * @param string $setting The nested form setting key.
	 * @return void
	 */
	static private function enqueue_styles_for_nested_module_form( $module, $form, $setting )
	{
		$fields = FLBuilderModel::get_settings_form_fields( $form );

		foreach ( $fields as $name => $field ) {
			if ( $field['type'] == 'icon' && ! empty( $module->settings->$setting ) ) {
				foreach ( $module->settings->$setting as $key => $val ) {
					if ( isset( $val->$name ) ) {
						self::enqueue_styles_for_icon( $val->$name );
					}
					else if( $name == $key && ! empty( $val ) ) {
						self::enqueue_styles_for_icon( $val );
					}
				}
			}
		}
	}

	/**
	 * Enqueue the stylesheet for an icon.
	 *
	 * @since 1.4.6
	 * @access private
	 * @param string $icon The icon CSS classname.
	 * @return void
	 */
	static private function enqueue_styles_for_icon( $icon )
	{
		do_action( 'fl_builder_enqueue_styles_for_icon', $icon );

		// Is this a core icon?
		if ( stristr( $icon, 'fa-' ) ) {
			wp_enqueue_style( 'font-awesome' );
		}
		else if ( stristr( $icon, 'fi-' ) ) {
			wp_enqueue_style( 'foundation-icons' );
		}
		else if ( stristr( $icon, 'dashicon' ) ) {
			wp_enqueue_style( 'dashicons' );
		}
		// It must be a custom icon.
		else {

			$sets = self::get_sets();

			foreach ( $sets as $key => $data ) {
				if ( in_array( $icon, $data['icons'] ) ) {
					self::enqueue_custom_styles_by_key( $key );
				}
			}
		}
	}

	/**
	 * Enqueue the stylesheet for an icon set by key.
	 *
	 * @since 1.4.6
	 * @access private
	 * @param string $key The icon set key.
	 * @return void
	 */
	static private function enqueue_custom_styles_by_key( $key )
	{
		if ( apply_filters( 'fl_builder_enqueue_custom_styles_by_key', true, $key ) ) {
			$sets = self::get_sets();

			if ( isset( $sets[ $key ] ) ) {

				$set = $sets[ $key ];

				if ( 'icomoon' == $set['type'] ) {
					wp_enqueue_style( $key, $set['stylesheet'], array(), FL_BUILDER_VERSION );
				}
				if ( 'fontello' == $set['type'] ) {
					wp_enqueue_style( $key, $set['stylesheet'], array(), FL_BUILDER_VERSION );
				}
			}
		}
	}
}