This repository has been archived by the owner on Mar 20, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
wp-schema-pro-genesis.php
272 lines (239 loc) · 7.26 KB
/
wp-schema-pro-genesis.php
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
<?php
/**
* Plugin Name: Schema Pro - Genesis
* Plugin URI: https://github.com/bizbudding/wp-schema-pro-genesis
* Description: Automatically disables specific Genesis schema when Schema Pro is outputting JSON-LD data.
* Version: 0.2.0
*
* Author: Mike Hemberger
* Author URI: https://bizbudding.com
* Text Domain: 'wp-schema-pro-genesis'
*/
// Exit if accessed directly.
if ( ! defined( 'ABSPATH' ) ) exit;
/**
* Main Schema_Pro_Genesis_Setup Class.
*
* @access private
* @since 0.1.0
*/
final class Schema_Pro_Genesis_Setup {
/**
* @var Schema_Pro_Genesis_Setup The one true Schema_Pro_Genesis_Setup
* @since 0.1.0
*/
private static $instance;
/**
* Main Schema_Pro_Genesis_Setup Instance.
*
* Insures that only one instance of Schema_Pro_Genesis_Setup exists in memory at any one
* time. Also prevents needing to define globals all over the place.
*
* @since 0.1.0
* @static var array $instance
* @return object | Schema_Pro_Genesis_Setup The one true Schema_Pro_Genesis_Setup
*/
public static function instance() {
if ( ! isset( self::$instance ) ) {
// Setup the setup
self::$instance = new Schema_Pro_Genesis_Setup;
// Methods
self::$instance->setup_constants();
self::$instance->includes();
self::$instance->init();
}
return self::$instance;
}
/**
* Throw error on object clone.
*
* The whole idea of the singleton design pattern is that there is a single
* object therefore, we don't want the object to be cloned.
*
* @since 0.1.0
* @access protected
* @return void
*/
public function __clone() {
// Cloning instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wp-schema-pro-genesis' ), '1.0' );
}
/**
* Disable unserializing of the class.
*
* @since 0.1.0
* @access protected
* @return void
*/
public function __wakeup() {
// Unserializing instances of the class is forbidden.
_doing_it_wrong( __FUNCTION__, __( 'Cheatin’ huh?', 'wp-schema-pro-genesis' ), '1.0' );
}
/**
* Setup plugin constants.
*
* @access private
* @since 0.1.0
* @return void
*/
private function setup_constants() {
// Plugin version.
if ( ! defined( 'SCHEMA_PRO_GENESIS_VERSION' ) ) {
define( 'SCHEMA_PRO_GENESIS_VERSION', '0.2.0' );
}
// Plugin Folder Path.
if ( ! defined( 'SCHEMA_PRO_GENESIS_PLUGIN_DIR' ) ) {
define( 'SCHEMA_PRO_GENESIS_PLUGIN_DIR', plugin_dir_path( __FILE__ ) );
}
// Plugin Includes Path
if ( ! defined( 'SCHEMA_PRO_GENESIS_INCLUDES_DIR' ) ) {
define( 'SCHEMA_PRO_GENESIS_INCLUDES_DIR', SCHEMA_PRO_GENESIS_PLUGIN_DIR . 'includes/' );
}
// Plugin Folder URL.
if ( ! defined( 'SCHEMA_PRO_GENESIS_PLUGIN_URL' ) ) {
define( 'SCHEMA_PRO_GENESIS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
}
// Plugin Root File.
if ( ! defined( 'SCHEMA_PRO_GENESIS_PLUGIN_FILE' ) ) {
define( 'SCHEMA_PRO_GENESIS_PLUGIN_FILE', __FILE__ );
}
// Plugin Base Name
if ( ! defined( 'SCHEMA_PRO_GENESIS_BASENAME' ) ) {
define( 'SCHEMA_PRO_GENESIS_BASENAME', dirname( plugin_basename( __FILE__ ) ) );
}
}
/**
* Include required files.
*
* @access private
* @since 0.1.0
* @return void
*/
private function includes() {
// Include vendor libraries.
require_once __DIR__ . '/vendor/autoload.php';
// foreach ( glob( SCHEMA_PRO_GENESIS_INCLUDES_DIR . '*.php' ) as $file ) { include $file; }
}
/**
* Initialize.
*
* @return void
*/
public function init() {
add_action( 'admin_init', array( $this, 'updater' ) );
add_action( 'wp', array( $this, 'run' ), 99 );
}
/**
* Setup the updater.
*
* composer require yahnis-elsts/plugin-update-checker
*
* @uses https://github.com/YahnisElsts/plugin-update-checker/
*
* @return void
*/
public function updater() {
// Bail if current user cannot manage plugins.
if ( ! current_user_can( 'install_plugins' ) ) {
return;
}
// Bail if plugin updater is not loaded.
if ( ! class_exists( 'Puc_v4_Factory' ) ) {
return;
}
// Setup the updater.
$updater = Puc_v4_Factory::buildUpdateChecker( 'https://github.com/bizbudding/wp-schema-pro-genesis', __FILE__, 'wp-schema-pro-genesis' );
}
/**
* Remove Genesis schema if WP Schema Pro is outputting schema on the current page.
* A lot of the conditional code was taken directly from:
* /plugins/wp-schema-pro/classes/class-bsf-aiosrs-pro-markup.php
*
* @uses Genesis.
* @uses Schema Pro.
*
* @return void
*/
public function run() {
// Bail if not on the front end.
if ( is_admin() ) {
return;
}
// Bail if Genesis is not the parent theme.
if ( 'genesis'!== get_template() ) {
return;
}
// Bail if Schema Pro is not active.
if ( ! class_exists( 'BSF_AIOSRS_Pro' ) ) {
return;
}
if ( class_exists( 'BSF_AIOSRS_Pro_Admin' ) ) {
// Get schema settings.
$general_settings = BSF_AIOSRS_Pro_Admin::get_options( 'wp-schema-pro-general-settings' );
// Body.
if ( ! empty( $general_settings['site-represent'] ) ) {
add_filter( 'genesis_attr_body', array( $this, 'remove_attributes' ), 20 );
}
}
if ( class_exists( 'BSF_AIOSRS_Pro_Admin' ) && class_exists( 'BSF_AIOSRS_Pro_Markup' ) ) {
$global_settings = BSF_AIOSRS_Pro_Admin::get_options( 'wp-schema-pro-global-schemas' );
$posts = BSF_AIOSRS_Pro_Markup::get_schema_posts();
// Breadcrumbs.
if ( '1' === $global_settings['breadcrumb'] ) {
add_filter( 'genesis_attr_breadcrumb', array( $this, 'remove_attributes' ), 20 );
add_filter( 'genesis_attr_breadcrumb-link-wrap', array( $this, 'remove_attributes' ), 20 );
}
// Content.
if ( is_array( $posts ) && ! empty( $posts ) ) {
add_filter( 'genesis_attr_content', array( $this, 'remove_attributes' ), 20 );
add_filter( 'genesis_attr_entry', array( $this, 'remove_attributes' ), 20 );
}
// Bail if not a single page/post/cpt.
if ( ! is_singular() ) {
return;
}
// Set variables.
$post_id = get_the_ID();
$about = (int)$global_settings['about-page'];
$contact = (int)$global_settings['contact-page'];
// Specific items.
if ( in_array( $post_id, array( $about, $contact ) ) ) {
add_filter( 'genesis_attr_content', array( $this, 'remove_attributes' ), 20 );
add_filter( 'genesis_attr_entry', array( $this, 'remove_attributes' ), 20 );
}
}
}
/**
* Clear schema from a Genesis element.
*
* @param array $attributes The attributes of the element.
*
* @return array The modified attirbutes.
*/
public function remove_attributes( $attributes ) {
$attributes['itemprop'] = '';
$attributes['itemscope'] = '';
$attributes['itemtype'] = '';
return $attributes;
}
}
/**
* The main function for that returns Schema_Pro_Genesis_Setup
*
* The main function responsible for returning the one true Schema_Pro_Genesis_Setup
* Instance to functions everywhere.
*
* Use this function like you would a global variable, except without needing
* to declare the global.
*
* Example: <?php $plugin = Schema_Pro_Genesis(); ?>
*
* @since 0.1.0
*
* @return object|Schema_Pro_Genesis_Setup The one true Schema_Pro_Genesis_Setup Instance.
*/
function Schema_Pro_Genesis() {
return Schema_Pro_Genesis_Setup::instance();
}
// Get Schema_Pro_Genesis Running.
Schema_Pro_Genesis();