Skip to content

Commit

Permalink
Revert "Merge branch 'master' into develop"
Browse files Browse the repository at this point in the history
This reverts commit a59a781, reversing
changes made to b43235c.
  • Loading branch information
FrankM1 committed Nov 28, 2020
1 parent 00d982d commit dae51b0
Show file tree
Hide file tree
Showing 7 changed files with 435 additions and 11 deletions.
11 changes: 11 additions & 0 deletions includes/classes/core.php
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,10 @@ public function init() {
$this->dynamic_css = new Dynamic_CSS();
$this->frontend = new Frontend();
$this->markup = new Markup();
if ( is_admin() ) {
$this->metaboxes = new MetaBoxes();
}
$this->metabox_actions = new Metabox\Actions();
$this->options_instance = new Options();
$this->schema = new SchemaORG();
$this->theme = new Theme();
Expand Down Expand Up @@ -195,6 +199,11 @@ function _include_config() {
require_once get_theme_file_path( '/includes/config/theme.php' );
require_once get_theme_file_path( '/includes/config/frontend.php' );
require_once get_theme_file_path( '/includes/config/skin-css.php' );

if ( is_admin() ) {
require_once get_theme_file_path( '/includes/config/metabox/meta-boxes.php' );
}

require_once get_theme_file_path( '/includes/config/strings.php' );
}

Expand All @@ -203,8 +212,10 @@ function _include_classes() {
require_once get_theme_file_path( '/includes/classes/css/css-base.php' );
require_once get_theme_file_path( '/includes/classes/css/global-css-file.php' );
require_once get_theme_file_path( '/includes/classes/css/css-generate.php' );

require_once get_theme_file_path( '/includes/classes/customizer/customizer.php' );
require_once get_theme_file_path( '/includes/classes/customizer/controls.php' );
require_once get_theme_file_path( '/includes/classes/metaboxes/actions.php' );
}

function _include_function() {
Expand Down
86 changes: 76 additions & 10 deletions includes/classes/css/css-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@

abstract class CSS_File {

const FILE_BASE_DIR = '/analytica/css';

// %s: Base folder; %s: file name
const FILE_NAME_PATTERN = '%s/%s.css';

const CSS_STATUS_FILE = 'file';
const CSS_STATUS_INLINE = 'inline';
const CSS_STATUS_EMPTY = 'empty';

Expand Down Expand Up @@ -36,8 +42,19 @@ abstract public function get_name();
* CSS_File constructor.
*/
public function __construct() {
if ( $this->use_external_file() ) {
$this->set_path_and_url();
}
}

/**
* @since 1.0.0
* @access protected
*/
protected function use_external_file() {
return 'internal' !== analytica_get_option( 'site-css-print-method' );
}

public function update() {
$this->parse_css();

Expand All @@ -47,16 +64,45 @@ public function update() {
];

if ( empty( $this->css ) ) {
$this->delete();

$meta['status'] = self::CSS_STATUS_EMPTY;
$meta['css'] = '';
} else {
$meta['status'] = self::CSS_STATUS_INLINE;
$meta['css'] = $this->css;
}

$file_created = false;
$use_external_file = $this->use_external_file();

if ( $use_external_file && wp_is_writable( dirname( $this->path ) ) ) {
global $wp_filesystem;

// Instantiate the Wordpress filesystem.
if ( empty( $wp_filesystem ) ) {
require_once( ABSPATH . '/wp-admin/includes/file.php' );
WP_Filesystem();
}

// Since we've already checked if the file is writable in the wp_is_writable()
// it's safe to continue without any additional checks as to the validity of the file.
$file_created = $wp_filesystem->put_contents( $this->path, $this->css, FS_CHMOD_FILE );
}

if ( $file_created ) {
$meta['status'] = self::CSS_STATUS_FILE;
} else {
$meta['status'] = self::CSS_STATUS_INLINE;
$meta['css'] = $this->css;
}
}

$this->update_meta( $meta );
}

public function delete() {
if ( file_exists( $this->path ) ) {
unlink( $this->path );
}
}

public function enqueue() {
$meta = $this->get_meta();

Expand All @@ -71,12 +117,22 @@ public function enqueue() {
$meta = $this->get_meta();
}

$dep = $this->get_inline_dependency();
// If the dependency has already been printed ( like a template in footer )
if ( wp_styles()->query( $dep, 'done' ) ) {
echo '<style>' . $this->get_css() . '</style>'; // XSS ok.
} else {
wp_add_inline_style( $dep , $this->get_css() );
if ( self::CSS_STATUS_INLINE === $meta['status'] ) {
$dep = $this->get_inline_dependency();
// If the dependency has already been printed ( like a template in footer )
if ( wp_styles()->query( $dep, 'done' ) ) {
echo '<style>' . $this->get_css() . '</style>'; // XSS ok.
} else {
wp_add_inline_style( $dep , $meta['css'] );
}
} else {

if ( ! file_exists( $this->path ) && current_user_can( 'edit_posts' ) ) {
$this->update();
$meta = $this->get_meta();
}

wp_enqueue_style( $this->get_file_handle_id(), $this->url, $this->get_enqueue_dependencies(), $meta['time'] );
}
}

Expand Down Expand Up @@ -156,6 +212,16 @@ protected function is_update_required() {
return false;
}

private function set_path_and_url() {
$wp_upload_dir = wp_upload_dir( null, false );

$relative_path = sprintf( self::FILE_NAME_PATTERN, self::FILE_BASE_DIR, $this->get_file_name() );

$this->path = $wp_upload_dir['basedir'] . $relative_path;

$this->url = set_url_scheme( $wp_upload_dir['baseurl'] . $relative_path );
}

private function parse_css() {
$this->render_css();
do_action( 'analytica/' . $this->get_name() . '-css-file/parse', $this );
Expand Down
25 changes: 24 additions & 1 deletion includes/classes/css/css-generate.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ class CSS_Generate {
public $fonts;

public function __construct() {

// Create the css directory if it's not exist
$wp_upload_dir = wp_upload_dir( null, false );

$css_path = $wp_upload_dir['basedir'] . CSS_File::FILE_BASE_DIR;

if ( ! is_dir( $css_path ) ) {
wp_mkdir_p( $css_path );
}

add_action( 'analytica/global-css-file/parse', [ $this, 'add_css' ] );

add_action( 'analytica_after_theme_is_activated', [ $this, 'update_css' ], 90 );
Expand Down Expand Up @@ -53,7 +63,20 @@ public function clear_cache() {
global $wpdb;

$wpdb->delete( $wpdb->options, [ 'option_name' => Global_CSS_File::META_KEY ] );


// Delete files
$wp_upload_dir = wp_upload_dir( null, false );

$path = sprintf( '%s%s%s*', $wp_upload_dir['basedir'], CSS_File::FILE_BASE_DIR, '/' );

foreach ( glob( $path ) as $file ) {
$deleted = unlink( $file );

if ( ! $deleted ) {
$errors['files'] = esc_html__( 'Cannot delete files cache', 'analytica' );
}
}

return $errors;
}

Expand Down
46 changes: 46 additions & 0 deletions includes/classes/metaboxes/actions.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<?php
namespace Analytica\Metabox;

/**
* Analytica Meta Box Operations
*
* @package Analytica
* @author Analytica
* @copyright Copyright (c) 2018, Analytica
* @link https://qazana.net/
* @since Analytica 1.0.0
*/

/**
* Meta Box
*/
class Actions {

/**
* Constructor
*/
public function __construct() {
add_action( 'wp', array( $this, 'add_action' ) );
}

/**
* Metabox Hooks
*/
function add_action() {
if ( is_singular() ) {
add_filter( 'analytica_site_header_is_active', array( $this, 'site_header' ) );
}
}

/**
* Site Header
*/
function site_header( $retval ) {

if ( 'disabled' == get_post_meta( get_the_ID(), '_analytica_site_header', true ) ) {
$retval = false;
}

return $retval;
}
}
12 changes: 12 additions & 0 deletions includes/config/customizer/90-utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,18 @@
function analytica_get_customizer_utilities( $controls ) {

$new_controls = [
[
'id' => 'site-css-print-method',
'section' => 'utilities',
'type' => 'radio-buttonset',
'title' => esc_html__( 'CSS Print Method' , 'analytica' ),
'default' => 'external',
'options' => [
'external' => esc_html__( 'External File', 'analytica' ),
'internal' => esc_html__( 'Internal Embedding', 'analytica' ),
],
],

[
'id' => 'site-settings-update-time',
'title' => 'Time',
Expand Down
Loading

0 comments on commit dae51b0

Please sign in to comment.