PluginProbe
WProofreader spell & grammar check plugin for WordPress / trunk
WProofreader spell & grammar check plugin for WordPress vtrunk
3.2.1 3.2.2 3.2.0 trunk 1.0 1.1 2.0 2.1 2.2 2.3 2.4 2.5 2.6 2.6.1 2.6.10 2.6.2 2.6.3 2.6.4 2.6.5 2.6.6 2.6.7 2.6.8 2.6.9 2.7.0 2.7.1 All 28 releases
webspellchecker / includes / class-wproofreader-context-detector.php

class-wproofreader-context-detector.php in WProofreader spell & grammar check plugin for WordPress trunk, at includes/class-wproofreader-context-detector.php

176 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 if ( ! defined( 'ABSPATH' ) ) {
3 exit;
4 }
5
6 /**
7 * Detects WordPress admin/editor context without deciding plugin eligibility.
8 */
9 class WProofreader_Context_Detector {
10
11 const SCREEN_SETTINGS = 'settings_page_spell-checker-settings';
12 const SCREEN_SITE_EDITOR = 'site-editor';
13 const SCREEN_PERMALINKS = 'options-permalink';
14 const SCREEN_EDIT_CATEGORY = 'edit-category';
15 const SCREEN_EDIT_PRODUCT_CATEGORY = 'edit-product_cat';
16 const SCREEN_EDIT_WPSC_PRODUCT_CAT = 'edit-wpsc_product_category';
17 const SCREEN_EDIT_PRODUCT_TAG = 'edit-product_tag';
18 const SCREEN_EDIT_POST_TAG = 'edit-post_tag';
19
20 const PLACE_SETTINGS = 'settings';
21 const PLACE_TAXONOMY = 'taxonomy';
22 const PLACE_POST_TYPE = 'post_type';
23 const PLACE_OTHER = 'other';
24
25 const TAXONOMY_CATEGORY = 'category';
26 const TAXONOMY_TAG = 'tag';
27
28 /**
29 * Get current admin screen if available.
30 *
31 * @return WP_Screen|null
32 */
33 public function get_screen() {
34 return function_exists( 'get_current_screen' ) ? get_current_screen() : null;
35 }
36
37 /**
38 * Check if current screen is the block editor.
39 *
40 * @param WP_Screen|null $screen Screen object.
41 * @return bool
42 */
43 public function is_block_editor_screen( $screen ): bool {
44 return ( $screen && method_exists( $screen, 'is_block_editor' ) && $screen->is_block_editor() );
45 }
46
47 /**
48 * Check if current screen is the site editor.
49 *
50 * @param WP_Screen|null $screen Screen object.
51 * @return bool
52 */
53 public function is_site_editor_screen( $screen ): bool {
54 if ( $this->screen_matches(
55 $screen,
56 array( self::SCREEN_SITE_EDITOR ),
57 array( 'gutenberg-edit-site' )
58 ) ) {
59 return true;
60 }
61
62 return $this->request_path_contains( 'site-editor.php' );
63 }
64
65 /**
66 * Check if current screen is the permalinks settings page.
67 *
68 * @param WP_Screen|null $screen Screen object.
69 * @return bool
70 */
71 public function is_permalinks_settings_screen( $screen ): bool {
72 if ( $this->screen_matches( $screen, array( self::SCREEN_PERMALINKS ), array() ) ) {
73 return true;
74 }
75
76 return $this->request_path_contains( 'options-permalink.php' );
77 }
78
79 /**
80 * Fallback when get_current_screen() is not yet available.
81 *
82 * @param string $needle Filename to look for in PHP_SELF.
83 * @return bool
84 */
85 private function request_path_contains( string $needle ): bool {
86 if ( empty( $_SERVER['PHP_SELF'] ) ) {
87 return false;
88 }
89
90 return false !== strpos( (string) $_SERVER['PHP_SELF'], $needle );
91 }
92
93 /**
94 * Detect current admin context.
95 *
96 * @return array
97 */
98 public function detect(): array {
99 $screen = $this->get_screen();
100
101 $context = array(
102 'place' => self::PLACE_OTHER,
103 'screen_id' => $screen->id ?? '',
104 'screen_base' => $screen->base ?? '',
105 'post_type' => $screen->post_type ?? '',
106 'taxonomy' => '',
107 );
108
109 if ( self::SCREEN_SETTINGS === $context['screen_base'] ) {
110 $context['place'] = self::PLACE_SETTINGS;
111 return $context;
112 }
113
114 $taxonomy = $this->taxonomy_for_screen( $context['screen_id'] );
115 if ( '' !== $taxonomy ) {
116 $context['place'] = self::PLACE_TAXONOMY;
117 $context['taxonomy'] = $taxonomy;
118 return $context;
119 }
120
121 if ( ! empty( $context['post_type'] ) ) {
122 $context['place'] = self::PLACE_POST_TYPE;
123 return $context;
124 }
125
126 return $context;
127 }
128
129 /**
130 * Match by screen id/base and optional base substrings.
131 *
132 * @param WP_Screen|null $screen Screen object.
133 * @param array $ids Exact ids/bases.
134 * @param array $base_substrings Base substrings.
135 * @return bool
136 */
137 private function screen_matches( $screen, array $ids, array $base_substrings ): bool {
138 if ( ! $screen ) {
139 return false;
140 }
141
142 $screen_id = isset( $screen->id ) ? (string) $screen->id : '';
143 $screen_base = isset( $screen->base ) ? (string) $screen->base : '';
144
145 if ( in_array( $screen_id, $ids, true ) || in_array( $screen_base, $ids, true ) ) {
146 return true;
147 }
148
149 foreach ( $base_substrings as $substring ) {
150 if ( '' !== $screen_base && false !== strpos( $screen_base, (string) $substring ) ) {
151 return true;
152 }
153 }
154
155 return false;
156 }
157
158 /**
159 * Map edit taxonomy screens to option groups.
160 *
161 * @param string $screen_id Screen id.
162 * @return string
163 */
164 private function taxonomy_for_screen( string $screen_id ): string {
165 $map = array(
166 self::SCREEN_EDIT_CATEGORY => self::TAXONOMY_CATEGORY,
167 self::SCREEN_EDIT_PRODUCT_CATEGORY => self::TAXONOMY_CATEGORY,
168 self::SCREEN_EDIT_WPSC_PRODUCT_CAT => self::TAXONOMY_CATEGORY,
169 self::SCREEN_EDIT_PRODUCT_TAG => self::TAXONOMY_TAG,
170 self::SCREEN_EDIT_POST_TAG => self::TAXONOMY_TAG,
171 );
172
173 return isset( $map[ $screen_id ] ) ? $map[ $screen_id ] : '';
174 }
175 }
176