'array', 'sanitize_callback' => array( __CLASS__, 'sanitize_settings' ), 'default' => Labelwise::defaults(), ) ); } /** * Sanitize submitted settings. * * @param mixed $input Raw input. * @return array */ public static function sanitize_settings( $input ) { $input = is_array( $input ) ? $input : array(); $defaults = Labelwise::defaults(); $current = Labelwise::settings(); $out = array(); $public_types = array_diff( array_keys( get_post_types( array( 'public' => true ) ) ), array( 'attachment' ) ); $types = isset( $input['post_types'] ) && is_array( $input['post_types'] ) ? array_map( 'sanitize_key', $input['post_types'] ) : array(); $out['post_types'] = array_values( array_intersect( $types, $public_types ) ); $position = isset( $input['position'] ) ? sanitize_key( $input['position'] ) : 'top'; $out['position'] = in_array( $position, array( 'top', 'bottom', 'none' ), true ) ? $position : 'top'; $style = isset( $input['style'] ) ? sanitize_key( $input['style'] ) : 'light'; $out['style'] = in_array( $style, array( 'light', 'dark' ), true ) ? $style : 'light'; $out['show_reviewer'] = empty( $input['show_reviewer'] ) ? 0 : 1; $out['policy_url'] = isset( $input['policy_url'] ) ? esc_url_raw( trim( (string) $input['policy_url'] ) ) : ''; $out['labels'] = array(); foreach ( $defaults['labels'] as $key => $default_label ) { $value = isset( $input['labels'][ $key ] ) ? sanitize_text_field( (string) $input['labels'][ $key ] ) : ''; $out['labels'][ $key ] = '' === $value ? $default_label : $value; } // Pro-only keys are preserved here and handled by the filter below. $out['gate_enabled'] = isset( $current['gate_enabled'] ) ? (int) $current['gate_enabled'] : 0; $out['chatbot_text'] = isset( $current['chatbot_text'] ) ? $current['chatbot_text'] : $defaults['chatbot_text']; /** * Filter sanitized settings (used by the Pro package). * * @param array $out Sanitized settings. * @param array $input Raw input. */ return apply_filters( 'labelwise_sanitize_settings', $out, $input ); } /** * Settings page markup. * * @return void */ public static function render_settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $settings = Labelwise::settings(); $option = Labelwise::OPTION; $types = get_post_types( array( 'public' => true ), 'objects' ); unset( $types['attachment'] ); ?>

$label ) : ?>
$text ) : ?>
can_use_premium_code(); $upgrade_url = $labelwise_fs ? $labelwise_fs->get_upgrade_url() : ( defined( 'LABELWISE_UPGRADE_URL' ) ? LABELWISE_UPGRADE_URL : '' ); if ( ! $has_pro ) : ?>

$types, 'post_status' => array( 'publish', 'draft', 'pending', 'future', 'private' ), 'posts_per_page' => 1, 'fields' => 'ids', 'no_found_rows' => false, 'update_post_meta_cache' => false, 'update_post_term_cache' => false, // phpcs:ignore WordPress.DB.SlowDBQuery.slow_db_query_meta_query -- Small admin-only count. 'meta_query' => array( array( 'key' => Labelwise::META_STATUS, 'value' => $status, ), ), ) ); return (int) $query->found_posts; } /** * Register the editor meta box. * * @return void */ public static function register_meta_box() { foreach ( Labelwise::enabled_post_types() as $type ) { add_meta_box( 'labelwise_disclosure', __( 'AI disclosure', 'labelwise' ), array( __CLASS__, 'render_meta_box' ), $type, 'side', 'default' ); } } /** * Meta box markup. * * @param WP_Post $post Current post. * @return void */ public static function render_meta_box( $post ) { wp_nonce_field( 'labelwise_save_' . $post->ID, 'labelwise_nonce' ); $status = Labelwise::get_status( $post->ID ); $reviewer = (string) get_post_meta( $post->ID, Labelwise::META_REVIEWER, true ); ?>



post_type, Labelwise::enabled_post_types(), true ) ) { return; } $old = Labelwise::get_status( $post_id ); $new = isset( $_POST['labelwise_status'] ) ? sanitize_key( wp_unslash( $_POST['labelwise_status'] ) ) : 'none'; if ( ! array_key_exists( $new, Labelwise::statuses() ) ) { $new = 'none'; } /** * Filter the status about to be saved (used by the Pro package). * * @param string $new Requested status. * @param int $post_id Post ID. * @param string $old Previous status. */ $new = (string) apply_filters( 'labelwise_sanitize_status', $new, $post_id, $old ); $reviewer = isset( $_POST['labelwise_reviewer'] ) ? sanitize_text_field( wp_unslash( $_POST['labelwise_reviewer'] ) ) : ''; if ( 'human_reviewed' === $new && '' === $reviewer ) { $user = wp_get_current_user(); $reviewer = $user->exists() ? $user->display_name : ''; } if ( 'none' === $new ) { delete_post_meta( $post_id, Labelwise::META_STATUS ); delete_post_meta( $post_id, Labelwise::META_REVIEWER ); delete_post_meta( $post_id, Labelwise::META_TIME ); } else { update_post_meta( $post_id, Labelwise::META_STATUS, $new ); update_post_meta( $post_id, Labelwise::META_REVIEWER, $reviewer ); if ( 'human_reviewed' === $new ) { if ( 'human_reviewed' !== $old || ! get_post_meta( $post_id, Labelwise::META_TIME, true ) ) { update_post_meta( $post_id, Labelwise::META_TIME, time() ); } } else { delete_post_meta( $post_id, Labelwise::META_TIME ); } } if ( $old !== $new ) { /** * Fires when a post's disclosure status changes. * * @param int $post_id Post ID. * @param string $old Previous status. * @param string $new New status. * @param string $reviewer Reviewer name. */ do_action( 'labelwise_status_changed', $post_id, $old, $new, $reviewer ); } } /** * Add the status column to enabled post types. * * @return void */ public static function register_columns() { foreach ( Labelwise::enabled_post_types() as $type ) { add_filter( "manage_{$type}_posts_columns", array( __CLASS__, 'add_column' ) ); add_action( "manage_{$type}_posts_custom_column", array( __CLASS__, 'render_column' ), 10, 2 ); } } /** * Add a column. * * @param array $columns Columns. * @return array */ public static function add_column( $columns ) { $columns['labelwise'] = __( 'AI label', 'labelwise' ); return $columns; } /** * Render the column. * * @param string $column Column key. * @param int $post_id Post ID. * @return void */ public static function render_column( $column, $post_id ) { if ( 'labelwise' !== $column ) { return; } $status = Labelwise::get_status( $post_id ); $statuses = Labelwise::statuses(); echo 'none' === $status ? '—' : esc_html( $statuses[ $status ] ); } /** * Show a one-time notice when the Pro gate held a post back. * * @return void */ public static function gate_notice() { $key = 'labelwise_gate_' . get_current_user_id(); if ( ! get_transient( $key ) ) { return; } delete_transient( $key ); echo '

' . esc_html__( 'Labelwise: this AI-labelled content needs a human review before it can be published. It was saved as “Pending review”.', 'labelwise' ) . '

'; } }