' . esc_html( sprintf( __( 'by %s', 'statwrite' ), 'Mr Noctis' ) ) . ''; } /** * Prints the WP-Hub logo used to mark sections that come from the WP-Hub console. */ public static function wphub_mark() { echo 'WP-Hub'; } public static function init() { add_action( 'admin_menu', array( __CLASS__, 'menu' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'assets' ) ); add_action( 'admin_post_wphs_save_settings', array( __CLASS__, 'save_settings' ) ); add_action( 'admin_post_wphs_cloud_test', array( __CLASS__, 'cloud_test' ) ); add_action( 'admin_post_wphs_disconnect', array( __CLASS__, 'disconnect' ) ); add_action( 'admin_post_wphs_cloud_sync', array( __CLASS__, 'cloud_sync' ) ); add_action( 'admin_post_wphs_purge_local', array( __CLASS__, 'purge_local' ) ); add_action( 'admin_post_wphs_cloud_restore', array( __CLASS__, 'cloud_restore' ) ); add_action( 'admin_post_wphs_cloud_stay_local', array( __CLASS__, 'cloud_stay_local' ) ); add_action( 'admin_post_wphs_dismiss_national_net', array( __CLASS__, 'dismiss_national_net' ) ); add_action( 'admin_notices', array( __CLASS__, 'connection_notices' ) ); add_filter( 'admin_body_class', array( __CLASS__, 'body_class' ) ); add_action( 'admin_enqueue_scripts', array( __CLASS__, 'menu_icon_css' ) ); add_filter( 'plugin_action_links_' . plugin_basename( WPHS_FILE ), array( __CLASS__, 'action_links' ) ); } public static function menu_icon_css() { wp_enqueue_style( 'wphs-menu', WPHS_URL . 'admin/assets/menu.css', array(), WPHS_VERSION ); } public static function body_class( $classes ) { $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; if ( $screen && false !== strpos( (string) $screen->id, 'statwrite' ) ) { $classes .= ' wphs-admin'; } return $classes; } public static function menu() { $cap = 'manage_options'; add_menu_page( __( 'Statwrite', 'statwrite' ), __( 'Statwrite', 'statwrite' ), $cap, 'statwrite', array( __CLASS__, 'render_dashboard' ), self::menu_icon(), 58 // After Comments, just above Appearance, next to other plugin menus. ); add_submenu_page( 'statwrite', __( 'Dashboard', 'statwrite' ), __( 'Dashboard', 'statwrite' ), $cap, 'statwrite', array( __CLASS__, 'render_dashboard' ) ); add_submenu_page( 'statwrite', __( 'AI Writer', 'statwrite' ), __( 'AI Writer', 'statwrite' ), 'publish_posts', 'statwrite-writer', array( __CLASS__, 'render_writer' ) ); add_submenu_page( 'statwrite', __( 'Settings', 'statwrite' ), __( 'Settings', 'statwrite' ), $cap, 'statwrite-settings', array( __CLASS__, 'render_settings' ) ); } public static function assets( $hook ) { if ( false === strpos( (string) $hook, 'statwrite' ) ) { return; } wp_enqueue_style( 'wphs-admin', WPHS_URL . 'admin/assets/admin.css', array(), WPHS_VERSION ); wp_enqueue_script( 'wphs-admin', WPHS_URL . 'admin/assets/admin.js', array(), WPHS_VERSION, true ); $presets = array(); foreach ( WPHS_AI_Provider::presets() as $id => $preset ) { $presets[ $id ] = array( 'base' => $preset['base'], 'model' => $preset['model'], 'signup' => $preset['signup'], ); } wp_localize_script( 'wphs-admin', 'wphsAdmin', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'wphs_writer' ), 'presets' => $presets, 'prompt' => WPHS_Writer::default_system_prompt(), 'i18n' => array( 'working' => __( 'Working…', 'statwrite' ), 'testing' => __( 'Testing the connection…', 'statwrite' ), 'writing' => __( 'Writing the article. This can take one to three minutes; keep this page open.', 'statwrite' ), 'queued' => __( 'The console is writing the article. You can stay here or come back later.', 'statwrite' ), 'failed' => __( 'Request failed. Try again.', 'statwrite' ), 'confirmReset' => __( 'Replace the system prompt with the default text?', 'statwrite' ), 'confirmPurge' => __( 'Purge local analytics data? This cannot be undone.', 'statwrite' ), 'confirmOff' => __( 'Remove the site key and disconnect from the console?', 'statwrite' ), 'noModels' => __( 'The provider did not list models; type the model name.', 'statwrite' ), 'models' => __( 'models loaded — pick one from the list.', 'statwrite' ), 'preview' => __( 'Preview', 'statwrite' ), 'edit' => __( 'Edit', 'statwrite' ), 'sendDraft' => __( 'Send as draft', 'statwrite' ), 'publish' => __( 'Publish now', 'statwrite' ), 'view' => __( 'View post', 'statwrite' ), 'empty' => __( 'No articles yet. Write your first one above.', 'statwrite' ), 'words' => __( 'words', 'statwrite' ), 'close' => __( 'Close', 'statwrite' ), /* translators: Used as "3 of 10" for the remaining article quota. */ 'of' => __( 'of', 'statwrite' ), 'conn' => array( 'connected' => __( 'Connected', 'statwrite' ), 'error' => __( 'Error', 'statwrite' ), 'pending' => __( 'Pending', 'statwrite' ), ), 'status' => array( 'queued' => __( 'Queued', 'statwrite' ), 'generating' => __( 'Writing…', 'statwrite' ), 'ready' => __( 'Ready to send', 'statwrite' ), 'published' => __( 'Sent to site', 'statwrite' ), 'publish_failed' => __( 'Sending failed', 'statwrite' ), 'failed' => __( 'Failed', 'statwrite' ), ), ), ) ); } public static function action_links( $links ) { array_unshift( $links, '' . esc_html__( 'Settings', 'statwrite' ) . '' ); return $links; } public static function render_writer() { if ( ! current_user_can( 'publish_posts' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'statwrite' ) ); } require WPHS_DIR . 'admin/views/writer.php'; } private static function menu_icon() { return 'dashicons-chart-bar'; } public static function render_dashboard() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'statwrite' ) ); } $token_set = WPHS_Cloud::has_token(); $overview = null; $overview_error = ''; $overview_stale = false; if ( $token_set ) { $result = WPHS_Cloud::overview(); if ( $result['ok'] ) { $overview = $result['data']; $overview_stale = ! empty( $result['stale'] ); } else { $overview_error = (string) $result['message']; } } if ( 'cloud' === WPHS_Settings::mode() ) { $status = WPHS_Cloud::status(); require WPHS_DIR . 'admin/views/cloud.php'; return; } $spool_size = WPHS_Spool::size(); if ( $spool_size > 0 && $spool_size < 5 * MB_IN_BYTES && ( time() - (int) get_option( 'wphs_last_run', 0 ) ) > 30 ) { WPHS_Aggregator::run(); } $data = WPHS_Reports::dashboard( WPHS_Reports::requested_range() ); require WPHS_DIR . 'admin/views/dashboard.php'; } public static function render_settings() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'statwrite' ) ); } $settings = WPHS_Settings::all(); require WPHS_DIR . 'admin/views/settings.php'; } public static function save_settings() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'statwrite' ) ); } check_admin_referer( 'wphs_save_settings' ); $old_token = WPHS_Settings::value( 'console_token' ); // Read only the settings fields, each with the sanitizer for its type; WPHS_Settings::sanitize() then validates the values. $input = array(); if ( isset( $_POST['storage_mode'] ) ) { $input['storage_mode'] = sanitize_key( wp_unslash( $_POST['storage_mode'] ) ); } if ( isset( $_POST['console_token'] ) ) { $input['console_token'] = sanitize_text_field( wp_unslash( $_POST['console_token'] ) ); } foreach ( array( 'retention_detail_days', 'retention_daily_days', 'retention_totals_months' ) as $field ) { if ( isset( $_POST[ $field ] ) ) { $input[ $field ] = absint( $_POST[ $field ] ); } } foreach ( array( 'track_logged_in', 'respect_dnt', 'bot_js_check', 'delete_on_uninstall' ) as $field ) { $input[ $field ] = ! empty( $_POST[ $field ] ); } // Not on the form: keep the stored value. $input['exclude_404'] = (bool) WPHS_Settings::value( 'exclude_404' ); WPHS_Settings::save( $input ); delete_option( 'wphs_last_cleanup' ); // A new site key connects everything at once, article writing included. $new_token = WPHS_Settings::value( 'console_token' ); if ( '' !== $new_token && $new_token !== $old_token && class_exists( 'WPHS_Writer' ) ) { WPHS_Writer::link_console(); } wp_safe_redirect( add_query_arg( array( 'page' => 'statwrite-settings', 'updated' => 1, ), admin_url( 'admin.php' ) ) ); exit; } public static function cloud_test() { self::guard( 'wphs_cloud_test' ); $result = WPHS_Cloud::ping(); self::cloud_notice( $result[0] ? 'success' : 'error', $result[1] ); self::back(); } public static function disconnect() { self::guard( 'wphs_disconnect' ); WPHS_Settings::set_token( '' ); self::cloud_notice( 'success', __( 'The site key was removed and the console was disconnected.', 'statwrite' ) ); self::back(); } public static function cloud_sync() { self::guard( 'wphs_cloud_sync' ); $result = WPHS_Cloud::run( true ); if ( $result['ok'] && 'sent' === $result['message'] ) { $message = sprintf( /* translators: %1$s: number of sent events; %2$s: number of queued events. */ __( '%1$s events were sent to the console; %2$s events remain queued.', 'statwrite' ), number_format_i18n( $result['sent'] ), number_format_i18n( $result['pending'] ) ); } elseif ( $result['ok'] ) { $message = __( 'The queue was empty; nothing to send.', 'statwrite' ); } else { $map = array( 'not_cloud' => __( 'Cloud mode is not active.', 'statwrite' ), 'no_token' => __( 'The site connection key is not set.', 'statwrite' ), 'backoff' => __( 'The next attempt is not allowed yet; try again later.', 'statwrite' ), 'locked' => __( 'Another process is already running.', 'statwrite' ), ); $message = isset( $map[ $result['message'] ] ) ? $map[ $result['message'] ] : __( 'Sync failed: ', 'statwrite' ) . $result['message']; } self::cloud_notice( $result['ok'] ? 'success' : 'error', $message ); self::back(); } /** * Outage fallback and national-internet notices. * * The fallback notice is shown on Statwrite screens and the WordPress dashboard until the owner * decides; the national-internet warning is shown on Statwrite screens while the site is * connected to the console. */ public static function connection_notices() { if ( ! current_user_can( 'manage_options' ) ) { return; } $screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; $id = $screen ? (string) $screen->id : ''; $ours = false !== strpos( $id, 'statwrite' ); $fallback = WPHS_Cloud::fallback_state(); if ( $fallback && ( $ours || 'dashboard' === $id ) ) { $hours = max( 6, (int) round( ( (int) $fallback['at'] - (int) $fallback['since'] ) / HOUR_IN_SECONDS ) ); echo '

' . esc_html__( 'Statwrite: the WP-Hub console did not answer, so analytics switched to local mode.', 'statwrite' ) . '

'; echo esc_html( sprintf( /* translators: 1: hours without an answer, 2: date and time of the switch. */ __( 'There was no answer from the console for %1$s hours, so on %2$s visits started being stored on this host instead. Nothing was lost: queued visits were counted locally. This usually happens when international internet is cut (national internet) and hosts in Iran cannot reach the console.', 'statwrite' ), self::local_digits( number_format_i18n( $hours ) ), wp_date( get_option( 'date_format' ) . ' ' . get_option( 'time_format' ), (int) $fallback['at'] ) ) ); if ( ! empty( $fallback['error'] ) ) { echo '
' . esc_html__( 'Last error:', 'statwrite' ) . ' ' . esc_html( $fallback['error'] ) . ''; } echo '

'; echo '' . esc_html__( 'Test and return to cloud mode', 'statwrite' ) . ' '; echo '' . esc_html__( 'Stay in local mode', 'statwrite' ) . ''; echo '

'; } if ( $ours && WPHS_Cloud::has_token() && self::is_iran_site() && ! get_user_meta( get_current_user_id(), 'wphs_dismissed_national_net', true ) ) { $dismiss = wp_nonce_url( admin_url( 'admin-post.php?action=wphs_dismiss_national_net' ), 'wphs_dismiss_national_net' ); echo '

' . esc_html__( 'National internet warning:', 'statwrite' ) . ' '; echo esc_html__( 'If international internet is cut (national internet), hosts inside Iran lose access to the WP-Hub console and there is nothing we can do about it from our side. In that case Statwrite switches analytics to local storage on this host after 6 hours without an answer, so no visits are lost; writing and publishing articles through the console stays unavailable until the connection returns.', 'statwrite' ); echo ' ' . esc_html__( 'Got it, hide this', 'statwrite' ) . ''; echo '

'; } } /** * Persian/Arabic-script locales read Latin digits inside RTL text awkwardly. */ private static function local_digits( $text ) { $locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale(); if ( 0 !== strpos( (string) $locale, 'fa' ) ) { return (string) $text; } // Array form: the two-string form of strtr() is byte-based and would corrupt UTF-8. return strtr( (string) $text, array_combine( str_split( '0123456789' ), array( '۰', '۱', '۲', '۳', '۴', '۵', '۶', '۷', '۸', '۹' ) ) ); } /** * The national-internet warning only concerns sites hosted for Iran: Persian admin language * or the Tehran timezone. Other sites never see it. */ private static function is_iran_site() { $locale = function_exists( 'determine_locale' ) ? determine_locale() : get_locale(); return 0 === strpos( (string) $locale, 'fa' ) || 'Asia/Tehran' === wp_timezone_string(); } public static function dismiss_national_net() { self::guard( 'wphs_dismiss_national_net' ); update_user_meta( get_current_user_id(), 'wphs_dismissed_national_net', 1 ); $back = wp_get_referer(); wp_safe_redirect( $back ? $back : admin_url( 'admin.php?page=statwrite' ) ); exit; } public static function cloud_restore() { self::guard( 'wphs_cloud_restore' ); $result = WPHS_Cloud::restore_cloud(); self::cloud_notice( $result[0] ? 'success' : 'error', $result[0] ? __( 'The console answered; analytics are sent to the cloud again.', 'statwrite' ) /* translators: %s: error message. */ : sprintf( __( 'The console still does not answer, so local mode stays on: %s', 'statwrite' ), $result[1] ) ); self::back_to( 'statwrite-settings' ); } public static function cloud_stay_local() { self::guard( 'wphs_cloud_stay_local' ); WPHS_Cloud::dismiss_fallback(); self::cloud_notice( 'success', __( 'Analytics stay in local mode. You can switch back to cloud mode in the settings at any time.', 'statwrite' ) ); self::back_to( 'statwrite-settings' ); } private static function back_to( $page ) { wp_safe_redirect( admin_url( 'admin.php?page=' . $page ) ); exit; } public static function purge_local() { self::guard( 'wphs_purge_local' ); WPHS_Cloud::purge_local_data(); self::cloud_notice( 'success', __( 'Local analytics data was purged.', 'statwrite' ) ); self::back(); } private static function guard( $action ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have permission to access this page.', 'statwrite' ) ); } check_admin_referer( $action ); } private static function cloud_notice( $type, $message ) { set_transient( 'wphs_cloud_notice', array( 'type' => 'success' === $type ? 'success' : 'error', 'message' => (string) $message, ), 120 ); } private static function back() { $back = wp_get_referer(); if ( ! $back || false === strpos( $back, 'statwrite' ) ) { $back = admin_url( 'admin.php?page=statwrite-settings' ); } wp_safe_redirect( $back ); exit; } }