Suiko: one button to connect, the connection status, the publication mode, a disconnect button. Plus, in the * posts list, a notice and a row action on drafts that Suiko filed, with the one-click "Apply this update". */ if ( ! defined( 'ABSPATH' ) ) { exit; } add_action( 'admin_menu', function () { add_options_page( 'Suiko', 'Suiko', 'manage_options', 'suiko', 'suiko_settings_page' ); } ); add_action( 'admin_init', function () { register_setting( 'suiko', 'suiko_mode', array( 'type' => 'string', 'sanitize_callback' => function ( $v ) { return 'direct' === $v ? 'direct' : 'draft'; }, 'default' => 'draft' ) ); } ); /** "Connect to Suiko": creates a pairing code and sends the administrator to Suiko, which exchanges the code for a key and comes back. */ add_action( 'admin_post_suiko_connect', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'Not allowed.' ); } check_admin_referer( 'suiko_connect' ); update_option( 'suiko_author', get_current_user_id(), false ); $code = suiko_new_pairing_code(); $url = add_query_arg( array( 'code' => $code, 'site' => rawurlencode( home_url( '/' ) ), 'name' => rawurlencode( get_bloginfo( 'name' ) ), 'lang' => rawurlencode( get_locale() ), 'return' => rawurlencode( admin_url( 'options-general.php?page=suiko' ) ), ), rtrim( SUIKO_APP_URL, '/' ) . '/connect/wordpress' ); wp_safe_redirect( $url ); exit; } ); /** The pairing redirect leaves the site for the Suiko app: that host, and only it, is allowed. */ add_filter( 'allowed_redirect_hosts', function ( $hosts ) { $h = wp_parse_url( SUIKO_APP_URL, PHP_URL_HOST ); return $h ? array_merge( (array) $hosts, array( $h ) ) : $hosts; } ); add_action( 'admin_post_suiko_disconnect', function () { if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'Not allowed.' ); } check_admin_referer( 'suiko_disconnect' ); suiko_disconnect(); wp_safe_redirect( admin_url( 'options-general.php?page=suiko&suiko=disconnected' ) ); exit; } ); /** One click: the Suiko draft replaces its live article. */ add_action( 'admin_post_suiko_apply', function () { $draft = isset( $_GET['draft'] ) ? (int) $_GET['draft'] : 0; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- the nonce is checked on the next line, it needs the id. check_admin_referer( 'suiko_apply_' . $draft ); $of = (int) get_post_meta( $draft, '_suiko_update_of', true ); if ( ! $of || ! current_user_can( 'edit_post', $of ) || ! current_user_can( 'edit_post', $draft ) ) { wp_die( 'Not allowed.' ); } $r = suiko_apply_draft( $draft ); if ( is_wp_error( $r ) ) { wp_die( esc_html( $r->get_error_message() ) ); } wp_safe_redirect( add_query_arg( array( 'suiko' => 'applied', 'post' => $r ), admin_url( 'edit.php' ) ) ); exit; } ); /** The plugin page on getsuiko.com in the site's language. */ function suiko_docs_url() { $l = substr( get_locale(), 0, 2 ); return 'https://getsuiko.com/' . ( 'fr' === $l ? 'fr/integrations/wordpress/' : ( 'de' === $l ? 'de/integrationen/wordpress/' : 'integrations/wordpress/' ) ); } function suiko_settings_page() { $connected = suiko_is_connected(); $org = get_option( 'suiko_organisation' ); $since = get_option( 'suiko_connected_at' ); ?>
ID, '_suiko_update_of', true ); if ( $of && 'draft' === $post->post_status && current_user_can( 'edit_post', $of ) ) { $actions['suiko_apply'] = '' . esc_html__( 'Apply this Suiko update', 'suiko' ) . ''; } return $actions; } add_filter( 'display_post_states', function ( $states, $post ) { if ( (int) get_post_meta( $post->ID, '_suiko_update_of', true ) ) { $states['suiko'] = __( 'Suiko update', 'suiko' ); } return $states; }, 10, 2 ); add_action( 'admin_notices', function () { // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read only, to show a notice after the redirect. if ( isset( $_GET['suiko'] ) && 'applied' === $_GET['suiko'] && isset( $_GET['post'] ) ) { $link = get_permalink( (int) $_GET['post'] ); // phpcs:ignore WordPress.Security.NonceVerification.Recommended echo '' . esc_html__( 'Suiko update applied to the live article.', 'suiko' ) . ( $link ? ' ' . esc_html__( 'View', 'suiko' ) . '' : '' ) . '
' . esc_html( sprintf( $text, get_post_field( 'post_title', $of ) ) ) . ' ' . esc_html__( 'Apply this Suiko update', 'suiko' ) . '