Skip to content

Commit ef636b6

Browse files
authored
Merge pull request #46 from justcoded/develop
REST Controller, Cronjob, auto target blank, disable gutenberg option
2 parents 9cf8ed1 + 1001ce5 commit ef636b6

File tree

6 files changed

+428
-5
lines changed

6 files changed

+428
-5
lines changed

framework/Objects/Cronjob.php

Lines changed: 178 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,178 @@
1+
<?php
2+
3+
namespace JustCoded\WP\Framework\Objects;
4+
5+
/**
6+
* Class Cronjob
7+
*
8+
* @package JustCoded\WP\Framework\Objects
9+
*
10+
* @method Cronjob instance() static
11+
*/
12+
abstract class Cronjob {
13+
use Singleton;
14+
15+
/**
16+
* Constant for single cron
17+
*/
18+
const FREQUENCY_ONCE = 'single';
19+
20+
/**
21+
* Constant for hourly cron
22+
*/
23+
const FREQUENCY_HOURLY = 'hourly';
24+
25+
/**
26+
* Constant for twicedaily cron
27+
*/
28+
const FREQUENCY_TWICEDAILY = 'twicedaily';
29+
30+
/**
31+
* Constant for daily cron
32+
*/
33+
const FREQUENCY_DAILY = 'daily';
34+
35+
/**
36+
* Cron id
37+
*
38+
* @var string
39+
*/
40+
protected $ID;
41+
42+
/**
43+
* Start time.
44+
*
45+
* @var int|string
46+
*/
47+
protected $start;
48+
49+
/**
50+
* Frequency name.
51+
*
52+
* @var string
53+
*/
54+
protected $frequency;
55+
56+
/**
57+
* Interval in seconds.
58+
*
59+
* @var int
60+
*/
61+
protected $interval;
62+
63+
/**
64+
* Cronjob constructor.
65+
*
66+
* @throws \Exception
67+
*/
68+
protected function __construct() {
69+
if ( empty( $this->ID ) ) {
70+
throw new \Exception( static::class . ' class: $ID property is required' );
71+
}
72+
73+
// register action hook.
74+
add_action( $this->ID, [ $this, 'handle' ] );
75+
76+
$this->cron_debug();
77+
78+
if ( static::FREQUENCY_ONCE !== $this->frequency ) {
79+
// deactivation hook for repeatable event.
80+
add_action( 'switch_theme', [ $this, 'deactivate' ] );
81+
}
82+
83+
if ( ! in_array( $this->frequency, $this->get_standard_frequencies(), true ) ) {
84+
if ( empty( $this->interval ) || ! is_numeric( $this->interval ) ) {
85+
throw new \Exception( static::class . ' class: $interval property is required and must be numeric if you use a custom schedule' );
86+
}
87+
88+
// register custom schedule.
89+
add_filter( 'cron_schedules', [ $this, 'register_custom_schedule' ], 99, 1 );
90+
}
91+
92+
// register cron.
93+
add_action( 'init', [ $this, 'register' ] );
94+
}
95+
96+
/**
97+
* Register custom schedule.
98+
*
99+
* @param array $schedules Non-default schedule.
100+
*
101+
* @return array
102+
*/
103+
public function register_custom_schedule( $schedules ) {
104+
$schedules[ $this->frequency ] = [
105+
'interval' => $this->interval,
106+
'display' => $this->frequency,
107+
];
108+
109+
return $schedules;
110+
}
111+
112+
/**
113+
* Registers cron with interval
114+
*/
115+
public function register() {
116+
if ( empty( $this->start ) ) {
117+
$this->start = time();
118+
} elseif ( ! is_numeric( $this->start ) && is_string( $this->start ) ) {
119+
$this->start = strtotime( $this->start );
120+
}
121+
122+
if ( static::FREQUENCY_ONCE === $this->frequency ) {
123+
wp_schedule_single_event( $this->start, $this->ID );
124+
} elseif ( ! wp_next_scheduled( $this->ID ) ) {
125+
wp_schedule_event( $this->start, $this->frequency, $this->ID );
126+
}
127+
}
128+
129+
/**
130+
* Deactivate cron on theme deactivate.
131+
*/
132+
public function deactivate() {
133+
if ( $start = wp_next_scheduled( $this->ID ) ) {
134+
wp_unschedule_event( $start, $this->ID );
135+
}
136+
}
137+
138+
/**
139+
* Get all frequency
140+
*
141+
* @return array
142+
*/
143+
protected function get_standard_frequencies() {
144+
return [
145+
self::FREQUENCY_HOURLY,
146+
self::FREQUENCY_TWICEDAILY,
147+
self::FREQUENCY_DAILY,
148+
];
149+
}
150+
151+
/**
152+
* Debug
153+
*
154+
* @return bool
155+
*/
156+
protected function cron_debug() {
157+
if ( ! defined( 'WP_DEBUG' ) || false === WP_DEBUG ) {
158+
return false;
159+
}
160+
161+
if ( ! isset( $_GET['do_cron'] ) || $_GET['do_cron'] !== $this->ID ) {
162+
return false;
163+
}
164+
165+
remove_action( $this->ID, [ $this, 'handle' ] );
166+
add_action( 'init', function () {
167+
$this->handle();
168+
wp_die( 'Finished cronjob <code>' . esc_attr__( $this->ID ) . '</code>' );
169+
}, 99 );
170+
171+
return true;
172+
}
173+
174+
/**
175+
* Handle function
176+
*/
177+
abstract public function handle();
178+
}

framework/Supports/Autoptimize.php

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
<?php
2+
23
namespace JustCoded\WP\Framework\Supports;
34

45
use JustCoded\WP\Framework\Objects\Singleton;
@@ -54,6 +55,7 @@ protected function __construct() {
5455
}
5556
}
5657
add_filter( 'autoptimize_filter_html_before_minify', array( $this, 'add_dns_prefetch' ) );
58+
add_filter( 'autoptimize_filter_html_before_minify', array( $this, 'add_external_links_target_rel' ) );
5759
}
5860

5961
/**
@@ -105,9 +107,7 @@ public function add_dns_prefetch( $content ) {
105107

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

@@ -130,6 +130,43 @@ public function add_dns_prefetch( $content ) {
130130
return $content;
131131
}
132132

133+
/**
134+
* Add target and rel for links.
135+
*
136+
* @param string $content HTML content generated for the page.
137+
*
138+
* @return string
139+
*/
140+
public function add_external_links_target_rel( $content ) {
141+
if ( ! preg_match_all( '#<a.*>.*<\/a>#Usmi', $content, $matches ) ) {
142+
return $content;
143+
}
144+
145+
preg_match( '#http(s)?\:\/\/(([a-z0-9\_\-\.]+)\.([a-z0-9]{2,5}))\/?#', site_url(), $site_domain );
146+
147+
foreach ( $matches[0] as $tag ) {
148+
if ( preg_match( '#href="(http\:\/\/|https\:\/\/|\/\/)(([a-z0-9\_\-\.]+)\.([a-z0-9]{2,5}))"#Usmi', $tag, $domain ) ) {
149+
if ( false !== strpos( $domain[2], $site_domain[2] ) ) {
150+
continue;
151+
}
152+
153+
$basic_tag = $tag;
154+
155+
if ( ! preg_match( '#rel="(.*)"#Usmi', $basic_tag, $rel_domain ) ) {
156+
$tag = str_replace( $domain[0], $domain[0] . ' rel="noopener noreferrer"', $tag );
157+
}
158+
159+
if ( ! preg_match( '#target="(.*)"#Usmi', $basic_tag, $target_domain ) ) {
160+
$tag = str_replace( $domain[0], $domain[0] . ' target="_blank"', $tag );
161+
}
162+
163+
$content = str_replace( $basic_tag, $tag, $content );
164+
}
165+
}
166+
167+
return $content;
168+
}
169+
133170
/**
134171
* Patch autoOptimize function to get correct script URLs.
135172
* For MultiSite we have another folder with "cms" that in real URL

framework/Theme.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,13 +113,24 @@ abstract class Theme {
113113
'caption',
114114
);
115115

116+
/**
117+
* Disable gutenberg for posts and custom post type.
118+
*
119+
* Set TRUE to disable it totally.
120+
* Set ARRAY to disable only specific ones.
121+
*
122+
* @var array|bool $disable_gutenberg
123+
*/
124+
public $disable_gutenberg;
125+
116126
/**
117127
* Init actions and hooks
118128
*/
119129
protected function __construct() {
120130
$this->register_post_types();
121131
$this->register_taxonomies();
122132
$this->init_views_templates();
133+
$this->register_api_endpoints();
123134

124135
/**
125136
* Pretty standard theme hooks
@@ -259,6 +270,18 @@ public function theme_setup() {
259270
add_theme_support( 'html5', $this->html5 );
260271
}
261272

273+
if ( isset( $this->disable_gutenberg ) ) {
274+
if ( is_bool( $this->disable_gutenberg ) && ! empty( $this->disable_gutenberg ) ) {
275+
add_filter( 'use_block_editor_for_post_type', '__return_false', 10 );
276+
}
277+
278+
if ( is_array( $this->disable_gutenberg ) ) {
279+
add_filter( 'use_block_editor_for_post_type', function ( $use_block_editor, $post_type ) {
280+
return ! in_array( $post_type, $this->disable_gutenberg, true );
281+
}, 10, 2 );
282+
}
283+
}
284+
262285
/**
263286
* Remove global content width.
264287
* This can affect "width" attribute for images. If required can be overwritten in app file.
@@ -382,6 +405,17 @@ public function register_post_types() {
382405
public function register_taxonomies() {
383406
}
384407

408+
/**
409+
* Register API Endpoints
410+
* Usage:
411+
* new \namespace\App\Endpoints\MyEndpoint();
412+
*
413+
* Each endpoint register it's own action hook
414+
*/
415+
public function register_api_endpoints() {
416+
417+
}
418+
385419
/**
386420
* Adds loading of custom features provided by 3d-party plugins.
387421
*/

0 commit comments

Comments
 (0)