Howseen, so WordPress applies that user's own rights (publish_posts, edit_post, * unfiltered_html) exactly as for a person writing in the editor. */ if ( ! defined( 'ABSPATH' ) ) { exit; } function howseen_register_routes() { $routes = array( 'ping' => 'howseen_rest_ping', 'posts' => 'howseen_rest_create', 'posts/find' => 'howseen_rest_find', 'posts/update' => 'howseen_rest_update', 'posts/trash' => 'howseen_rest_trash', 'posts/restore' => 'howseen_rest_restore', 'site/verify' => 'howseen_rest_site_verify', ); foreach ( $routes as $route => $cb ) { register_rest_route( HOWSEEN_NAMESPACE, '/' . $route, array( 'methods' => 'POST', 'callback' => $cb, 'permission_callback' => 'howseen_verify_request', ) ); } // Installed by Howseen right after the client approved the one-click connection (an administrator's application // password, WordPress's own authentication): Howseen hands its token over and pairs it on its side with a signed // ping, in a separate request (no call back to Howseen from here: one PHP worker would wait for itself). register_rest_route( HOWSEEN_NAMESPACE, '/claim', array( 'methods' => 'POST', 'callback' => 'howseen_rest_claim', 'permission_callback' => function () { return current_user_can( 'manage_options' ); }, ) ); } function howseen_rest_claim( $request ) { $p = howseen_params( $request ); $token = isset( $p['token'] ) && is_string( $p['token'] ) ? trim( $p['token'] ) : ''; if ( ! howseen_token_valid_format( $token ) ) { return new WP_Error( 'howseen_bad_token', 'Invalid Howseen token.', array( 'status' => 400 ) ); } if ( $token !== howseen_token() ) { delete_option( 'howseen_paired' ); } update_option( 'howseen_token', $token, false ); $author = (int) get_option( 'howseen_author', 0 ); if ( ! $author || ! user_can( $author, 'publish_posts' ) ) { update_option( 'howseen_author', get_current_user_id(), false ); } // An administrator handed it over for Howseen: confirmed (AI visits start); a wrong token is refused by Howseen. update_option( 'howseen_paired', '1', false ); howseen_set_status( true, howseen_t( 'ok_connected' ) ); return rest_ensure_response( array( 'ok' => true, 'site_url' => home_url(), 'endpoint' => rest_url( HOWSEEN_NAMESPACE . '/' ), 'plugin_version' => HOWSEEN_VERSION, ) ); } /** The user Howseen publishes as: the chosen author, if they may still publish. */ function howseen_author() { $id = (int) get_option( 'howseen_author', 0 ); $u = $id ? get_userdata( $id ) : false; if ( ! $u || ! user_can( $u, 'publish_posts' ) ) { return new WP_Error( 'howseen_no_author', 'No author allowed to publish is set in Settings > Howseen.', array( 'status' => 409 ) ); } return $u; } /** Runs the request as the chosen author (kses and capabilities follow that user). */ function howseen_act_as_author() { $u = howseen_author(); if ( is_wp_error( $u ) ) { return $u; } wp_set_current_user( $u->ID ); return $u; } /** JSON body of the request as an array. */ function howseen_params( $request ) { $p = json_decode( (string) $request->get_body(), true ); return is_array( $p ) ? $p : array(); } function howseen_rest_ping( $request ) { $u = howseen_author(); if ( is_wp_error( $u ) ) { return $u; } return rest_ensure_response( array( 'ok' => true, 'version' => HOWSEEN_VERSION, 'site_url' => home_url(), 'wp_version' => get_bloginfo( 'version' ), ) ); } /** FAQ pairs sent by Howseen, cleaned (plain text, bounded). */ function howseen_clean_faq( $faq ) { $out = array(); if ( ! is_array( $faq ) ) { return $out; } foreach ( array_slice( $faq, 0, 30 ) as $pair ) { if ( ! is_array( $pair ) || ! isset( $pair['q'], $pair['a'] ) ) { continue; } $q = sanitize_text_field( (string) $pair['q'] ); $a = sanitize_textarea_field( wp_strip_all_tags( (string) $pair['a'] ) ); if ( '' !== $q && '' !== $a ) { $out[] = array( 'q' => mb_substr( $q, 0, 300 ), 'a' => mb_substr( $a, 0, 1000 ), ); } } return $out; } /** Meta description + FAQ data, kept by this plugin and given to the SEO plugin in place (Yoast, Rank Math, SEOPress). */ function howseen_save_meta( $post_id, $p ) { if ( isset( $p['meta_description'] ) ) { $desc = sanitize_text_field( (string) $p['meta_description'] ); update_post_meta( $post_id, '_howseen_meta_description', $desc ); update_post_meta( $post_id, '_yoast_wpseo_metadesc', $desc ); update_post_meta( $post_id, 'rank_math_description', $desc ); update_post_meta( $post_id, '_seopress_titles_desc', $desc ); update_post_meta( $post_id, '_genesis_description', $desc ); // The SEO Framework } if ( isset( $p['faq'] ) ) { $faq = howseen_clean_faq( $p['faq'] ); if ( count( $faq ) >= 2 ) { update_post_meta( $post_id, '_howseen_faq', wp_slash( wp_json_encode( $faq ) ) ); } else { delete_post_meta( $post_id, '_howseen_faq' ); } } } /** * Featured image from the article's first image (setting on by default). Returns the content without that inline * image when it became the featured image (themes show the featured image above the post: never twice). */ function howseen_featured_image( $post_id, $image, $content ) { if ( '1' !== (string) get_option( 'howseen_featured', '1' ) || ! is_array( $image ) || empty( $image['url'] ) ) { return $content; } $url = esc_url_raw( (string) $image['url'], array( 'https' ) ); if ( '' === $url ) { return $content; } require_once ABSPATH . 'wp-admin/includes/media.php'; require_once ABSPATH . 'wp-admin/includes/file.php'; require_once ABSPATH . 'wp-admin/includes/image.php'; $alt = isset( $image['alt'] ) ? sanitize_text_field( (string) $image['alt'] ) : ''; // Bounded (6 s): Howseen waits 15 s for the whole publish; an image that takes longer stays inline instead. $short = function ( $args ) { $args['timeout'] = 6; return $args; }; add_filter( 'http_request_args', $short ); $att = media_sideload_image( $url, $post_id, $alt, 'id' ); remove_filter( 'http_request_args', $short ); if ( is_wp_error( $att ) ) { return $content; // The inline image stays: the article keeps its picture either way. } set_post_thumbnail( $post_id, $att ); if ( '' !== $alt ) { update_post_meta( $att, '_wp_attachment_image_alt', $alt ); } $quoted = preg_quote( esc_url( $url ), '/' ) . '|' . preg_quote( $url, '/' ); $out = preg_replace( '/

\s*]*src="(?:' . $quoted . ')"[^>]*>\s*<\/p>|]*src="(?:' . $quoted . ')"[^>]*>/i', '', $content, 1 ); return is_string( $out ) ? $out : $content; } /** The post Howseen already created for this article (a retry after a timeout never creates a second one). */ function howseen_post_by_ref( $ref ) { if ( '' === $ref ) { return 0; } $ids = get_posts( array( 'post_type' => 'post', 'post_status' => array( 'publish', 'future', 'draft', 'pending', 'private' ), 'meta_key' => '_howseen_ref', // phpcs:ignore WordPress.DB.SlowDBQuery 'meta_value' => $ref, // phpcs:ignore WordPress.DB.SlowDBQuery 'fields' => 'ids', 'numberposts' => 1, ) ); return $ids ? (int) $ids[0] : 0; } function howseen_post_answer( $post_id ) { return rest_ensure_response( array( 'id' => (int) $post_id, 'link' => get_permalink( $post_id ), 'status' => get_post_status( $post_id ), ) ); } function howseen_rest_create( $request ) { $u = howseen_act_as_author(); if ( is_wp_error( $u ) ) { return $u; } $p = howseen_params( $request ); $title = isset( $p['title'] ) ? sanitize_text_field( (string) $p['title'] ) : ''; $html = isset( $p['content'] ) ? (string) $p['content'] : ''; if ( '' === $title || '' === trim( $html ) ) { return new WP_Error( 'howseen_bad_request', 'Title and content are required.', array( 'status' => 400 ) ); } $ref = isset( $p['ref'] ) ? sanitize_text_field( (string) $p['ref'] ) : ''; $id = howseen_post_by_ref( $ref ); if ( $id ) { // Created by an earlier attempt that stopped half way: finish it (online), never a second post. if ( 'publish' !== get_post_status( $id ) ) { wp_update_post( array( 'ID' => $id, 'post_status' => 'publish' ) ); } return howseen_post_answer( $id ); } $arr = array( 'post_title' => $title, 'post_content' => $html, 'post_status' => 'publish', 'post_type' => 'post', 'post_author' => $u->ID, ); if ( ! empty( $p['slug'] ) ) { $arr['post_name'] = sanitize_title( (string) $p['slug'] ); } if ( ! empty( $p['excerpt'] ) ) { $arr['post_excerpt'] = sanitize_textarea_field( (string) $p['excerpt'] ); } $cat = (int) get_option( 'howseen_category', 0 ); if ( $cat > 0 && term_exists( $cat, 'category' ) ) { $arr['post_category'] = array( $cat ); } // Created as a draft first: meta, FAQ and featured image are in place before the post goes online. $arr['post_status'] = 'draft'; $id = wp_insert_post( wp_slash( $arr ), true ); if ( is_wp_error( $id ) ) { $id->add_data( array( 'status' => 500 ) ); return $id; } if ( '' !== $ref ) { update_post_meta( $id, '_howseen_ref', $ref ); } howseen_save_meta( $id, $p ); $content = howseen_featured_image( $id, isset( $p['image'] ) ? $p['image'] : null, $html ); $done = wp_update_post( wp_slash( array( 'ID' => $id, 'post_content' => $content, 'post_status' => 'publish', ) ), true ); if ( is_wp_error( $done ) ) { $done->add_data( array( 'status' => 500 ) ); return $done; } return howseen_post_answer( $id ); } /** A post or page of this site, by its public URL. */ function howseen_rest_find( $request ) { $u = howseen_act_as_author(); if ( is_wp_error( $u ) ) { return $u; } $p = howseen_params( $request ); $url = isset( $p['url'] ) ? esc_url_raw( (string) $p['url'] ) : ''; $id = $url ? url_to_postid( $url ) : 0; $post = $id ? get_post( $id ) : null; if ( ! $post || ! in_array( $post->post_type, array( 'post', 'page' ), true ) || 'publish' !== $post->post_status ) { return rest_ensure_response( array( 'found' => false ) ); } $out = array( 'found' => true, 'id' => (int) $post->ID, 'kind' => 'page' === $post->post_type ? 'pages' : 'posts', 'link' => get_permalink( $post ), ); if ( isset( $p['context'] ) && 'edit' === $p['context'] ) { if ( ! current_user_can( 'edit_post', $post->ID ) ) { return new WP_Error( 'howseen_forbidden', 'The Howseen author may not edit this page.', array( 'status' => 409 ) ); } $out['raw'] = $post->post_content; $out['unfiltered'] = current_user_can( 'unfiltered_html' ); } return rest_ensure_response( $out ); } /** Updates a post or page in place (Content Refresh, inbound links). The address never changes. */ function howseen_rest_update( $request ) { $u = howseen_act_as_author(); if ( is_wp_error( $u ) ) { return $u; } $p = howseen_params( $request ); $id = isset( $p['id'] ) ? (int) $p['id'] : 0; $post = $id ? get_post( $id ) : null; if ( ! $post || ! in_array( $post->post_type, array( 'post', 'page' ), true ) ) { return new WP_Error( 'howseen_not_found', 'Post not found.', array( 'status' => 404 ) ); } if ( ! current_user_can( 'edit_post', $id ) ) { return new WP_Error( 'howseen_forbidden', 'The Howseen author may not edit this page.', array( 'status' => 409 ) ); } $arr = array( 'ID' => $id ); if ( isset( $p['content'] ) ) { $arr['post_content'] = (string) $p['content']; } if ( empty( $p['body_only'] ) ) { if ( ! empty( $p['title'] ) ) { $arr['post_title'] = sanitize_text_field( (string) $p['title'] ); } if ( isset( $p['excerpt'] ) ) { $arr['post_excerpt'] = sanitize_textarea_field( (string) $p['excerpt'] ); } } $done = wp_update_post( wp_slash( $arr ), true ); if ( is_wp_error( $done ) ) { $done->add_data( array( 'status' => 500 ) ); return $done; } if ( empty( $p['body_only'] ) ) { howseen_save_meta( $id, $p ); } clean_post_cache( $id ); return rest_ensure_response( array( 'id' => $id, 'link' => get_permalink( $id ), 'raw' => get_post_field( 'post_content', $id, 'raw' ), ) ); } /** Only the post Howseen created for this very article (same _howseen_ref) can be trashed or restored. */ function howseen_own_post( $request ) { $p = howseen_params( $request ); $id = isset( $p['id'] ) ? (int) $p['id'] : 0; $ref = isset( $p['ref'] ) ? sanitize_text_field( (string) $p['ref'] ) : ''; $post = $id ? get_post( $id ) : null; if ( ! $post || 'post' !== $post->post_type || '' === $ref || (string) get_post_meta( $id, '_howseen_ref', true ) !== $ref ) { return new WP_Error( 'howseen_not_found', 'Not a post published by Howseen.', array( 'status' => 404 ) ); } if ( ! current_user_can( 'delete_post', $id ) ) { return new WP_Error( 'howseen_forbidden', 'The Howseen author may not delete this post.', array( 'status' => 409 ) ); } return $post; } function howseen_rest_trash( $request ) { $u = howseen_act_as_author(); if ( is_wp_error( $u ) ) { return $u; } $post = howseen_own_post( $request ); if ( is_wp_error( $post ) ) { return $post; } if ( 'trash' !== $post->post_status && ! wp_trash_post( $post->ID ) ) { return new WP_Error( 'howseen_failed', 'WordPress refused to move the post to the trash.', array( 'status' => 500 ) ); } return rest_ensure_response( array( 'ok' => true ) ); } function howseen_rest_restore( $request ) { $u = howseen_act_as_author(); if ( is_wp_error( $u ) ) { return $u; } $post = howseen_own_post( $request ); if ( is_wp_error( $post ) ) { return $post; } if ( 'trash' === $post->post_status ) { // WordPress 5.6+ restores to draft by default: back to the status it had before the trash. $keep = function ( $new_status, $post_id, $previous ) { return $previous ? $previous : $new_status; }; add_filter( 'wp_untrash_post_status', $keep, 10, 3 ); $ok = wp_untrash_post( $post->ID ); remove_filter( 'wp_untrash_post_status', $keep, 10 ); if ( ! $ok ) { return new WP_Error( 'howseen_failed', 'WordPress refused to restore the post.', array( 'status' => 500 ) ); } } return rest_ensure_response( array( 'ok' => true, 'link' => get_permalink( $post->ID ), ) ); } /** * Ownership proof asked by Bing Webmaster Tools (msvalidate.01) or Google Search Console (google-site-verification), * printed in the head of every page (front.php). Keys absent = unchanged, "" = removed. Only the code itself is kept: * letters, digits, "-" and "_" (both engines issue such codes), so nothing else can be injected in the head. */ function howseen_rest_site_verify( $request ) { $p = howseen_params( $request ); $keys = array( 'bing' => 'howseen_verify_bing', 'google' => 'howseen_verify_google', ); // Every value checked before any is written: a refusal changes nothing. $set = array(); foreach ( $keys as $key => $option ) { if ( ! array_key_exists( $key, $p ) ) { continue; } $code = is_string( $p[ $key ] ) ? trim( $p[ $key ] ) : null; if ( null === $code || ( '' !== $code && 1 !== preg_match( '/^[A-Za-z0-9_-]{8,100}$/', $code ) ) ) { return new WP_Error( 'howseen_bad_code', 'Invalid verification code.', array( 'status' => 400 ) ); } $set[ $option ] = $code; } $changed = false; foreach ( $set as $option => $code ) { if ( (string) get_option( $option, '' ) !== $code ) { update_option( $option, $code, true ); $changed = true; } } if ( $changed ) { howseen_purge_page_cache(); } return rest_ensure_response( array( 'ok' => true, 'bing' => (string) get_option( 'howseen_verify_bing', '' ), 'google' => (string) get_option( 'howseen_verify_google', '' ), ) ); } /** The head changed: empty the common page caches so Bing and Google read the new tags now (each call only if present). */ function howseen_purge_page_cache() { if ( function_exists( 'rocket_clean_domain' ) ) { rocket_clean_domain(); } if ( function_exists( 'w3tc_flush_all' ) ) { w3tc_flush_all(); } if ( function_exists( 'wp_cache_clear_cache' ) ) { wp_cache_clear_cache(); } if ( class_exists( 'autoptimizeCache' ) && method_exists( 'autoptimizeCache', 'clearall' ) ) { autoptimizeCache::clearall(); } do_action( 'litespeed_purge_all' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- LiteSpeed Cache's own purge hook. do_action( 'breeze_clear_all_cache' ); // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- Breeze's own purge hook. if ( function_exists( 'sg_cachepress_purge_cache' ) ) { sg_cachepress_purge_cache(); } }