Opal-Estate-Pro/inc/vendors/cmb2-plugins/custom-fields/iconpicker/iconpicker.php

91 lines
2.9 KiB
PHP
Raw Normal View History

2019-09-26 08:28:57 +02:00
<?php
/**
* Opalestate_Field_Iconpicker
*
* @package opalestate
* @author Opal Team <info@wpopal.com >
*/
if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly
}
class Opalestate_Field_Iconpicker {
/**
* Current version number
*/
const VERSION = '1.0.0';
/**
2019-09-27 08:57:08 +02:00
* Initialize the plugin by hooking into CMB2.
2019-09-26 08:28:57 +02:00
*/
2019-09-26 09:06:11 +02:00
public function __construct() {
add_filter( 'cmb2_render_opal_iconpicker', [ $this, 'render_iconpicker' ], 10, 5 );
add_filter( 'cmb2_sanitize_opal_iconpicker', [ $this, 'sanitize' ], 10, 4 );
}
public function get_icons() {
$fontawesome_key = 'opalestate_fontawesome_data';
2019-09-26 08:28:57 +02:00
2019-09-26 09:06:11 +02:00
$icon_data = [];
2019-09-27 08:57:08 +02:00
2019-09-26 09:06:11 +02:00
if ( false === ( $fontawesome_icons = get_transient( $fontawesome_key ) ) ) {
2019-09-27 08:57:08 +02:00
$fontawesome = new Opalestate_Iconpicker_Fontawesome();
2019-09-26 09:06:11 +02:00
$fontawesome_icons = $fontawesome->get_icons();
set_transient( $fontawesome_key, $fontawesome_icons, 24 * 7 * HOUR_IN_SECONDS );
}
2019-09-27 08:57:08 +02:00
$icon_data = array_merge( $icon_data, $fontawesome_icons );
return apply_filters( 'opalestate_get_font_data', $icon_data );
2019-09-26 08:28:57 +02:00
}
/**
* Render field.
*/
2019-09-26 09:06:11 +02:00
public function render_iconpicker( $field, $field_escaped_value, $field_object_id, $field_object_type, $field_type_object ) {
$this->setup_admin_scripts();
2019-09-26 08:28:57 +02:00
2019-09-27 08:57:08 +02:00
$output = sprintf(
'<select id="%1$s" class="opalestate-iconpicker" name="%2$s">',
sanitize_key( $field->args['_id'] ),
esc_attr( $field->args['_id'] )
);
foreach ( $this->get_icons() as $icon_item ) {
$full_icon_class = $icon_item['prefix'] . ' ' . $icon_item['class'];
$output .= '<option value="' . $full_icon_class . '" ' . selected( $full_icon_class, $field->escaped_value(), false ) . '>' . esc_html( $icon_item['class'] ) . '</option>';
}
$output .= '</select>';
echo '<p class="description">' . $field->args( 'description' ) . '</p>';
echo $output;
2019-09-26 08:28:57 +02:00
}
/**
* Sanitize data.
*/
2019-09-26 09:06:11 +02:00
public function sanitize( $override_value, $value, $object_id, $field_args ) {
2019-09-27 08:57:08 +02:00
return sanitize_text_field( $value );
2019-09-26 08:28:57 +02:00
}
/**
* Enqueue scripts and styles.
*/
2019-09-26 09:06:11 +02:00
public function setup_admin_scripts() {
2019-09-27 08:57:08 +02:00
wp_register_style( 'fontawesome', OPALESTATE_PLUGIN_URL . 'assets/3rd/fontawesome/css/all.min.css', null, '5.11.2', false );
2019-09-26 08:28:57 +02:00
// Iconpicker.
wp_register_style( 'fonticonpicker', plugins_url( 'assets/css/jquery.fonticonpicker.min.css', __FILE__ ), [], self::VERSION );
2019-09-27 08:57:08 +02:00
wp_register_style( 'fonticonpicker-grey-theme', plugins_url( 'assets/themes/grey-theme/jquery.fonticonpicker.grey.min.css', __FILE__ ), [ 'fontawesome' ], self::VERSION );
2019-09-26 08:28:57 +02:00
wp_enqueue_style( 'fonticonpicker' );
wp_enqueue_style( 'fonticonpicker-grey-theme' );
2019-09-27 08:57:08 +02:00
wp_enqueue_script( 'fonticonpicker', plugins_url( 'assets/js/jquery.fonticonpicker.min.js', __FILE__ ), [], '2.0.0' );
wp_enqueue_script( 'opalestate-fonticonpicker', plugins_url( 'assets/js/script.js', __FILE__ ), [ 'fonticonpicker' ], self::VERSION );
2019-09-26 08:28:57 +02:00
}
}
2019-09-26 09:06:11 +02:00
new Opalestate_Field_Iconpicker();