-
Notifications
You must be signed in to change notification settings - Fork 19
Integration branch: Sidebar Metabox #1332
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 17 commits
0251fcc
dd1392f
67e92d8
1d93cea
1d8e558
8c4cd1c
7668fd2
90b4ae2
fb9db34
ffc4dd6
900e882
b77dee3
7817b40
a2794a3
a8c78cc
f930e50
57bffd6
8abb52c
2620171
2fc1454
c7c39e0
a47dbe8
6e0567a
913672f
9613324
775df9e
2f9ddf6
777c1a7
ee17ec0
cf707ff
b8168e6
ccc6410
3b9cefc
b82799c
e5a6128
3b0a198
2c2f729
2122c4e
dfcb6ff
719d58c
b773509
eebeac8
d6f7b44
7d46dc7
2a9621b
a155912
de7ddb4
91df4ff
1aada59
30734be
3e3ee98
dd4c35f
ccc011a
9340134
6090da0
f4e1c3a
627e4d6
4814709
1ed4412
3d5b5b2
0e1e751
60e4e91
7ff8b53
b15b919
04ae21e
0fca6e7
2d45195
396d699
2e5232a
99a51e1
091a658
c760dd8
32f82f4
fb89e6f
872fbb9
b48b482
87aee09
79e0e19
7c7ff1a
325497e
762ee96
3eb22d0
2464b63
8dcbcef
8126f73
50892a9
2d737d0
0eec8a6
42e75df
2f09a85
f107fa1
853e7d7
b6cfdd4
00773d0
3932c69
e3ed1ce
af212eb
56f52bb
7d35769
3d8231d
5e3e32b
d99ed26
a41c6d1
e623aab
eaa9b54
56c66c1
4b867f7
f6fcc2c
3dbca12
1f558f7
1cb1078
c6abe5c
8f9194b
538e397
6e3dd1d
3394747
2d0603b
cc030e8
0ba5849
b05f240
25d1cf5
bb04b29
97541e6
79d3239
e53d9c4
27a9a15
b357636
742bae0
3e93eb7
ab74aeb
22da85b
c448773
78297e6
94f4983
c57e304
3598cba
5e3f57e
0cfedf4
fb3cf58
893f014
223c161
bf6b5f6
d45d70c
8cc8e1c
2a1a478
d97c0e5
52d6ebb
08c337a
6f5e460
f464a92
f617c14
d13ba3b
3355a3f
72ddff2
bc17adc
9dd19ae
5e4fd73
476132b
18c2449
6465af8
ee25828
b690bd7
2b779de
7f52e0b
0d0725a
ab79fdb
2ff7da5
bc1d434
1131feb
f550a42
57d2c52
1aac068
6cad30e
7c9afba
95b5822
b7e1437
386e525
ca02b4b
059c3f3
e23016d
bd9a654
25885cb
7b16bf5
f752c3c
578b1fe
261d9df
a9141d4
3eadb5b
7446b0e
4e0196d
f176039
2ebe9fa
96ff297
850857a
43fb777
569993c
b4d7b98
d58f17b
865c7cc
5de0870
0efd13a
b6d6b10
987487b
a1a5851
6eb1ff5
ff03831
7e878a4
f3dda57
4319781
f3a4afb
102f8e9
be9860a
f4fcfde
df5e7b1
99ade24
bb31a14
92bee8c
54211cb
95322ba
997a194
414a55c
1a48608
1cf3fb5
8b7f140
71b9ab4
8fdd5b6
8b2954f
322d2b5
d83e00a
21b2fbe
395d57f
f8508a4
bbc21e1
1c6ef4b
0a88b7e
0fb86a3
1338972
93553e0
9f3a412
7a65cee
e521eab
7ec6158
f590a14
cc0b17c
30d3241
54f6c93
614ec89
0069a71
e3f11f8
4726454
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -206,6 +206,34 @@ function () use ( $ns, $version ) { | |
| ); | ||
| } | ||
| ); | ||
|
|
||
| // Sidebar data endpoint - returns all metabox data in one call. | ||
| add_action( | ||
| 'rest_api_init', | ||
| function () use ( $ns, $version ) { | ||
| register_rest_route( | ||
| $ns . $version, | ||
| '/sidebar-data/(?P<id>\d+)', | ||
| [ | ||
| 'methods' => 'GET', | ||
| 'callback' => [ $this, 'get_sidebar_data' ], | ||
| 'args' => [ | ||
| 'id' => [ | ||
| 'required' => true, | ||
| 'validate_callback' => function ( $param ) { | ||
| return is_numeric( $param ); | ||
| }, | ||
| 'sanitize_callback' => 'absint', | ||
| ], | ||
| ], | ||
| 'permission_callback' => function ( $request ) { | ||
| $post_id = (int) $request['id']; | ||
| return current_user_can( 'edit_post', $post_id ); | ||
| }, | ||
| ] | ||
| ); | ||
| } | ||
| ); | ||
|
pattonwebz marked this conversation as resolved.
Comment on lines
+215
to
+240
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. While this follows the existing pattern in |
||
| } | ||
|
|
||
| /** | ||
|
|
@@ -668,4 +696,226 @@ public function get_site_summary( \WP_REST_Request $request ) { | |
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * REST handler that gets all sidebar data for a post (summary, details, readability). | ||
| * | ||
| * @since 1.xx.x | ||
|
pattonwebz marked this conversation as resolved.
pattonwebz marked this conversation as resolved.
|
||
| * | ||
| * @param \WP_REST_Request $request The request passed from the REST call. | ||
| * | ||
| * @return \WP_REST_Response | ||
| */ | ||
| public function get_sidebar_data( \WP_REST_Request $request ) { | ||
| $post_id = (int) $request['id']; | ||
|
|
||
| try { | ||
| $data = [ | ||
| 'post_id' => $post_id, | ||
| 'summary' => $this->get_summary_data( $post_id ), | ||
| 'details' => $this->get_details_data( $post_id ), | ||
| 'readability' => $this->get_readability_data( $post_id ), | ||
| ]; | ||
|
|
||
| return new \WP_REST_Response( | ||
| [ | ||
| 'success' => true, | ||
| 'data' => $data, | ||
| ], | ||
| 200 | ||
| ); | ||
| } catch ( \Exception $ex ) { | ||
| return new \WP_REST_Response( | ||
| [ | ||
| 'success' => false, | ||
| 'message' => $ex->getMessage(), | ||
| ], | ||
| 500 | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Get summary data for a post. | ||
| * | ||
| * Returns cached summary data from post meta. If no cache exists, returns defaults. | ||
| * | ||
| * @since 1.xx.x | ||
| * | ||
| * @param int $post_id The post ID. | ||
| * | ||
| * @return array | ||
| */ | ||
| private function get_summary_data( $post_id ) { | ||
| // Get summary from post meta. | ||
| $summary = get_post_meta( $post_id, '_edac_summary', true ); | ||
|
|
||
| // If summary doesn't exist or is invalid, return defaults. | ||
| if ( ! $summary || ! is_array( $summary ) ) { | ||
| $summary = [ | ||
| 'passed_tests' => 0, | ||
| 'errors' => 0, | ||
| 'contrast_errors' => 0, | ||
| 'warnings' => 0, | ||
| 'ignored' => 0, | ||
| 'readability' => 0, | ||
| ]; | ||
| } | ||
|
|
||
| return $summary; | ||
| } | ||
|
|
||
| /** | ||
| * Get details data for a post (errors, warnings, passed rules). | ||
| * | ||
| * @since 1.xx.x | ||
| * | ||
| * @param int $post_id The post ID. | ||
| * | ||
| * @throws \Exception If the database table name is invalid. | ||
| * @return array | ||
| */ | ||
| private function get_details_data( $post_id ) { | ||
| global $wpdb; | ||
| $table_name = edac_get_valid_table_name( $wpdb->prefix . 'accessibility_checker' ); | ||
| $siteid = get_current_blog_id(); | ||
|
|
||
| if ( ! $table_name ) { | ||
| throw new \Exception( esc_html__( 'Invalid table name', 'accessibility-checker' ) ); | ||
| } | ||
|
|
||
| $rules = edac_register_rules(); | ||
| if ( ! $rules ) { | ||
| return [ | ||
| 'errors' => [], | ||
| 'warnings' => [], | ||
| 'passed' => [], | ||
| ]; | ||
| } | ||
|
|
||
| // If ANWW is active remove link_blank for details. | ||
| if ( defined( 'ANWW_VERSION' ) ) { | ||
| $rules = edac_remove_element_with_value( $rules, 'slug', 'link_blank' ); | ||
| } | ||
|
|
||
| $passed_rules = []; | ||
| $error_rules = edac_filter_by_value( $rules, 'rule_type', 'error' ); | ||
| $warning_rules = edac_filter_by_value( $rules, 'rule_type', 'warning' ); | ||
|
|
||
| // Process both error and warning rules. | ||
| $error_rules = $this->process_rules_for_details( $error_rules, $post_id, $table_name, $siteid, $passed_rules ); | ||
| $warning_rules = $this->process_rules_for_details( $warning_rules, $post_id, $table_name, $siteid, $passed_rules ); | ||
|
|
||
| return [ | ||
| 'errors' => array_values( $error_rules ), | ||
| 'warnings' => array_values( $warning_rules ), | ||
| 'passed' => $passed_rules, | ||
| ]; | ||
| } | ||
|
|
||
| /** | ||
| * Process rules and fetch issue details from the database. | ||
| * | ||
| * @since 1.xx.x | ||
| * | ||
| * @param array $rules The rules to process. | ||
| * @param int $post_id The post ID. | ||
| * @param string $table_name The database table name. | ||
| * @param int $siteid The site/blog ID. | ||
| * @param array &$passed_rules Reference to passed rules array (populated by this method). | ||
| * | ||
| * @return array The processed rules with counts and details. | ||
| */ | ||
| private function process_rules_for_details( $rules, $post_id, $table_name, $siteid, &$passed_rules ) { | ||
| global $wpdb; | ||
|
|
||
| foreach ( $rules as $key => $rule ) { | ||
| // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching | ||
| $results = $wpdb->get_results( | ||
| $wpdb->prepare( | ||
| 'SELECT id, postid, object, ruletype, ignre, ignre_user, ignre_date, ignre_comment FROM %i where postid = %d and rule = %s and siteid = %d and ignre = %d', | ||
| $table_name, | ||
| $post_id, | ||
| $rule['slug'], | ||
| $siteid, | ||
| 0 | ||
| ), | ||
| ARRAY_A | ||
| ); | ||
| $count = count( $results ); | ||
|
|
||
| if ( $count ) { | ||
| $rules[ $key ]['count'] = $count; | ||
| $rules[ $key ]['details'] = $results; | ||
| } else { | ||
| $rule['count'] = 0; | ||
| $passed_rules[] = $rule; | ||
| unset( $rules[ $key ] ); | ||
| } | ||
| } | ||
|
|
||
| return $rules; | ||
| } | ||
|
|
||
| /** | ||
| * Get readability data for a post. | ||
| * | ||
| * @since 1.xx.x | ||
| * | ||
| * @param int $post_id The post ID. | ||
| * | ||
| * @throws \Exception If the post is not found. | ||
| * @return array | ||
| */ | ||
| private function get_readability_data( $post_id ) { | ||
| $simplified_summary = (string) get_post_meta( $post_id, '_edac_simplified_summary', true ); | ||
| $simplified_summary_position = get_option( 'edac_simplified_summary_position', false ); | ||
|
|
||
| $content_post = get_post( $post_id ); | ||
| if ( ! $content_post ) { | ||
| throw new \Exception( esc_html__( 'Post not found', 'accessibility-checker' ) ); | ||
| } | ||
|
|
||
| $content = $content_post->post_content; | ||
| $content = apply_filters( 'the_content', $content ); | ||
|
|
||
| /** | ||
| * Filter the content used for reading grade readability analysis. | ||
| * | ||
| * @since 1.4.0 | ||
| * | ||
| * @param string $content The content to be filtered. | ||
| * @param int $post_id The post ID. | ||
| */ | ||
| $content = apply_filters( 'edac_filter_readability_content', $content, $post_id ); | ||
| $content = wp_filter_nohtml_kses( $content ); | ||
| $content = str_replace( ']]>', ']]>', $content ); | ||
|
|
||
| // Get readability metadata. | ||
| $edac_summary = get_post_meta( $post_id, '_edac_summary', true ); | ||
| $post_grade_readability = isset( $edac_summary['readability'] ) ? $edac_summary['readability'] : 0; | ||
| $post_grade = (int) filter_var( $post_grade_readability, FILTER_SANITIZE_NUMBER_INT ); | ||
| $post_grade_failed = $post_grade >= 9; // Treat Flesch-Kincaid grade 9+ (above roughly 8th-grade reading level recommended for plain language) as a readability failure. | ||
|
|
||
| $simplified_summary_grade = 0; | ||
| if ( class_exists( 'DaveChild\TextStatistics\TextStatistics' ) ) { | ||
| $text_statistics = new \DaveChild\TextStatistics\TextStatistics(); | ||
| $simplified_summary_grade = (int) floor( $text_statistics->fleschKincaidGradeLevel( $simplified_summary ) ); | ||
| } | ||
|
|
||
| $simplified_summary_grade_failed = $simplified_summary_grade >= 9; | ||
| $simplified_summary_prompt = get_option( 'edac_simplified_summary_prompt' ); | ||
|
|
||
| return [ | ||
| 'post_grade' => $post_grade, | ||
| 'post_grade_readability' => $post_grade_readability, | ||
| 'post_grade_failed' => $post_grade_failed, | ||
| 'simplified_summary' => $simplified_summary, | ||
| 'simplified_summary_grade' => $simplified_summary_grade, | ||
| 'simplified_summary_grade_failed' => $simplified_summary_grade_failed, | ||
| 'simplified_summary_prompt' => $simplified_summary_prompt, | ||
| 'simplified_summary_position' => $simplified_summary_position, | ||
| 'content_length' => strlen( $content ), | ||
| ]; | ||
| } | ||
|
Comment on lines
+824
to
+1288
|
||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.