PluginProbe
Remove Schema / trunk
Remove Schema vtrunk
2.0.0 trunk 1.6.1 1.6.2
remove-schema / src / Admin / class-posteditor.php

class-posteditor.php in Remove Schema trunk, at src/Admin/class-posteditor.php

175 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Post editor schema removal settings.
4 *
5 * @package TimVanIersel\RemoveSchema
6 */
7
8 declare(strict_types=1);
9
10 namespace TimVanIersel\RemoveSchema\Admin;
11
12 use TimVanIersel\RemoveSchema\Contracts\Service;
13 use TimVanIersel\RemoveSchema\Options\SchemaOptions;
14 use TimVanIersel\RemoveSchema\PluginContext;
15 use WP_Post;
16
17 if ( ! defined( 'ABSPATH' ) ) {
18 exit;
19 }
20
21 /**
22 * Adds legacy page-specific Remove Schema options to post edit screens.
23 */
24 final class PostEditor implements Service {
25 private const NONCE_ACTION = 'remove_schema_save_post_options';
26 private const NONCE_NAME = 'remove_schema_nonce';
27
28 /**
29 * Constructor.
30 *
31 * @param PluginContext $context Plugin context.
32 */
33 public function __construct(
34 private readonly PluginContext $context
35 ) {
36 }
37
38 /**
39 * Register WordPress hooks for the service.
40 *
41 * @return void
42 */
43 public function register(): void {
44 add_action( 'add_meta_boxes', array( $this, 'addMetaBoxes' ) );
45 add_action( 'save_post', array( $this, 'savePost' ) );
46 }
47
48 /**
49 * Add the Remove Schema meta box to all post types.
50 *
51 * @return void
52 */
53 public function addMetaBoxes(): void {
54 foreach ( get_post_types() as $post_type ) {
55 add_meta_box(
56 'remove-schema',
57 __( 'Remove schema', 'remove-schema' ),
58 array( $this, 'renderMetaBox' ),
59 $post_type,
60 'side'
61 );
62 }
63 }
64
65 /**
66 * Render the page-specific options meta box.
67 *
68 * @param WP_Post $post Current post.
69 * @return void
70 */
71 public function renderMetaBox( WP_Post $post ): void {
72 wp_nonce_field( self::NONCE_ACTION, self::NONCE_NAME );
73
74 $options = SchemaOptions::normalize_post_options(
75 get_post_meta( $post->ID, SchemaOptions::POST_META_KEY, true )
76 );
77 ?>
78 <input class="hidden" type="hidden" name="<?php echo esc_attr( $this->context->option_name() ); ?>[fake_field]" value="1" />
79
80 <p>
81 <label>
82 <input type="checkbox" name="<?php echo esc_attr( $this->context->option_name() ); ?>[keep_schema]" value="1" <?php checked( $options['keep_schema'], 1 ); ?> />
83 <strong><?php esc_html_e( 'Turn off remove schema on this page', 'remove-schema' ); ?></strong>
84 </label>
85 </p>
86
87 <div class="remove-schema-post-options<?php echo ! empty( $options['keep_schema'] ) ? ' is-disabled' : ''; ?>">
88 <?php $this->renderPostCheckbox( 'rm_jsonld', __( 'Remove all JSON-LD', 'remove-schema' ), $options ); ?>
89 <?php if ( $this->isPluginActive( 'wordpress-seo/wp-seo.php' ) || $this->isPluginActive( 'wordpress-seo-premium/wp-seo-premium.php' ) ) : ?>
90 <?php $this->renderPostCheckbox( 'yoast_jsonld', __( 'Remove Yoast JSON-LD', 'remove-schema' ), $options ); ?>
91 <?php endif; ?>
92 <?php if ( $this->isPluginActive( 'woocommerce/woocommerce.php' ) ) : ?>
93 <?php $this->renderPostCheckbox( 'woocommerce_jsonld', __( 'Remove WooCommerce JSON-LD', 'remove-schema' ), $options ); ?>
94 <?php endif; ?>
95 <?php if ( $this->isPluginActive( 'wp-schema-pro/wp-schema-pro.php' ) ) : ?>
96 <?php $this->renderPostCheckbox( 'schema_pro', __( 'Remove Schema pro JSON-LD', 'remove-schema' ), $options ); ?>
97 <?php endif; ?>
98 <?php $this->renderPostCheckbox( 'microdata', __( 'Remove all Microdata', 'remove-schema' ), $options ); ?>
99 <?php $this->renderPostCheckbox( 'rdfa', __( 'Remove all RDFa', 'remove-schema' ), $options ); ?>
100 </div>
101 <?php
102 }
103
104 /**
105 * Save page-specific options.
106 *
107 * @param int $post_id Post ID.
108 * @return void
109 */
110 public function savePost( int $post_id ): void {
111 if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
112 return;
113 }
114
115 if ( ! isset( $_POST[ self::NONCE_NAME ] ) || ! wp_verify_nonce( sanitize_text_field( wp_unslash( $_POST[ self::NONCE_NAME ] ) ), self::NONCE_ACTION ) ) {
116 return;
117 }
118
119 if ( ! current_user_can( 'edit_post', $post_id ) ) {
120 return;
121 }
122
123 $raw_options = array();
124 $option_name = $this->context->option_name();
125
126 if ( isset( $_POST[ $option_name ] ) && is_array( $_POST[ $option_name ] ) ) {
127 $raw_options = array_map(
128 'sanitize_text_field',
129 wp_unslash( $_POST[ $option_name ] ) // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
130 );
131 }
132
133 $options = SchemaOptions::sanitize_post_options( $raw_options );
134
135 update_post_meta( $post_id, SchemaOptions::POST_META_KEY, $options );
136 }
137
138 /**
139 * Render one post meta checkbox.
140 *
141 * @param string $key Option key.
142 * @param string $label Field label.
143 * @param array<string, int> $options Current options.
144 * @return void
145 */
146 private function renderPostCheckbox( string $key, string $label, array $options ): void {
147 ?>
148 <p>
149 <label>
150 <input type="checkbox" name="<?php echo esc_attr( $this->context->option_name() ); ?>[<?php echo esc_attr( $key ); ?>]" value="1" <?php checked( $options[ $key ], 1 ); ?> />
151 <?php echo esc_html( $label ); ?>
152 </label>
153 </p>
154 <?php
155 }
156
157 /**
158 * Check whether a plugin is active on single-site or multisite installs.
159 *
160 * @param string $plugin_path Plugin path relative to the plugins directory.
161 * @return bool
162 */
163 private function isPluginActive( string $plugin_path ): bool {
164 if ( ! function_exists( 'is_plugin_active' ) ) {
165 require_once ABSPATH . 'wp-admin/includes/plugin.php';
166 }
167
168 if ( is_multisite() && is_plugin_active_for_network( $plugin_path ) ) {
169 return true;
170 }
171
172 return is_plugin_active( $plugin_path );
173 }
174 }
175