' . 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 = '' . esc_html( $report['verdict'] ) . '
'; $html .= '' . esc_html__( 'View page', 'aritoc' ) . ''; $edit = get_edit_post_link( $post->ID ); if ( $edit ) { $html .= ' · ' . esc_html__( 'Edit page', 'aritoc' ) . ''; } $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 .= '| ' . esc_html__( 'Level', 'aritoc' ) . ' | ' . esc_html__( 'Heading', 'aritoc' ) . ' | '; if ( $paged ) { $html .= '' . esc_html__( 'Page', 'aritoc' ) . ' | '; } $html .= '' . esc_html__( 'Result', 'aritoc' ) . ' |
|---|---|---|---|
| H' . (int) $h['level'] . ' | '; $html .= '' . ( '' !== $h['text'] ? esc_html( $h['text'] ) : '' . esc_html__( '(empty)', 'aritoc' ) . '' ) . ' | '; if ( $paged ) { $html .= '' . (int) $h['page'] . ' | '; } $html .= '' . self::reason( $h ) . ' |
#' . 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 '' . esc_html( $field['help'] ) . '
'; } echo '' . esc_html( $section['intro'] ) . '
'; } echo '%s
[aritoc title="In this article" levels="2,3" exclude="FAQ|Related*" numbering="roman" theme="dark" collapsed="yes"]<?php if ( function_exists( 'aritoc' ) ) { aritoc(); } ?>data-aritoc-label="Short label" to show a shorter label.', 'aritoc' ), array( 'code' => array() ) ); ?>