$args Extra query arguments. * @return string Admin address. */ public static function url( array $args = array() ): string { return add_query_arg( array_merge( array( 'page' => self::SLUG ), $args ), admin_url( 'admin.php' ) ); } /** * Loads the style and script on this page. * * @param string $hook Admin page hook. * @return void */ public function enqueue( string $hook ): void { if ( 'agentwelcome_page_' . self::SLUG !== $hook ) { return; } Assets::style(); wp_enqueue_script( 'agentwelcome-admin', AGENTWELCOME_URL . 'assets/js/admin.js', array(), Assets::version( 'assets/js/admin.js' ), true ); wp_enqueue_script( 'agentwelcome-verify', AGENTWELCOME_URL . 'assets/js/verify.js', array( 'wp-api-fetch' ), Assets::version( 'assets/js/verify.js' ), true ); } /** * Prints the page with the tab that was asked for. * * @return void */ public function render(): void { if ( ! current_user_can( 'manage_options' ) ) { return; } $tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only tab switch. $tab = 'all' === $tab ? 'all' : 'one'; View::render( 'verify-page', array( 'tab' => $tab, 'tabs' => array( 'one' => array( __( 'One page', 'agentwelcome' ), self::url() ), 'all' => array( __( 'All pages', 'agentwelcome' ), self::url( array( 'tab' => 'all' ) ) ), ), 'notices' => $this->notices->data(), 'blocked' => $this->crawlers->blocked(), 'content' => 'all' === $tab ? array( $this, 'render_all' ) : array( $this, 'render_one' ), ) ); } /** * Prints the tab that checks one page, and the result when an address was given. * * @return void */ public function render_one(): void { $url = ''; $selector = ''; $strip = ''; $headings = ''; if ( isset( $_POST['agentwelcome_verify_url'] ) ) { check_admin_referer( self::NONCE ); $url = trim( sanitize_text_field( wp_unslash( $_POST['agentwelcome_verify_url'] ) ) ); $selector = isset( $_POST['agentwelcome_verify_selector'] ) ? sanitize_text_field( wp_unslash( $_POST['agentwelcome_verify_selector'] ) ) : ''; $strip = isset( $_POST['agentwelcome_verify_strip'] ) ? sanitize_textarea_field( wp_unslash( $_POST['agentwelcome_verify_strip'] ) ) : ''; $headings = isset( $_POST['agentwelcome_verify_headings'] ) ? sanitize_textarea_field( wp_unslash( $_POST['agentwelcome_verify_headings'] ) ) : ''; } elseif ( isset( $_GET['url'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check, nothing is changed. $url = trim( sanitize_text_field( wp_unslash( $_GET['url'] ) ) ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- Read-only check, nothing is changed. } View::render( 'verify', array( 'nonce' => self::NONCE, 'action' => self::url(), 'url' => $url, 'selector' => $selector, 'strip' => $strip, 'headings' => $headings, 'placeholder' => home_url( '/' ), ) ); if ( '' !== $url ) { $this->result( $url, $selector, $strip, $headings ); } } /** * Prints the tab that scans every page. * * @return void */ public function render_all(): void { $status = $this->scan->status(); $rows = array(); foreach ( $status['differs'] as $row ) { $rows[] = array( 'title' => $row['title'], 'changed' => $row['changed'], 'url' => self::url( array( 'url' => (string) get_permalink( $row['id'] ) ) ), ); } View::render( 'scan', array( 'status' => $status, 'rows' => $rows, 'inactive' => null !== $this->environment->reason(), 'cron_disabled' => $this->environment->cron_disabled(), 'action' => admin_url( 'admin-post.php' ), 'nonce' => self::SCAN_NONCE, 'started' => $status['started'] > 0 ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $status['started'] ) : '', 'finished' => $status['finished'] > 0 ? wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $status['finished'] ) : '', ) ); } /** * Handles the scan buttons: start, stop, update the pages that differ, or generate the missing ones. * * @return void */ public function handle_scan(): void { check_admin_referer( self::SCAN_NONCE ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do that.', 'agentwelcome' ), 403 ); } $action = isset( $_POST['scan_action'] ) ? sanitize_key( wp_unslash( $_POST['scan_action'] ) ) : ''; $notice = 'unknown'; $count = 0; if ( 'stop' === $action ) { $this->scan->stop(); $notice = 'scan_stopped'; } elseif ( ! $this->environment->is_active() ) { $notice = 'inactive'; } elseif ( 'start' === $action ) { $this->scan->start(); $notice = 'scan_started'; } elseif ( 'update_outdated' === $action ) { $ids = $this->scan->differing_ids(); $this->pages->queue( $ids ); $count = count( $ids ); $notice = 'queued'; } elseif ( 'generate_missing' === $action ) { $this->batch->start( false ); $notice = 'missing_started'; } wp_safe_redirect( self::url( array( 'tab' => 'all', 'agentwelcome_notice' => $notice, 'agentwelcome_count' => $count, ) ) ); exit; } /** * Checks the page at an address and prints the result. * * @param string $url Address of the page. * @param string $selector Content selectors to try for this check only. * @param string $strip Selectors of parts to remove for this check only. * @param string $headings Selectors with a heading level, to write as headings for this check only. * @return void */ private function result( string $url, string $selector, string $strip, string $headings ): void { $view = $this->urls->resolve_view( $url ); if ( null === $view ) { $this->notice( 'error', __( 'No page was found for that URL on this site.', 'agentwelcome' ) ); return; } $post = $view['post']; $paged = $view['paged']; $selectors = new Selector(); $overrides = array(); $content = implode( ', ', $selectors->parse_list( $selector )['valid'] ); $extra = $selectors->parse_list( $strip )['valid']; $levels = ( new HeadingRules( $selectors ) )->parse( $headings )['valid']; if ( '' !== $content ) { $overrides['selector'] = $content; } if ( ! empty( $extra ) ) { $overrides['strip'] = $extra; } if ( ! empty( $levels ) ) { $overrides['headings'] = $levels; } if ( ! empty( $overrides ) ) { $this->notice( 'info', __( 'The selectors below were applied to this check only. The comparison shows what would change if you saved them.', 'agentwelcome' ) ); } $result = $this->verifier->verify( $post, $overrides, true, $paged ); if ( null !== $result->page && RenderResult::FALLBACK === $result->page->source ) { $this->notice( 'warning', '' === $result->page->notice ? __( 'Generated from the post content only.', 'agentwelcome' ) : $result->page->notice ); } $permalink = (string) $this->urls->view_url( $post, $paged ); View::render( 'verify-result', array( 'post' => $post, 'title' => get_the_title( $post ), 'md_url' => $this->urls->markdown_url( $post, $paged ), 'result' => $result, 'update_url' => VerifyResult::NOT_SERVED === $result->state || $paged > 1 ? '' : $this->update_url( $post, $permalink ), 'stored_at' => null === $result->entry ? '' : wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), $result->entry->timestamp() ), 'applied' => ! empty( $overrides ), ) ); } /** * Address that regenerates the stored copy of a page and comes back to the check. * * @param WP_Post $post The page. * @param string $permalink Address of the page. * @return string The address with a nonce, or an empty string when the plugin is off. */ private function update_url( WP_Post $post, string $permalink ): string { if ( ! $this->environment->is_active() ) { return ''; } return wp_nonce_url( add_query_arg( array( 'action' => 'agentwelcome_regenerate_post', 'post' => $post->ID, 'agentwelcome_return' => rawurlencode( self::url( array( 'url' => $permalink ) ) ), ), admin_url( 'admin-post.php' ) ), 'agentwelcome_regenerate_' . $post->ID ); } /** * Prints a notice. * * @param string $type Notice type: error, info or warning. * @param string $message Text of the notice. * @return void */ private function notice( string $type, string $message ): void { printf( '

%2$s

', esc_attr( $type ), esc_html( $message ) ); } }