' . esc_html__( 'Settings', 'thirddraft' ) . '' ); return $links; } /** * The page's stylesheet and script, on this page only. * * @since 1.0.0 * * @param string $hook The current screen's hook suffix. * @return void */ public static function assets( $hook ) { if ( '' === self::$hook || $hook !== self::$hook ) { return; } $base = plugins_url( 'assets/', THIRDDRAFT_FILE ); wp_enqueue_style( 'thirddraft-admin', $base . 'admin.css', array(), THIRDDRAFT_VERSION ); wp_enqueue_script( 'thirddraft-admin', $base . 'admin.js', array(), THIRDDRAFT_VERSION, true ); wp_localize_script( 'thirddraft-admin', 'thirddraftSettings', array( 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'pageUrl' => ThirdDraft_Connection::page_url(), 'plainAdmin' => __( 'This admin page is open over HTTP. After connecting, ThirdDraft returns you to it over HTTPS, so open the admin with https:// first, or ask your host to turn HTTPS on for it.', 'thirddraft' ), 'stateNonce' => wp_create_nonce( 'thirddraft_state' ), 'checksNonce' => wp_create_nonce( 'thirddraft_checks' ), 'polls' => (int) ( self::CONNECT_WAIT / 3 ), ) ); } /** * ThirdDraft sends the browser back here with a code, or to say the * person cancelled. The state made for this person stands in for a nonce. * * @since 1.0.0 * * @return void */ public static function returned() { // phpcs:disable WordPress.Security.NonceVerification.Recommended -- ThirdDraft's return is checked against the state kept for this user. if ( ! isset( $_GET['state'] ) || ( ! isset( $_GET['thirddraft_code'] ) && ! isset( $_GET['thirddraft_error'] ) ) ) { return; } if ( ! current_user_can( 'manage_options' ) ) { return; } $state = sanitize_text_field( wp_unslash( $_GET['state'] ) ); if ( ! isset( $_GET['thirddraft_code'] ) ) { ThirdDraft_Connection::cancelled( $state ); self::back( 'cancelled' ); } $result = ThirdDraft_Connection::finish( sanitize_text_field( wp_unslash( $_GET['thirddraft_code'] ) ), $state ); // phpcs:enable ThirdDraft_Checks::forget(); self::back( 'connected' === $result ? '' : $result ); } /** * Connect to ThirdDraft. * * @since 1.0.0 * * @return void */ public static function connect() { self::guard( 'thirddraft_connect' ); $url = ThirdDraft_Connection::begin(); add_filter( 'allowed_redirect_hosts', array( __CLASS__, 'allow_app_host' ) ); wp_safe_redirect( $url ); exit; } /** * Try again: ask ThirdDraft for its test once more. * * @since 1.0.0 * * @return void */ public static function retest() { self::guard( 'thirddraft_retest' ); $result = ThirdDraft_Connection::retest(); ThirdDraft_Checks::forget(); self::back( 'queued' === $result ? '' : 'retest_' . $result ); } /** * Disconnect. * * @since 1.0.0 * * @return void */ public static function disconnect() { self::guard( 'thirddraft_disconnect' ); ThirdDraft_Connection::disconnect(); ThirdDraft_Checks::forget(); self::back( 'disconnected' ); } /** * Saves Post as, Category and IndexNow. * * @since 1.0.0 * @since 1.1.0 IndexNow. * * @return void */ public static function save() { self::guard( 'thirddraft_save' ); // phpcs:disable WordPress.Security.NonceVerification.Missing -- verified in guard(). $author = isset( $_POST['thirddraft_author'] ) ? absint( $_POST['thirddraft_author'] ) : 0; $category = isset( $_POST['thirddraft_category'] ) ? absint( $_POST['thirddraft_category'] ) : 0; $indexnow = ! empty( $_POST['thirddraft_indexnow'] ); // phpcs:enable if ( $author && ! user_can( $author, 'publish_posts' ) ) { $author = 0; } if ( $category && ! term_exists( $category, 'category' ) ) { $category = 0; } ThirdDraft_Store::save_settings( $author, $category, $indexnow ); self::back( 'saved' ); } /** * The connection's state, for the page while it waits. * * @since 1.0.0 * * @return void */ public static function ajax_state() { check_ajax_referer( 'thirddraft_state', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } wp_send_json_success( array( 'state' => self::state() ) ); } /** * The checks that fail. * * @since 1.0.0 * * @return void */ public static function ajax_checks() { check_ajax_referer( 'thirddraft_checks', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } wp_send_json_success( array( 'failures' => ThirdDraft_Checks::failures() ) ); } /** * Lets the browser be sent to ThirdDraft. * * @since 1.0.0 * * @param string[] $hosts The hosts allowed. * @return string[] */ public static function allow_app_host( $hosts ) { $hosts[] = ThirdDraft_Connection::app_host(); return $hosts; } /** * Where the connection stands, from what really arrived. * * @since 1.0.0 * * @return string 'disconnected', 'connecting', 'unreached', 'refused' or 'connected'. */ public static function state() { $connection = ThirdDraft_Store::connection(); if ( empty( $connection ) ) { return 'disconnected'; } $status = ThirdDraft_Store::status(); $pending = isset( $connection['pending_since'] ) ? (int) $connection['pending_since'] : 0; if ( null !== $status['refusal'] ) { return 'refused'; } if ( $pending && $status['last_ok_at'] < $pending ) { return time() - $pending < self::CONNECT_WAIT ? 'connecting' : 'unreached'; } return 'connected'; } /** * The page. * * @since 1.0.0 * * @return void */ public static function render() { if ( ! current_user_can( 'manage_options' ) ) { return; } $state = self::state(); ?>
>
roles as $role => $details ) { if ( ! empty( $details['capabilities']['publish_posts'] ) ) { $roles[] = $role; } } if ( empty( $roles ) ) { return array(); } $users = get_users( array( 'role__in' => $roles, 'orderby' => 'display_name', 'order' => 'ASC', 'number' => 200, ) ); return is_array( $users ) ? $users : array(); } /** * The recent articles, newest first: from the log, then from the posts * themselves when the log has fewer. * * @return void */ private static function recent_articles() { $rows = self::recent_rows(); if ( empty( $rows ) ) { return; } ?> */ private static function recent_rows() { $rows = array(); $seen = array(); foreach ( ThirdDraft_Store::log() as $entry ) { if ( count( $rows ) >= self::RECENT ) { break; } $post = get_post( isset( $entry['post'] ) ? (int) $entry['post'] : 0 ); $rows[] = array( 'post' => $post instanceof WP_Post ? $post : null, 'at' => isset( $entry['at'] ) ? (int) $entry['at'] : 0, ); $seen[ isset( $entry['article'] ) ? (int) $entry['article'] : 0 ] = true; } if ( count( $rows ) >= self::RECENT ) { return $rows; } $posts = get_posts( array( 'post_type' => 'post', 'post_status' => array( 'publish', 'future', 'draft', 'pending', 'private' ), 'meta_key' => ThirdDraft_Post_Writer::ARTICLE, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_key -- twenty posts, on this page only. 'orderby' => 'modified', 'order' => 'DESC', 'numberposts' => self::RECENT, 'no_found_rows' => true, ) ); foreach ( $posts as $post ) { if ( count( $rows ) >= self::RECENT ) { break; } $article = (int) get_post_meta( $post->ID, ThirdDraft_Post_Writer::ARTICLE, true ); if ( isset( $seen[ $article ] ) ) { continue; } $seen[ $article ] = true; $rows[] = array( 'post' => $post, 'at' => (int) strtotime( $post->post_modified_gmt . ' UTC' ), ); } return $rows; } /** * A post's status, in words. * * @param WP_Post|null $post The post. * @return string */ private static function post_status_label( $post ) { if ( ! $post instanceof WP_Post ) { return __( 'Deleted', 'thirddraft' ); } $labels = array( 'publish' => __( 'Published', 'thirddraft' ), 'future' => __( 'Scheduled', 'thirddraft' ), 'draft' => __( 'Draft', 'thirddraft' ), 'pending' => __( 'Pending review', 'thirddraft' ), 'private' => __( 'Private', 'thirddraft' ), 'trash' => __( 'In the trash', 'thirddraft' ), ); return isset( $labels[ $post->post_status ] ) ? $labels[ $post->post_status ] : $post->post_status; } /** * A one-button form for an action. * * @param string $action The admin-post action. * @param string $label The button. * @param string $classes The button's classes. * @return void */ private static function action_form( $action, $label, $classes ) { ?> array( 'success', __( 'Saved.', 'thirddraft' ) ), 'disconnected' => array( 'success', __( 'Disconnected. The articles already here stay.', 'thirddraft' ) ), 'cancelled' => array( 'info', __( 'Connecting was cancelled.', 'thirddraft' ) ), 'expired' => array( 'error', __( 'Connecting took too long. Try again.', 'thirddraft' ) ), 'mismatch' => array( 'error', __( 'That didn’t work. Click Connect to ThirdDraft again.', 'thirddraft' ) ), 'refused' => array( 'error', __( 'ThirdDraft didn’t accept the connection. Try again.', 'thirddraft' ) ), 'busy' => array( 'warning', __( 'ThirdDraft is busy. Try again in a minute.', 'thirddraft' ) ), 'retest_busy' => array( 'warning', __( 'ThirdDraft is busy. Try again in a minute.', 'thirddraft' ) ), 'unreachable' => array( 'error', __( 'This site couldn’t reach ThirdDraft. Try again in a minute.', 'thirddraft' ) ), 'retest_refused' => array( 'error', __( 'ThirdDraft doesn’t recognise this connection any more. Connect again.', 'thirddraft' ) ), 'retest_unreachable' => array( 'error', __( 'This site couldn’t reach ThirdDraft. Try again in a minute.', 'thirddraft' ) ), ); if ( ! isset( $notices[ $name ] ) ) { return; } printf( '%2$s