Skip to content

Commit

Permalink
Merge pull request #46 from justcoded/develop
Browse files Browse the repository at this point in the history
REST Controller, Cronjob, auto target blank, disable gutenberg option
  • Loading branch information
aprokopenko authored Aug 22, 2019
2 parents 9cf8ed1 + 1001ce5 commit ef636b6
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 5 deletions.
178 changes: 178 additions & 0 deletions framework/Objects/Cronjob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
<?php

namespace JustCoded\WP\Framework\Objects;

/**
* Class Cronjob
*
* @package JustCoded\WP\Framework\Objects
*
* @method Cronjob instance() static
*/
abstract class Cronjob {
use Singleton;

/**
* Constant for single cron
*/
const FREQUENCY_ONCE = 'single';

/**
* Constant for hourly cron
*/
const FREQUENCY_HOURLY = 'hourly';

/**
* Constant for twicedaily cron
*/
const FREQUENCY_TWICEDAILY = 'twicedaily';

/**
* Constant for daily cron
*/
const FREQUENCY_DAILY = 'daily';

/**
* Cron id
*
* @var string
*/
protected $ID;

/**
* Start time.
*
* @var int|string
*/
protected $start;

/**
* Frequency name.
*
* @var string
*/
protected $frequency;

/**
* Interval in seconds.
*
* @var int
*/
protected $interval;

/**
* Cronjob constructor.
*
* @throws \Exception
*/
protected function __construct() {
if ( empty( $this->ID ) ) {
throw new \Exception( static::class . ' class: $ID property is required' );
}

// register action hook.
add_action( $this->ID, [ $this, 'handle' ] );

$this->cron_debug();

if ( static::FREQUENCY_ONCE !== $this->frequency ) {
// deactivation hook for repeatable event.
add_action( 'switch_theme', [ $this, 'deactivate' ] );
}

if ( ! in_array( $this->frequency, $this->get_standard_frequencies(), true ) ) {
if ( empty( $this->interval ) || ! is_numeric( $this->interval ) ) {
throw new \Exception( static::class . ' class: $interval property is required and must be numeric if you use a custom schedule' );
}

// register custom schedule.
add_filter( 'cron_schedules', [ $this, 'register_custom_schedule' ], 99, 1 );
}

// register cron.
add_action( 'init', [ $this, 'register' ] );
}

/**
* Register custom schedule.
*
* @param array $schedules Non-default schedule.
*
* @return array
*/
public function register_custom_schedule( $schedules ) {
$schedules[ $this->frequency ] = [
'interval' => $this->interval,
'display' => $this->frequency,
];

return $schedules;
}

/**
* Registers cron with interval
*/
public function register() {
if ( empty( $this->start ) ) {
$this->start = time();
} elseif ( ! is_numeric( $this->start ) && is_string( $this->start ) ) {
$this->start = strtotime( $this->start );
}

if ( static::FREQUENCY_ONCE === $this->frequency ) {
wp_schedule_single_event( $this->start, $this->ID );
} elseif ( ! wp_next_scheduled( $this->ID ) ) {
wp_schedule_event( $this->start, $this->frequency, $this->ID );
}
}

/**
* Deactivate cron on theme deactivate.
*/
public function deactivate() {
if ( $start = wp_next_scheduled( $this->ID ) ) {
wp_unschedule_event( $start, $this->ID );
}
}

/**
* Get all frequency
*
* @return array
*/
protected function get_standard_frequencies() {
return [
self::FREQUENCY_HOURLY,
self::FREQUENCY_TWICEDAILY,
self::FREQUENCY_DAILY,
];
}

/**
* Debug
*
* @return bool
*/
protected function cron_debug() {
if ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) {
return false;
}

if ( ! isset( $_GET['do_cron'] ) || $_GET['do_cron'] !== $this->ID ) {
return false;
}

remove_action( $this->ID, [ $this, 'handle' ] );
add_action( 'init', function () {
$this->handle();
wp_die( 'Finished cronjob <code>' . esc_attr__( $this->ID ) . '</code>' );
}, 99 );

return true;
}

/**
* Handle function
*/
abstract public function handle();
}
43 changes: 40 additions & 3 deletions framework/Supports/Autoptimize.php
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?php

namespace JustCoded\WP\Framework\Supports;

use JustCoded\WP\Framework\Objects\Singleton;
Expand Down Expand Up @@ -54,6 +55,7 @@ protected function __construct() {
}
}
add_filter( 'autoptimize_filter_html_before_minify', array( $this, 'add_dns_prefetch' ) );
add_filter( 'autoptimize_filter_html_before_minify', array( $this, 'add_external_links_target_rel' ) );
}

/**
Expand Down Expand Up @@ -105,9 +107,7 @@ public function add_dns_prefetch( $content ) {

foreach ( $tags_matches as $tag ) {
if ( preg_match( '#(http\:\/\/|https\:\/\/|\/\/)(([a-z0-9\_\-\.]+)\.([a-z0-9]{2,5}))\/#Usmi', $tag, $domain ) ) {
foreach ( $matches[0] as $tag ) {
$prefetch_domains[] = $domain[0];
}
$prefetch_domains[] = $domain[0];
}
}

Expand All @@ -130,6 +130,43 @@ public function add_dns_prefetch( $content ) {
return $content;
}

/**
* Add target and rel for links.
*
* @param string $content HTML content generated for the page.
*
* @return string
*/
public function add_external_links_target_rel( $content ) {
if ( ! preg_match_all( '#<a.*>.*<\/a>#Usmi', $content, $matches ) ) {
return $content;
}

preg_match( '#http(s)?\:\/\/(([a-z0-9\_\-\.]+)\.([a-z0-9]{2,5}))\/?#', site_url(), $site_domain );

foreach ( $matches[0] as $tag ) {
if ( preg_match( '#href="(http\:\/\/|https\:\/\/|\/\/)(([a-z0-9\_\-\.]+)\.([a-z0-9]{2,5}))"#Usmi', $tag, $domain ) ) {
if ( false !== strpos( $domain[2], $site_domain[2] ) ) {
continue;
}

$basic_tag = $tag;

if ( ! preg_match( '#rel="(.*)"#Usmi', $basic_tag, $rel_domain ) ) {
$tag = str_replace( $domain[0], $domain[0] . ' rel="noopener noreferrer"', $tag );
}

if ( ! preg_match( '#target="(.*)"#Usmi', $basic_tag, $target_domain ) ) {
$tag = str_replace( $domain[0], $domain[0] . ' target="_blank"', $tag );
}

$content = str_replace( $basic_tag, $tag, $content );
}
}

return $content;
}

/**
* Patch autoOptimize function to get correct script URLs.
* For MultiSite we have another folder with "cms" that in real URL
Expand Down
34 changes: 34 additions & 0 deletions framework/Theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,24 @@ abstract class Theme {
'caption',
);

/**
* Disable gutenberg for posts and custom post type.
*
* Set TRUE to disable it totally.
* Set ARRAY to disable only specific ones.
*
* @var array|bool $disable_gutenberg
*/
public $disable_gutenberg;

/**
* Init actions and hooks
*/
protected function __construct() {
$this->register_post_types();
$this->register_taxonomies();
$this->init_views_templates();
$this->register_api_endpoints();

/**
* Pretty standard theme hooks
Expand Down Expand Up @@ -259,6 +270,18 @@ public function theme_setup() {
add_theme_support( 'html5', $this->html5 );
}

if ( isset( $this->disable_gutenberg ) ) {
if ( is_bool( $this->disable_gutenberg ) && ! empty( $this->disable_gutenberg ) ) {
add_filter( 'use_block_editor_for_post_type', '__return_false', 10 );
}

if ( is_array( $this->disable_gutenberg ) ) {
add_filter( 'use_block_editor_for_post_type', function ( $use_block_editor, $post_type ) {
return ! in_array( $post_type, $this->disable_gutenberg, true );
}, 10, 2 );
}
}

/**
* Remove global content width.
* This can affect "width" attribute for images. If required can be overwritten in app file.
Expand Down Expand Up @@ -382,6 +405,17 @@ public function register_post_types() {
public function register_taxonomies() {
}

/**
* Register API Endpoints
* Usage:
* new \namespace\App\Endpoints\MyEndpoint();
*
* Each endpoint register it's own action hook
*/
public function register_api_endpoints() {

}

/**
* Adds loading of custom features provided by 3d-party plugins.
*/
Expand Down
Loading

0 comments on commit ef636b6

Please sign in to comment.