class-gutenberg.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
<?php
/**
* Smush integration with Gutenberg editor: Gutenberg class
*
* @package Smush\Core\Integrations
* @since 2.8.1
*
* @author Anton Vanyukov <anton@incsub.com>
*
* @copyright (c) 2018, Incsub (http://incsub.com)
*/
namespace Smush\Core\Integrations;
if ( ! defined( 'WPINC' ) ) {
die;
}
/**
* Class Gutenberg for Gutenberg integration.
*
* @since 2.8.1
*/
class Gutenberg extends Abstract_Integration {
/**
* Gutenberg constructor.
*
* @since 2.8.1
*/
public function __construct() {
$this->module = 'gutenberg';
$this->class = 'free';
$this->check_for_gutenberg();
parent::__construct();
if ( ! $this->enabled ) {
// Disable setting if Gutenberg is not active.
add_filter( 'wp_smush_integration_status_' . $this->module, '__return_true' );
// Hook at the end of setting row to output an error div.
add_action( 'smush_setting_column_right_inside', array( $this, 'integration_error' ) );
return;
}
// Register gutenberg block assets.
add_action( 'enqueue_block_editor_assets', array( $this, 'enqueue_gb' ) );
}
/**************************************
*
* OVERWRITE PARENT CLASS FUNCTIONALITY
*/
/**
* Filters the setting variable to add Gutenberg setting title and description.
*
* @since 2.8.1
*
* @param array $settings Settings array.
*
* @return mixed
*/
public function register( $settings ) {
$settings[ $this->module ] = array(
'label' => esc_html__( 'Show Smush stats in Gutenberg blocks', 'wp-smushit' ),
'short_label' => esc_html__( 'Gutenberg Support', 'wp-smushit' ),
'desc' => esc_html__(
'Add statistics and the manual smush button to Gutenberg blocks that display images.',
'wp-smushit'
),
);
return $settings;
}
/**************************************
*
* PUBLIC CLASSES
*/
/**
* Prints the message for Gutenberg setup.
*
* @since 2.8.1
*
* @param string $setting_key Settings key.
*/
public function integration_error( $setting_key ) {
// Return if not Gutenberg integration.
if ( $this->module !== $setting_key ) {
return;
}
?>
<div class="sui-toggle-content">
<div class="sui-notice">
<div class="sui-notice-content">
<div class="sui-notice-message">
<i class="sui-notice-icon sui-icon-info" aria-hidden="true"></i>
<p><?php esc_html_e( 'To use this feature you need to install and activate the Gutenberg plugin.', 'wp-smushit' ); ?></p>
</div>
</div>
</div>
</div>
<?php
}
/**
* Enqueue Gutenberg block assets for backend editor.
*
* `wp-blocks`: includes block type registration and related functions.
* `wp-element`: includes the WordPress Element abstraction for describing the structure of your blocks.
* `wp-i18n`: To internationalize the block's text.
*
* @since 2.8.1
*/
public function enqueue_gb() {
$enabled = $this->settings->get( $this->module );
if ( ! $enabled ) {
return;
}
// Gutenberg block scripts.
wp_enqueue_script(
'smush-gutenberg',
WP_SMUSH_URL . 'app/assets/js/smush-blocks.min.js',
array( 'wp-blocks', 'wp-i18n', 'wp-element' ),
WP_SMUSH_VERSION,
true
);
}
/**************************************
*
* PRIVATE CLASSES
*/
/**
* Make sure we only enqueue when Gutenberg is active.
*
* For WordPress pre 5.0 - only when Gutenberg plugin is installed.
* For WordPress 5.0+ - only when Classic Editor is NOT installed.
*
* @since 3.0.2
*/
private function check_for_gutenberg() {
global $wp_version;
if ( ! function_exists( 'is_plugin_active' ) ) {
/* @noinspection PhpIncludeInspection */
include_once ABSPATH . 'wp-admin/includes/plugin.php';
}
// Check if WordPress 5.0 or higher.
$is_wp5point0 = version_compare( $wp_version, '4.9.9', '>' );
if ( $is_wp5point0 ) {
$this->enabled = ! is_plugin_active( 'classic-editor/classic-editor.php' );
} else {
$this->enabled = is_plugin_active( 'gutenberg/gutenberg.php' );
}
}
}