Howseen: paste the token (connects at once), test, disconnect, and choose author, category and * featured image. Only administrators (manage_options); every form has its own nonce. */ if ( ! defined( 'ABSPATH' ) ) { exit; } add_action( 'admin_menu', 'howseen_admin_menu' ); add_action( 'admin_post_howseen_connect', 'howseen_handle_connect' ); add_action( 'admin_post_howseen_test', 'howseen_handle_test' ); add_action( 'admin_post_howseen_disconnect', 'howseen_handle_disconnect' ); add_action( 'admin_post_howseen_settings', 'howseen_handle_settings' ); add_filter( 'plugin_action_links_' . plugin_basename( dirname( __DIR__ ) . '/howseen.php' ), 'howseen_action_links' ); function howseen_admin_menu() { add_options_page( 'Howseen', 'Howseen', 'manage_options', 'howseen', 'howseen_settings_page' ); } function howseen_action_links( $links ) { array_unshift( $links, '' . esc_html( howseen_t( 'title' ) ) . '' ); return $links; } /** Common guard of every form: administrator only (each handler checks its own nonce right after). */ function howseen_guard() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'Sorry, you are not allowed to do that.', 'howseen' ), 403 ); } } function howseen_back() { wp_safe_redirect( admin_url( 'options-general.php?page=howseen' ) ); exit; } function howseen_handle_connect() { howseen_guard(); check_admin_referer( 'howseen_connect' ); $token = isset( $_POST['howseen_token'] ) ? trim( sanitize_text_field( wp_unslash( $_POST['howseen_token'] ) ) ) : ''; if ( ! howseen_token_valid_format( $token ) ) { howseen_set_status( false, howseen_t( 'err_token_format' ) ); howseen_back(); } if ( $token !== howseen_token() ) { delete_option( 'howseen_paired' ); // a new token is paired on its own } update_option( 'howseen_token', $token, false ); // The administrator who connects is the default author (changeable below). $author = (int) get_option( 'howseen_author', 0 ); if ( ! $author || ! user_can( $author, 'publish_posts' ) ) { update_option( 'howseen_author', get_current_user_id(), false ); } $r = howseen_register(); howseen_set_status( $r['ok'], $r['message'] ); howseen_back(); } function howseen_handle_test() { howseen_guard(); check_admin_referer( 'howseen_test' ); $r = howseen_register(); howseen_set_status( $r['ok'], $r['message'] ); howseen_back(); } function howseen_handle_disconnect() { howseen_guard(); check_admin_referer( 'howseen_disconnect' ); delete_option( 'howseen_token' ); delete_option( 'howseen_paired' ); wp_clear_scheduled_hook( 'howseen_send_reads' ); howseen_set_status( false, howseen_t( 'disconnected' ) ); howseen_back(); } function howseen_handle_settings() { howseen_guard(); check_admin_referer( 'howseen_settings' ); $author = isset( $_POST['howseen_author'] ) ? absint( $_POST['howseen_author'] ) : 0; if ( $author && user_can( $author, 'publish_posts' ) ) { update_option( 'howseen_author', $author, false ); } $cat = isset( $_POST['howseen_category'] ) ? absint( $_POST['howseen_category'] ) : 0; update_option( 'howseen_category', ( $cat && term_exists( $cat, 'category' ) ) ? $cat : 0, false ); update_option( 'howseen_featured', empty( $_POST['howseen_featured'] ) ? '0' : '1', false ); wp_safe_redirect( admin_url( 'options-general.php?page=howseen&settings-updated=1' ) ); exit; } function howseen_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $token = howseen_token(); $status = get_option( 'howseen_status', array() ); $ok = howseen_paired(); $post = esc_url( admin_url( 'admin-post.php' ) ); ?>
|