-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathschema-optimizer-pro.php
166 lines (146 loc) · 7.2 KB
/
schema-optimizer-pro.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
<?php
/*
* Plugin Name: Schema Optimizer Pro
* Text Domain: schema-optimizer-pro
* Description: Schema Optimizer Pro lets you easily add schema markup to your website, increasing search visibility and traffic, with ability to customize schema markup using custom fields, optimize performance with caching and validate with debug mode.
* Version: 1.0
* Requires PHP: 5.6
* Requires at least: 4.5
* Tested up to: 6.1.1
* Plugin URI: https://github.com/nasyx-rakeeb/schema-optimizer-pro
* Author URI: https://nasyxrakeeb.vercel.app
* Author: Nasyx Rakeeb
* License: GPL2
* License URI: https://github.com/nasyx-rakeeb/schema-optimizer-pro/blob/main/LICENSE.txt
*/
class SEO_Schema_Markup_Generator {
public function __construct() {
add_action('wp_head', array($this, 'generate_schema_markup'));
add_action('admin_menu', array($this, 'add_settings_page'));
add_action('admin_init', array($this, 'register_settings'));
function add_settings_link($links) {
$settings_link = '<a href="admin.php?page=schema-optimizer-pro">Settings</a>';
array_unshift($links, $settings_link);
return $links;
}
add_filter('plugin_action_links_' . plugin_basename(__FILE__), 'add_settings_link');
}
public function add_settings_page() {
add_menu_page('Schema Optimizer Pro', 'Schema Optimizer Pro', 'manage_options', 'schema-optimizer-pro', array($this, 'display_settings_page'), 'dashicons-admin-generic', 99);
}
public function register_settings() {
register_setting('schema-optimizer-pro-options', 'schema-optimizer-pro-options', array($this, 'sanitize_options'), array('custom_fields' => '', 'cache_enabled' => 0, 'debug_mode' => 0));
}
public function display_settings_page() {
$options = get_option('schema-optimizer-pro-options');
?>
<div class="wrap">
<h1>Schema Optimizer Pro</h1>
<form method="post" action="options.php">
<?php settings_fields('schema-optimizer-pro-options'); ?>
<table class="form-table">
<tr>
<th>
<label for="schema-optimizer-pro-options[custom_fields]">Custom Fields</label>
</th>
<td>
<input type="text" id="schema-optimizer-pro-options[custom_fields]" name="schema-optimizer-pro-options[custom_fields]" value="<?php echo esc_attr(isset($options['custom_fields']) ? $options['custom_fields'] : ''); ?>" class="regular-text">
<p class="description">
Enter a comma-separated list of custom fields to use for the schema markup.
</p>
</td>
</tr>
<tr>
<th>
</th>
<td>
<input type="checkbox" id="schema-optimizer-pro-options[cache_enabled]" name="schema-optimizer-pro-options[cache_enabled]" value="1" <?php checked(isset($options['cache_enabled']) ? $options['cache_enabled'] : 0, 1); ?>>
<label for="schema-optimizer-pro-options[cache_enabled]"><strong>Enable caching</strong></label>
<p class="description">
Enable caching of the generated schema markup for improved performance.
</p>
</td>
</tr>
<tr>
<th>
</th>
<td>
<input type="checkbox" id="schema-optimizer-pro-options[debug_mode]" name="schema-optimizer-pro-options[debug_mode]" value="1" <?php checked(isset($options['debug_mode']) ? $options['debug_mode'] : 0, 1); ?>>
<label for="schema-optimizer-pro-options[debug_mode]"><strong>Debug mode</strong></label>
<p class="description">
Enable debug mode to view and validate the generated schema markup.
</p>
</td>
</tr>
</table>
<?php submit_button(); ?>
</form>
</div>
<?php
}
public function sanitize_options($options) {
$options['custom_fields'] = (isset($options['custom_fields']) ? sanitize_text_field($options['custom_fields']) : '');
$options['cache_enabled'] = (isset($options['cache_enabled']) ? intval($options['cache_enabled']) : 0);
$options['debug_mode'] = (isset($options['debug_mode']) ? intval($options['debug_mode']) : 0);
return $options;
}
public function generate_schema_markup() {
global $post;
$options = get_option('schema-optimizer-pro-options');
if ($options['cache_enabled']) {
// check for cached schema markup
$cached_schema = get_transient('schema_markup_' . $post->ID);
if ($cached_schema) {
echo $cached_schema;
return;
}
}
$schema = array();
if (is_single()) {
$schema['@context'] = "http://schema.org";
$schema['@type'] = "Article";
$schema['headline'] = get_the_title();
$schema['datePublished'] = get_the_date('c');
$schema['dateModified'] = get_the_modified_date('c');
$schema['author'] = array(
'@type' => 'Person',
'name' => get_the_author()
);
$schema['publisher'] = array(
'@type' => 'Organization',
'name' => get_bloginfo('name'),
'logo' => array(
'@type' => 'ImageObject',
'url' => get_site_icon_url()
)
);
$schema['image'] = get_the_post_thumbnail_url();
$schema['mainEntityOfPage'] = get_the_permalink();
// add custom fields
$custom_fields = explode(',', $options['custom_fields']);
foreach ($custom_fields as $custom_field) {
$custom_field = trim($custom_field);
$schema[$custom_field] = get_post_meta($post->ID, $custom_field, true);
}
}
if (!empty($schema)) {
$schema_script = '<script type="application/ld+json">' . json_encode($schema) . '</script>';
if ($options['debug_mode']) {
// validate schema markup
$validator = new JsonLdValidator();
$validation_errors = $validator->validate($schema_script);
if (count($validation_errors) > 0) {
echo '<!-- Schema Markup Validation Errors: ' . implode(', ', $validation_errors) . ' -->';
} else {
echo $schema_script;
}
} else {
echo $schema_script;
} if ($options['cache_enabled']) {
// cache the schema markup for 1 hour
set_transient('schema_markup_' . $post->ID, $schema_script, HOUR_IN_SECONDS);
}
}
}
}
new SEO_Schema_Markup_Generator();