PluginProbe
Broken Link Notifier / trunk
Broken Link Notifier vtrunk
1.3.8 1.3.7.6 trunk 1.3.0 1.3.1.1 1.3.2 1.3.3 1.3.4 1.3.5 1.3.6 1.3.7 1.3.7.1 1.3.7.2 1.3.7.3 1.3.7.4 1.3.7.5
broken-link-notifier / includes / scan.php

scan.php in Broken Link Notifier trunk, at includes/scan.php

179 lines 5.8 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Shared scan class file.
4 */
5
6 // Exit if accessed directly.
7 if ( !defined( 'ABSPATH' ) ) {
8 exit;
9 }
10
11
12 /**
13 * Initiate the class
14 */
15 add_action( 'init', function() {
16 new BLNOTIFIER_SCAN;
17 } );
18
19
20 /**
21 * Main plugin class.
22 */
23 class BLNOTIFIER_SCAN {
24
25 /**
26 * The key that is used to identify the ajax response
27 *
28 * @var string
29 */
30 private $ajax_key = 'blnotifier_scan';
31
32
33 /**
34 * Name of nonce used for ajax call
35 *
36 * @var string
37 */
38 private $nonce = 'blnotifier_scan';
39
40
41 /**
42 * Constructor
43 */
44 public function __construct() {
45
46 // Ajax
47 add_action( 'wp_ajax_'.$this->ajax_key, [ $this, 'ajax' ] );
48
49 // Enqueue script
50 add_action( 'admin_enqueue_scripts', [ $this, 'enqueue_scripts' ] );
51
52 } // End __construct()
53
54
55 /**
56 * Ajax call
57 *
58 * @return void
59 */
60 public function ajax() {
61 // Verify nonce
62 if ( !isset( $_REQUEST[ 'nonce' ] ) || !wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST[ 'nonce' ] ) ), $this->nonce ) ) {
63 exit( 'No naughty business please.' );
64 }
65
66 // Check permissions
67 if ( !(new BLNOTIFIER_HELPERS)->user_can_manage_broken_links() ) {
68 exit( 'Unauthorized access.' );
69 }
70
71 // Initiate helpers
72 $HELPERS = new BLNOTIFIER_HELPERS;
73
74 // Get the ID
75 $link = isset( $_REQUEST[ 'link' ] ) ? $HELPERS->sanitize_link( wp_unslash( $_REQUEST[ 'link' ] ) ) : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
76 $post_id = isset( $_REQUEST[ 'postID' ] ) ? absint( wp_unslash( $_REQUEST[ 'postID' ] ) ) : false;
77 $method = isset( $_REQUEST[ 'method' ] ) ? sanitize_key( wp_unslash( $_REQUEST[ 'method' ] ) ) : false;
78
79 // Make sure we have a source URL
80 if ( $link ) {
81
82 // Check status
83 $status = $HELPERS->check_link( $link );
84
85 // Add to results
86 $bad_status_codes = $HELPERS->get_bad_status_codes();
87 $warning_status_codes = $HELPERS->get_warning_status_codes();
88 $notify_status_codes = array_merge( $bad_status_codes, $warning_status_codes );
89 if ( in_array( $status[ 'code' ], $notify_status_codes ) ) {
90 (new BLNOTIFIER_RESULTS)->add( [
91 'type' => $status[ 'type' ],
92 'code' => $status[ 'code' ],
93 'text' => $status[ 'text' ],
94 'link' => $status[ 'link' ],
95 'source' => get_the_permalink( $post_id ),
96 'author' => get_current_user_id(),
97 'location' => 'content',
98 'method' => $method
99 ] );
100 }
101
102 // Return
103 $result[ 'type' ] = 'success';
104 $result[ 'status' ] = $status;
105 $result[ 'link' ] = $link;
106 $result[ 'post_id' ] = $post_id;
107
108 // Nope
109 } else {
110 $result[ 'type' ] = 'error';
111 $result[ 'msg' ] = 'No link found';
112 }
113
114 // Echo the result or redirect
115 if ( !empty( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) && strtolower( sanitize_key( wp_unslash( $_SERVER[ 'HTTP_X_REQUESTED_WITH' ] ) ) ) === 'xmlhttprequest' ) {
116 echo wp_json_encode( $result );
117 } else {
118 $referer = isset( $_SERVER[ 'HTTP_REFERER' ] ) ? filter_var( wp_unslash( $_SERVER[ 'HTTP_REFERER' ] ), FILTER_SANITIZE_URL ) : '';
119 header( 'Location: ' . $referer );
120 }
121
122 // We're done here
123 die();
124 } // End ajax()
125
126
127 /**
128 * Enqueue script
129 *
130 * @param string $screen
131 * @return void
132 */
133 public function enqueue_scripts( $screen ) {
134 // Only on these pages
135 $options_page = 'toplevel_page_'.BLNOTIFIER_TEXTDOMAIN;
136 $tab = (new BLNOTIFIER_HELPERS)->get_tab();
137
138 $is_scan_single_page = (
139 $screen === $options_page &&
140 $tab === 'scan-single' &&
141 isset( $_REQUEST[ '_wpnonce' ], $_REQUEST[ 'scan' ] ) &&
142 wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST[ '_wpnonce' ] ) ), 'blnotifier_scan_single' ) &&
143 sanitize_text_field( wp_unslash( $_REQUEST[ 'scan' ] ) )
144 );
145
146 $is_blinks_page = (
147 $screen === 'edit.php' &&
148 isset( $_REQUEST[ '_wpnonce' ], $_REQUEST[ 'blinks' ] ) &&
149 wp_verify_nonce( sanitize_text_field( wp_unslash( $_REQUEST[ '_wpnonce' ] ) ), 'blnotifier_blinks' ) &&
150 sanitize_key( wp_unslash( $_REQUEST[ 'blinks' ] ) ) === 'true'
151 );
152
153 if ( $is_scan_single_page || $is_blinks_page ) {
154 if ( !$tab ) {
155 $tab = 'scan-multi';
156 }
157
158 if ( $tab == 'scan-single' ) {
159 $post_id = url_to_postid( filter_var( wp_unslash( $_REQUEST[ 'scan' ] ), FILTER_SANITIZE_URL ) );
160 } else {
161 $post_id = false;
162 }
163
164 // Nonce
165 $nonce = wp_create_nonce( $this->nonce );
166
167 // Register, localize, and enqueue
168 $handle = 'blnotifier_'.str_replace( '-', '_', $tab ).'_script';
169 wp_enqueue_script( 'jquery' );
170 wp_register_script( $handle, site_url().BLNOTIFIER_PLUGIN_JS_PATH.$tab.'.js', [ 'jquery' ], BLNOTIFIER_SCRIPT_VERSION, true );
171 wp_localize_script( $handle, 'blnotifier_'.str_replace( '-', '_', $tab ), [
172 'post_id' => $post_id,
173 'nonce' => $nonce,
174 'ajaxurl' => admin_url( 'admin-ajax.php' )
175 ] );
176 wp_enqueue_script( $handle );
177 }
178 } // End enqueue_scripts()
179 }