Skip to content

Commit

Permalink
UPD: Allow to use customizer and Google fonts loader outside of theme
Browse files Browse the repository at this point in the history
  • Loading branch information
MjHead committed Sep 29, 2017
1 parent fc4a674 commit 552fa07
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 46 deletions.
79 changes: 37 additions & 42 deletions modules/cherry-customizer/cherry-customizer.php
Expand Up @@ -113,9 +113,10 @@ class Cherry_Customizer {

/*
* $args = array(
* 'just_fonts' => false, // set to TRUE if you want use customizer only as fonts manager.
* 'prefix' => 'unique_prefix', // theme or plugin slug (*).
* 'capability' => 'edit_theme_options', // (default: `edit_theme_options`).
* 'type' => 'theme_mod', // `theme_mod` - for themes; `option` - for plugins (default: `theme_mod`).
* 'type' => 'theme_mod', // `theme_mod` - for themes; `option` - for plugins (default: `theme_mod`)
* 'options' => array(
* 'unique_panel_ID' => array(
* 'title' => esc_html__( 'Panel Title', 'text-domain' ),
Expand Down Expand Up @@ -155,29 +156,18 @@ class Cherry_Customizer {
* Cherry customizer class construct.
*/
public function __construct( $core, $args ) {

/**
* Cherry Customizer only works in WordPress 4.0 or later.
*/
if ( version_compare( $GLOBALS['wp_version'], '4.0', '<' ) ) {
return;
}

$this->type = ! empty( $args['type'] ) && $this->sanitize_type( $args['type'] ) ? $args['type'] : 'theme_mod';

if ( empty( $args['options'] ) || ( ( 'option' === $this->type ) && empty( $args['prefix'] ) ) ) {
return;
}

$this->prefix = $this->prepare_prefix( $args['prefix'] );
$this->capability = ! empty( $args['capability'] ) ? $args['capability'] : 'edit_theme_options';
$this->type = ! empty( $args['type'] ) && $this->sanitize_type( $args['type'] ) ? $args['type'] : 'theme_mod';
$this->options = $args['options'];
$this->core = $core;
$this->fonts = array();
$this->module_path = $args['module_path'];

add_action( 'customize_register', array( $this, 'register' ) );

// Prepare fonts data.
add_action( 'after_switch_theme', array( $this, 'init_fonts' ), 10 );
add_action( 'after_switch_theme', array( $this, 'add_options' ), 11 );
Expand All @@ -186,6 +176,27 @@ public function __construct( $core, $args ) {
add_action( 'switch_theme', array( $this, 'clear_fonts' ) );
add_action( 'upgrader_process_complete', array( $this, 'fire_clear_fonts' ), 10, 2 );

/**
* Fonts are loaded, abort if $args['just_fonts'] set to TRUE
*/
if ( isset( $args['just_fonts'] ) && true === $args['just_fonts'] ) {
return;
}

$this->type = ! empty( $args['type'] ) && $this->sanitize_type( $args['type'] ) ? $args['type'] : 'theme_mod';

if ( empty( $args['options'] ) || ( ( 'option' === $this->type ) && empty( $args['prefix'] ) ) ) {
return;
}

$this->prefix = $this->prepare_prefix( $args['prefix'] );
$this->capability = ! empty( $args['capability'] ) ? $args['capability'] : 'edit_theme_options';
$this->type = ! empty( $args['type'] ) && $this->sanitize_type( $args['type'] ) ? $args['type'] : 'theme_mod';
$this->options = $args['options'];


add_action( 'customize_register', array( $this, 'register' ) );

add_filter( 'cherry_customizer_get_core', array( $this, 'pass_core_into_control' ) );

$this->include_custom_controls();
Expand Down Expand Up @@ -877,13 +888,22 @@ public function get_image_types() {
* @since 1.0.0
*/
public function init_fonts() {

$inited = get_option( 'cherry_customiser_fonts_inited' );

if ( $inited ) {
return;
}

$fonts_data = $this->get_fonts_data();
$fonts_data = (array) $fonts_data;

foreach ( $fonts_data as $type => $file ) {
$data = $this->read_font_file( $file );
add_option( 'cherry_customiser_fonts_' . $type, $data );
}

add_option( 'cherry_customiser_fonts_inited', true );
}

/**
Expand Down Expand Up @@ -986,20 +1006,7 @@ public function read_font_file( $file ) {
* @return bool
*/
public function file_exists( $file ) {

if ( ! function_exists( 'WP_Filesystem' ) ) {
include_once( ABSPATH . '/wp-admin/includes/file.php' );
}

WP_Filesystem();
global $wp_filesystem;

if ( $wp_filesystem->abspath() ) {
return $wp_filesystem->exists( $file );

} else {
return file_exists( $file );
}
return file_exists( $file );
}

/**
Expand All @@ -1012,21 +1019,7 @@ public function file_exists( $file ) {
*/
public function get_file( $file ) {

if ( ! function_exists( 'WP_Filesystem' ) ) {
include_once( ABSPATH . '/wp-admin/includes/file.php' );
}

WP_Filesystem();
global $wp_filesystem;

$result = '';

if ( $wp_filesystem->abspath() ) {
$result = $wp_filesystem->get_contents( $file );

} else {
$result = Cherry_Toolkit::get_file( $file );
}
$result = Cherry_Toolkit::get_file( $file );

return $result;
}
Expand Down Expand Up @@ -1130,6 +1123,8 @@ public function clear_fonts() {
delete_option( 'cherry_customiser_fonts_' . $type );
}

delete_option( 'cherry_customiser_fonts_inited' );

$this->fonts = array();
}

Expand Down
17 changes: 13 additions & 4 deletions modules/cherry-google-fonts-loader/cherry-google-fonts-loader.php
Expand Up @@ -101,7 +101,16 @@ function __construct( $core, $args ) {
public function prepare_fonts() {

$font_url = $this->get_fonts_url();
wp_enqueue_style( 'cherry-google-fonts', $font_url );
wp_enqueue_style( 'cherry-google-fonts-' . $this->args['prefix'], $font_url );
}

/**
* Returns transient key.
*
* @return [type] [description]
*/
public function transient_key() {
return 'cherry_google_fonts_url_' . $this->args['prefix'];
}

/**
Expand All @@ -112,7 +121,7 @@ public function prepare_fonts() {
*/
public function get_fonts_url() {

$font_url = get_transient( 'cherry_google_fonts_url' );
$font_url = get_transient( $this->transient_key() );

if ( ! $font_url ) {

Expand All @@ -132,7 +141,7 @@ public function get_fonts_url() {

global $wp_customize;
if ( ! isset( $wp_customize ) ) {
set_transient( 'cherry_google_fonts_url', $font_url, WEEK_IN_SECONDS );
set_transient( $this->transient_key(), $font_url, WEEK_IN_SECONDS );
}
}

Expand Down Expand Up @@ -348,7 +357,7 @@ public function get_google_fonts() {
* @since 1.0.0
*/
public function reset_fonts_cache() {
delete_transient( 'cherry_google_fonts_url' );
delete_transient( $this->transient_key() );
}

/**
Expand Down

0 comments on commit 552fa07

Please sign in to comment.