0 && user_can( $author, 'publish_posts' ) ) { return $author; } // Fallback: the oldest administrator. $admins = get_users( array( 'role' => 'administrator', 'orderby' => 'ID', 'order' => 'ASC', 'number' => 1, 'fields' => 'ID', ) ); return $admins ? (int) $admins[0] : 0; } /** * Default category ID (0 = none). * * @return int */ public static function default_category() { return (int) get_option( self::OPTION_CATEGORY, 0 ); } /** * Whether the tracker module is enabled. * * @return bool */ public static function tracker_enabled() { return '1' === get_option( self::OPTION_TRACKER, '0' ); } /** * AdMetric Tracking ID. * * @return string */ public static function tracking_id() { return (string) get_option( self::OPTION_TRACKING_ID, '' ); } // ------------------------------------------------------------------- // Admin UI. // ------------------------------------------------------------------- /** * Add the options page. */ public function add_menu() { add_options_page( __( 'AdMetric', 'admetric' ), __( 'AdMetric', 'admetric' ), 'manage_options', 'admetric', array( $this, 'render_page' ) ); } /** * Register module settings (token is handled separately, write-only). */ public function register_settings() { register_setting( 'admetric_modules', self::OPTION_PUBLISHING, array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_checkbox' ), 'default' => '1', ) ); register_setting( 'admetric_modules', self::OPTION_POST_STATUS, array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_post_status' ), 'default' => 'publish', ) ); register_setting( 'admetric_modules', self::OPTION_AUTHOR, array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ) ); register_setting( 'admetric_modules', self::OPTION_CATEGORY, array( 'type' => 'integer', 'sanitize_callback' => 'absint', 'default' => 0, ) ); register_setting( 'admetric_modules', self::OPTION_TRACKER, array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_checkbox' ), 'default' => '0', ) ); register_setting( 'admetric_modules', self::OPTION_TRACKING_ID, array( 'type' => 'string', 'sanitize_callback' => array( $this, 'sanitize_tracking_id' ), 'default' => '', ) ); } /** * Checkbox sanitizer. * * @param mixed $value Raw value. * @return string '1' or '0'. */ public function sanitize_checkbox( $value ) { return ( '1' === (string) $value ) ? '1' : '0'; } /** * Post status sanitizer. * * @param mixed $value Raw value. * @return string 'publish' or 'draft'. */ public function sanitize_post_status( $value ) { return ( 'draft' === (string) $value ) ? 'draft' : 'publish'; } /** * Tracking ID sanitizer: alphanumeric, dash, underscore. * * @param mixed $value Raw value. * @return string */ public function sanitize_tracking_id( $value ) { return preg_replace( '/[^A-Za-z0-9_\-]/', '', (string) $value ); } /** * Handle the "save token" form (admin-post). */ public function handle_save_token() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to manage this plugin.', 'admetric' ) ); } check_admin_referer( 'admetric_save_token' ); $token = isset( $_POST['admetric_token'] ) ? sanitize_text_field( wp_unslash( $_POST['admetric_token'] ) ) : ''; if ( '' !== $token ) { AdMetric_Token::save( $token ); } wp_safe_redirect( add_query_arg( 'admetric-connected', '1', $this->page_url() ) ); exit; } /** * Handle the "disconnect" form (admin-post). */ public function handle_disconnect() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to manage this plugin.', 'admetric' ) ); } check_admin_referer( 'admetric_disconnect' ); AdMetric_Token::clear(); AdMetric_Connect::clear_connection_metadata(); wp_safe_redirect( add_query_arg( 'admetric-disconnected', '1', $this->page_url() ) ); exit; } /** * Settings page URL. * * @return string */ private function page_url() { return admin_url( 'options-general.php?page=admetric' ); } /** * Render the settings page. */ public function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $connected = AdMetric_Token::is_connected(); $result = isset( $_GET['admetric-connect-result'] ) ? sanitize_key( wp_unslash( $_GET['admetric-connect-result'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended $messages = array( 'connected' => array( 'success', 'Сайт успешно подключён к AdMetric.' ), 'invalid_address' => array( 'error', 'Не удалось подтвердить адрес сайта. Начните подключение заново из плагина.' ), 'expired' => array( 'error', 'Ссылка подключения устарела. Нажмите «Подключить к AdMetric» ещё раз.' ), 'invalid_request' => array( 'error', 'Не удалось подтвердить запрос. Попробуйте подключить сайт заново.' ), 'activation_expired' => array( 'error', 'Подключение не завершено. Повторите попытку из плагина.' ), 'verification_failed' => array( 'error', 'Сайт не отвечает на проверку подключения. Убедитесь, что сайт доступен из интернета, и повторите.' ), 'rate_limited' => array( 'warning', 'Слишком много попыток подключения. Повторите через несколько минут.' ), 'service_error' => array( 'error', 'Не удалось завершить подключение. Повторите попытку позже.' ), ); ?>