' . esc_html__( 'Settings', 'aritoc' ) . '' ); return $links; } public static function register() { register_setting( 'aritoc_group', AriTOC_Settings::OPTION, array( 'type' => 'array', 'sanitize_callback' => array( 'AriTOC_Settings', 'sanitize' ), 'default' => AriTOC_Settings::defaults(), ) ); } public static function menu() { add_options_page( __( 'AriTOC', 'aritoc' ), __( 'AriTOC', 'aritoc' ), 'manage_options', self::PAGE, array( __CLASS__, 'render_page' ) ); } public static function assets( $hook ) { if ( 'settings_page_' . self::PAGE !== $hook ) { return; } wp_enqueue_style( 'aritoc-admin', ARITOC_URL . 'assets/css/admin.css', array( 'wp-color-picker' ), ARITOC_VERSION ); wp_enqueue_style( 'aritoc', ARITOC_URL . 'assets/css/aritoc.css', array(), ARITOC_VERSION ); wp_enqueue_script( 'aritoc', ARITOC_URL . 'assets/js/aritoc.js', array(), ARITOC_VERSION, true ); wp_enqueue_script( 'aritoc-admin', ARITOC_URL . 'assets/js/admin.js', array( 'jquery', 'wp-color-picker', 'aritoc' ), ARITOC_VERSION, true ); wp_localize_script( 'aritoc-admin', 'aritocAdmin', array( 'ajax' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( self::NONCE ), 'saved' => __( 'All changes saved', 'aritoc' ), 'unsaved' => __( 'Unsaved changes', 'aritoc' ), 'checking' => __( 'Checking…', 'aritoc' ), 'failed' => __( 'The check could not run. Reload the page and try again.', 'aritoc' ), 'contrast' => __( 'Hard to read for some visitors (contrast below 4.5:1):', 'aritoc' ), 'textLabel' => __( 'Text', 'aritoc' ), 'linkLabel' => __( 'Links', 'aritoc' ), 'updated' => __( 'Preview updated', 'aritoc' ), /* translators: %s: color name, such as Background. */ 'pickColor' => __( 'Select color: %s', 'aritoc' ), ) ); } /* ------------------------------------------------------------------ * * AJAX: preview and page check * ------------------------------------------------------------------ */ /** * Settings sent from the screen (unsaved values included), sanitized. * * @return array */ private static function posted_settings() { $raw = isset( $_POST[ AriTOC_Settings::OPTION ] ) && is_array( $_POST[ AriTOC_Settings::OPTION ] ) ? wp_unslash( $_POST[ AriTOC_Settings::OPTION ] ) : array(); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.NonceVerification.Missing -- sanitized next line; nonce checked by the callers. return wp_parse_args( AriTOC_Settings::sanitize( $raw ), AriTOC_Settings::defaults() ); } /** * Post chosen on the screen, when it is one the plugin can check. * * @return WP_Post|null */ private static function posted_post() { $id = isset( $_POST['post_id'] ) ? absint( $_POST['post_id'] ) : 0; // phpcs:ignore WordPress.Security.NonceVerification.Missing -- nonce checked by the callers. $post = $id ? get_post( $id ) : null; if ( ! $post instanceof WP_Post || 'attachment' === $post->post_type || ! current_user_can( 'edit_post', $post->ID ) ) { return null; } return $post; } public static function ajax_preview() { check_ajax_referer( self::NONCE, 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } wp_send_json_success( array( 'html' => self::preview_html( self::posted_settings(), self::posted_post() ) ) ); } public static function ajax_check() { check_ajax_referer( self::NONCE, 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( null, 403 ); } $post = self::posted_post(); if ( ! $post ) { wp_send_json_error( array( 'html' => '

' . esc_html__( 'Choose a page to check.', 'aritoc' ) . '

' ) ); } wp_send_json_success( array( 'html' => self::report_html( $post, AriTOC_Plugin::diagnose( $post, self::posted_settings() ) ) ) ); } /** * Preview: the sample headings, or the headings of a real post. * * @param array $args Settings. * @param WP_Post|null $post Post to preview with, or null for the sample. * @return string */ private static function preview_html( array $args, $post = null ) { $args['context'] = 'preview'; $args['highlight'] = 0; $note = ''; if ( $post ) { $report = AriTOC_Plugin::diagnose( $post, $args ); $items = array(); foreach ( $report['headings'] as $h ) { if ( 'listed' === $h['status'] ) { $items[] = array( 'level' => $h['level'], 'id' => '' !== $h['id'] ? $h['id'] : 'aritoc-preview-' . count( $items ), 'text' => $h['text'], 'page' => $h['page'], ); } } $args = AriTOC_Settings::merge( $args, AriTOC_Post::overrides( $post->ID ) ); if ( ! $items ) { return '

' . esc_html__( 'This page has no headings that would be listed with these settings.', 'aritoc' ) . '

'; } if ( count( $items ) < $report['min'] ) { /* translators: %d: minimum number of headings. */ $note = '

' . esc_html( sprintf( __( 'Below the minimum of %d headings: visitors see no table on this page.', 'aritoc' ), $report['min'] ) ) . '

'; } } else { $items = array_values( array_filter( AriTOC_Renderer::sample_items(), function ( $item ) use ( $args ) { return in_array( (int) $item['level'], array_map( 'intval', $args['levels'] ), true ); } ) ); if ( ! $items ) { return '

' . esc_html__( 'None of the sample headings (H2 and H3) are in the selected levels.', 'aritoc' ) . '

'; } } $html = AriTOC_Renderer::build( $items, $args ); if ( in_array( $args['align'], array( 'left', 'right' ), true ) ) { $demo = '

' . esc_html__( 'Your article text starts here and flows around the table when it floats left or right.', 'aritoc' ) . '

'; $html = '
' . $html . $demo . $demo . '
'; } return $note . $html; } /** * Page check result. * * @param WP_Post $post Post. * @param array $report From AriTOC_Plugin::diagnose(). * @return string */ private static function report_html( WP_Post $post, array $report ) { $icons = array( 'ok' => 'dashicons-yes-alt', 'no' => 'dashicons-dismiss', 'info' => 'dashicons-info-outline', ); $html = '
'; $html .= ''; $html .= '

' . esc_html( $report['verdict'] ) . '

'; $html .= '
'; $html .= ''; if ( ! $report['headings'] ) { return $html . '

' . esc_html__( 'No headings were found in this page’s content.', 'aritoc' ) . '

'; } $paged = count( array_unique( wp_list_pluck( $report['headings'], 'page' ) ) ) > 1; $html .= ''; $html .= ''; if ( $paged ) { $html .= ''; } $html .= ''; foreach ( $report['headings'] as $h ) { $listed = 'listed' === $h['status']; $html .= ''; $html .= ''; $html .= ''; if ( $paged ) { $html .= ''; } $html .= ''; } return $html . '
' . esc_html__( 'Headings on this page', 'aritoc' ) . '
' . esc_html__( 'Level', 'aritoc' ) . '' . esc_html__( 'Heading', 'aritoc' ) . '' . esc_html__( 'Page', 'aritoc' ) . '' . esc_html__( 'Result', 'aritoc' ) . '
H' . (int) $h['level'] . '' . ( '' !== $h['text'] ? esc_html( $h['text'] ) : '' . esc_html__( '(empty)', 'aritoc' ) . '' ) . '' . (int) $h['page'] . '' . self::reason( $h ) . '
'; } /** * Result of one heading in plain words. * * @param array $h Heading row. * @return string Escaped HTML. */ private static function reason( array $h ) { switch ( $h['status'] ) { case 'listed': return '' . esc_html__( 'Listed', 'aritoc' ) . ' #' . esc_html( $h['id'] ) . ''; case 'level': /* translators: %d: heading level. */ $text = sprintf( __( 'H%d is not a selected level', 'aritoc' ), (int) $h['level'] ); break; case 'class': /* translators: %s: CSS class or attribute. */ $text = sprintf( __( 'skipped by %s', 'aritoc' ), $h['detail'] ); break; case 'text': /* translators: %s: skip pattern as typed in the settings. */ $text = sprintf( __( 'matches “%s”', 'aritoc' ), $h['detail'] ); break; default: $text = __( 'has no text', 'aritoc' ); } return '' . esc_html__( 'Skipped', 'aritoc' ) . ' ' . esc_html( $text ); } /* ------------------------------------------------------------------ * * Tools * ------------------------------------------------------------------ */ private static function guard( $action ) { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'aritoc' ), 403 ); } check_admin_referer( $action ); } private static function done( $message ) { wp_safe_redirect( self::url( array( 'aritoc_msg' => $message ) ) . '#tools' ); exit; } public static function export() { self::guard( 'aritoc_export' ); $data = array( 'plugin' => 'aritoc', 'version' => ARITOC_VERSION, 'settings' => AriTOC_Settings::all(), ); nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=aritoc-settings-' . gmdate( 'Y-m-d' ) . '.json' ); echo wp_json_encode( $data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES ); exit; } public static function import() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'aritoc' ), 403 ); } check_admin_referer( 'aritoc_import' ); // $_FILES is never slashed, and unslashing breaks Windows paths (C:\tmp\php1.tmp); is_uploaded_file() validates it. $file = isset( $_FILES['aritoc_file']['tmp_name'] ) ? (string) $_FILES['aritoc_file']['tmp_name'] : ''; // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized, WordPress.Security.ValidatedSanitizedInput.MissingUnslash -- see comment above. if ( '' === $file || ! is_uploaded_file( $file ) || filesize( $file ) > 200000 ) { self::done( 'import_failed' ); } $json = json_decode( (string) file_get_contents( $file ), true ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents -- local upload. if ( ! is_array( $json ) || ! isset( $json['plugin'], $json['settings'] ) || 'aritoc' !== $json['plugin'] || ! is_array( $json['settings'] ) ) { self::done( 'import_failed' ); } update_option( AriTOC_Settings::OPTION, AriTOC_Settings::sanitize( $json['settings'] ) ); self::done( 'imported' ); } public static function migrate() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'aritoc' ), 403 ); } check_admin_referer( 'aritoc_migrate' ); $source = isset( $_POST['aritoc_source'] ) ? sanitize_key( wp_unslash( $_POST['aritoc_source'] ) ) : ''; self::done( AriTOC_Migrate::import( $source ) ? 'migrated' : 'migrate_failed' ); } /** * Back to the defaults. The shortcode takeover and the delete-data * choice are kept: turning the takeover off would silently break * old shortcodes in posts. */ public static function reset() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to do this.', 'aritoc' ), 403 ); } check_admin_referer( 'aritoc_reset' ); $keep = AriTOC_Settings::all(); $fresh = AriTOC_Settings::defaults(); $fresh['takeover_shortcodes'] = (int) ! empty( $keep['takeover_shortcodes'] ); $fresh['delete_data'] = (int) ! empty( $keep['delete_data'] ); update_option( AriTOC_Settings::OPTION, $fresh ); self::done( 'reset' ); } /* ------------------------------------------------------------------ * * Field rendering (from the schema) * ------------------------------------------------------------------ */ private static function name( $key, $multi = false ) { return esc_attr( AriTOC_Settings::OPTION . '[' . $key . ']' . ( $multi ? '[]' : '' ) ); } /** * One table row for a schema field. * * @param string $key Field key. * @param array $field Field definition. * @param array $s Current settings. */ private static function field_row( $key, array $field, array $s ) { $id = 'aritoc-' . $key; $value = isset( $s[ $key ] ) && is_scalar( $s[ $key ] ) ? (string) $s[ $key ] : ''; $labelled = in_array( $field['type'], array( 'select', 'number', 'text', 'textarea' ), true ); $attrs = ''; if ( ! empty( $field['show_if'] ) ) { list( $dep, $wanted ) = $field['show_if']; $attrs .= ' data-show-if="' . esc_attr( $dep . ':' . $wanted ) . '"'; if ( ! isset( $s[ $dep ] ) || (string) $s[ $dep ] !== (string) $wanted ) { $attrs .= ' hidden'; } } echo ''; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. if ( $labelled ) { echo ''; } else { echo '' . esc_html( $field['label'] ) . ''; } echo ''; $help_id = ! empty( $field['help'] ) ? $id . '-help' : ''; $describe = $help_id ? ' aria-describedby="' . esc_attr( $help_id ) . '"' : ''; switch ( $field['type'] ) { case 'post_types': echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. echo ''; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $pt ) { if ( 'attachment' === $pt->name ) { continue; } printf( '', self::name( 'post_types', true ), // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). esc_attr( $pt->name ), checked( in_array( $pt->name, (array) $s['post_types'], true ), true, false ), esc_html( $pt->labels->name ) ); } echo '
'; break; case 'levels': echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. echo ''; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). for ( $i = 1; $i <= 6; $i++ ) { printf( '', self::name( 'levels', true ), // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). (int) $i, checked( in_array( $i, array_map( 'intval', (array) $s['levels'] ), true ), true, false ), (int) $i ); } echo '
'; break; case 'select': echo ''; break; case 'number': printf( '%s', esc_attr( $id ), self::name( $key ), // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). esc_attr( $value ), isset( $field['min'] ) ? (int) $field['min'] : 0, isset( $field['max'] ) ? (int) $field['max'] : 9999, $describe, // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. ! empty( $field['suffix'] ) ? ' ' . esc_html( $field['suffix'] ) : '' ); break; case 'text': printf( '', esc_attr( $id ), ! empty( $field['size'] ) && 'small' === $field['size'] ? 'small-text code' : 'regular-text', self::name( $key ), // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). esc_attr( $value ), esc_attr( isset( $field['placeholder'] ) ? $field['placeholder'] : '' ), $describe // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. ); break; case 'textarea': echo ''; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. break; case 'colors': $colors = array( 'color_bg' => __( 'Background', 'aritoc' ), 'color_text' => __( 'Text', 'aritoc' ), 'color_link' => __( 'Links', 'aritoc' ), 'color_border' => __( 'Border and lines', 'aritoc' ), ); echo '
'; // The color picker wraps the input's parent label, so each name sits beside the input instead. foreach ( $colors as $color => $label ) { printf( '
%2$s
', esc_attr( 'aritoc-' . $color ), esc_html( $label ), self::name( $color ), // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). esc_attr( isset( $s[ $color ] ) ? (string) $s[ $color ] : '' ) ); } echo '
'; break; case 'flags': echo '
'; // phpcs:ignore WordPress.Security.EscapeOutput -- escaped above. foreach ( $field['items'] as $flag => $item ) { $enable_if = ! empty( $item['enable_if'] ) ? $item['enable_if'] : ''; $off = $enable_if && empty( $s[ $enable_if ] ); printf( '', self::name( $flag ), // phpcs:ignore WordPress.Security.EscapeOutput -- escaped in name(). checked( ! empty( $s[ $flag ] ), true, false ), esc_html( $item['label'] ), $enable_if ? ' data-enable-if="' . esc_attr( $enable_if ) . '"' : '', // phpcs:ignore WordPress.Security.EscapeOutput -- escaped inline. $enable_if ? ' aritoc-check--sub' : '', $off ? ' disabled' : '' ); } echo '
'; break; } if ( $help_id ) { echo '

' . esc_html( $field['help'] ) . '

'; } echo ''; } private static function section( $id, array $section, array $s ) { echo '

' . esc_html( $section['title'] ) . '

'; if ( ! empty( $section['intro'] ) ) { echo '

' . esc_html( $section['intro'] ) . '

'; } echo ''; foreach ( $section['fields'] as $key => $field ) { self::field_row( $key, $field, $s ); } echo ''; } /* ------------------------------------------------------------------ * * Page * ------------------------------------------------------------------ */ private static function notice() { $msg = isset( $_GET['aritoc_msg'] ) ? sanitize_key( wp_unslash( $_GET['aritoc_msg'] ) ) : ''; // phpcs:ignore WordPress.Security.NonceVerification.Recommended -- display only. $all = array( 'imported' => array( 'success', __( 'Settings imported.', 'aritoc' ) ), 'import_failed' => array( 'error', __( 'That file is not an AriTOC settings file.', 'aritoc' ) ), 'migrated' => array( 'success', __( 'Settings copied. The other plugin’s shortcodes now show AriTOC tables. Check a few pages, then deactivate the other plugin.', 'aritoc' ) ), 'migrate_failed' => array( 'error', __( 'No settings were found for that plugin.', 'aritoc' ) ), 'reset' => array( 'success', __( 'Settings are back to the defaults.', 'aritoc' ) ), ); if ( isset( $all[ $msg ] ) ) { printf( '

%s

', esc_attr( $all[ $msg ][0] ), esc_html( $all[ $msg ][1] ) ); } } /** * Recent posts to check or preview with. * * @return WP_Post[] */ private static function recent_posts() { $types = get_post_types( array( 'public' => true ) ); unset( $types['attachment'] ); $posts = get_posts( array( 'post_type' => array_values( $types ), 'post_status' => array( 'publish', 'private', 'draft' ), 'numberposts' => 30, 'orderby' => 'modified', 'order' => 'DESC', 'suppress_filters' => false, ) ); return array_values( array_filter( $posts, function ( $post ) { return current_user_can( 'edit_post', $post->ID ); } ) ); } private static function post_options( array $posts ) { foreach ( $posts as $post ) { $type = get_post_type_object( $post->post_type ); $name = '' !== trim( $post->post_title ) ? $post->post_title : __( '(no title)', 'aritoc' ); $status = get_post_status_object( $post->post_status ); printf( '', (int) $post->ID, esc_html( $name . ' — ' . ( $type ? $type->labels->singular_name : $post->post_type ) . ( 'publish' !== $post->post_status && $status ? ' (' . $status->label . ')' : '' ) ) ); } } public static function render_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } $s = AriTOC_Settings::all(); $schema = AriTOC_Settings::schema(); $posts = self::recent_posts(); $tabs = array( 'overview' => __( 'Overview', 'aritoc' ) ); foreach ( $schema as $id => $section ) { $tabs[ $id ] = 'tools' === $id ? __( 'Tools', 'aritoc' ) : $section['title']; } ?>


$section ) : ?>

[aritoc title="In this article" levels="2,3" exclude="FAQ|Related*" numbering="roman" theme="dark" collapsed="yes"]
<?php if ( function_exists( 'aritoc' ) ) { aritoc(); } ?>
no-toc to skip it, or data-aritoc-label="Short label" to show a shorter label.', 'aritoc' ), array( 'code' => array() ) ); ?>