term_id, '_docsstation_hide_from_global', true ); $isolated_sidebar = get_term_meta( $term->term_id, '_docsstation_isolated_sidebar', true ); ?>

' . esc_html__( 'Hidden from Main Docs', 'docsstation' ) . ''; } if ( '1' === get_term_meta( $term_id, '_docsstation_isolated_sidebar', true ) ) { $badges[] = '' . esc_html__( 'Isolated Sidebar', 'docsstation' ) . ''; } if ( empty( $badges ) ) { return '' . esc_html__( 'Global (Default)', 'docsstation' ) . ''; } return implode( ' ', $badges ); } public static function register_category_menu_item_box() { if ( ! current_user_can( 'edit_theme_options' ) ) { return; } $taxonomy = get_taxonomy( 'docsstation_category' ); if ( $taxonomy && class_exists( 'Walker_Nav_Menu_Checklist' ) ) { add_meta_box( 'add-docsstation-category-menu-item', __( 'DocsStation Categories', 'docsstation' ), array( __CLASS__, 'category_menu_item_box' ), 'nav-menus', 'side', 'default', $taxonomy ); } } public static function category_menu_item_box( $data_object, $box ) { global $_nav_menu_placeholder, $nav_menu_selected_id; $terms = get_terms( array( 'taxonomy' => 'docsstation_category', 'hide_empty' => false, 'hierarchical' => true, 'orderby' => 'name', 'order' => 'ASC', 'number' => 500, ) ); if ( is_wp_error( $terms ) || ! $terms ) { echo '

' . esc_html__( 'No DocsStation categories found.', 'docsstation' ) . '

'; return; } $walker = new Walker_Nav_Menu_Checklist( array( 'parent' => 'parent', 'id' => 'term_id', ) ); ?>

id="docsstation_category-tab" class="select-all" /> class="button submit-add-to-menu" value="" name="add-taxonomy-menu-item" id="submit-taxonomy-docsstation_category" />

'docsstation_category', 'hide_empty' => false, 'orderby' => 'name', 'order' => 'ASC' ) ); if ( is_wp_error( $categories ) || ! is_array( $categories ) ) { $categories = array(); } $documents = get_posts( array( 'post_type' => 'docsstation_doc', 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'posts_per_page' => -1, 'no_found_rows' => true, 'update_post_meta_cache' => false, 'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC' ), 'order' => 'ASC', ) ); $grouped = array(); foreach ( $documents as $document ) { $terms = get_the_terms( $document->ID, 'docsstation_category' ); $key = ! empty( $terms ) && ! is_wp_error( $terms ) ? $terms[0]->term_id : 0; $grouped[ $key ][] = $document; } ?>

term_id ] ) ? $grouped[ $category->term_id ] : array(); $is_hidden = '1' === get_term_meta( $category->term_id, '_docsstation_hide_from_global', true ); $is_isolated = '1' === get_term_meta( $category->term_id, '_docsstation_isolated_sidebar', true ); ?>
name ); ?>
    ID ); ?>
$document_id ) { if ( 'docsstation_doc' === get_post_type( $document_id ) && current_user_can( 'edit_post', $document_id ) ) { update_post_meta( $document_id, '_docsstation_order_initialized', '1' ); wp_update_post( array( 'ID' => $document_id, 'menu_order' => $position ) ); } } wp_safe_redirect( add_query_arg( 'post_type', 'docsstation_doc', admin_url( 'edit.php?page=docsstation-arrange&docsstation_order_saved=1' ) ) ); exit; } public static function import_export_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>


0 ) : ?>

'docsstation', 'version' => DOCSSTATION_VERSION, 'generated' => current_time( 'mysql' ), 'taxonomies' => array( 'docsstation_category' => self::export_terms( 'docsstation_category' ), ), 'documents' => array(), ); $documents = get_posts( array( 'post_type' => 'docsstation_doc', 'post_status' => array( 'publish', 'draft', 'private', 'future' ), 'posts_per_page' => -1 ) ); foreach ( $documents as $document ) { $export['documents'][] = array( 'slug' => $document->post_name, 'title' => $document->post_title, 'content' => $document->post_content, 'excerpt' => $document->post_excerpt, 'status' => $document->post_status, 'menu_order' => $document->menu_order, 'categories' => self::term_names( $document->ID, 'docsstation_category' ), ); } nocache_headers(); header( 'Content-Type: application/json; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=docsstation-export-' . gmdate( 'Y-m-d' ) . '.json' ); echo wp_json_encode( $export, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES ); exit; } public static function export_xml() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to export DocsStation content.', 'docsstation' ) ); } check_admin_referer( 'docsstation_export_xml' ); $documents = get_posts( array( 'post_type' => 'docsstation_doc', 'post_status' => array( 'publish', 'draft', 'pending', 'private', 'future' ), 'posts_per_page' => -1, 'orderby' => array( 'menu_order' => 'ASC', 'title' => 'ASC' ), 'order' => 'ASC', ) ); $xml = '' . "\n"; $xml .= '' . "\n"; $xml .= '' . self::xml_text( home_url( '/' ) ) . '' . "\n"; $xml .= '' . self::xml_text( __( 'DocsStation WordPress XML export', 'docsstation' ) ) . '' . "\n"; $xml .= '' . self::xml_text( gmdate( 'D, d M Y H:i:s +0000' ) ) . '' . "\n"; $xml .= '' . self::xml_text( get_locale() ) . '' . "\n"; $xml .= '1.2' . "\n"; $terms = get_terms( array( 'taxonomy' => 'docsstation_category', 'hide_empty' => false, ) ); if ( ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $parent = $term->parent ? get_term( $term->parent, 'docsstation_category' ) : null; $parent_slug = $parent && ! is_wp_error( $parent ) ? $parent->slug : ''; $xml .= '' . "\n"; $xml .= '' . (int) $term->term_id . '' . "\n"; $xml .= '' . self::xml_text( $term->slug ) . '' . "\n"; $xml .= '' . self::xml_text( $term->name ) . '' . "\n"; $xml .= '' . self::xml_text( $parent_slug ) . '' . "\n"; $xml .= '' . "\n"; } } foreach ( $documents as $document ) { $post_date_gmt = get_gmt_from_date( $document->post_date ); $post_date_gmt = $post_date_gmt ? $post_date_gmt : '1970-01-01 00:00:00'; $author = get_userdata( $document->post_author ); $xml .= '' . "\n"; $xml .= '' . self::xml_text( $document->post_title ) . '' . "\n"; $xml .= '' . self::xml_text( mysql2date( 'D, d M Y H:i:s +0000', $document->post_date_gmt, false ) ) . '' . "\n"; $xml .= '' . self::xml_cdata( $author ? $author->user_login : '' ) . '' . "\n"; $xml .= '' . self::xml_text( get_permalink( $document->ID ) ) . '' . "\n"; $xml .= '' . "\n"; $xml .= '' . self::xml_cdata( $document->post_content ) . '' . "\n"; $xml .= '' . self::xml_cdata( $document->post_excerpt ) . '' . "\n"; $xml .= '' . (int) $document->ID . '' . "\n"; $xml .= '' . self::xml_text( $document->post_date ) . '' . "\n"; $xml .= '' . self::xml_text( $post_date_gmt ) . '' . "\n"; $xml .= '' . self::xml_text( $document->post_name ) . '' . "\n"; $xml .= '' . self::xml_text( $document->post_status ) . '' . "\n"; $xml .= '0' . "\n"; $xml .= '' . (int) $document->menu_order . '' . "\n"; $xml .= 'docs' . "\n"; $xml .= '' . "\n"; $terms = get_the_terms( $document->ID, 'docsstation_category' ); if ( $terms && ! is_wp_error( $terms ) ) { foreach ( $terms as $term ) { $xml .= '' . self::xml_text( $term->name ) . '' . "\n"; } } $xml .= '' . "\n"; } $xml .= '' . "\n"; $xml .= '' . "\n"; nocache_headers(); header( 'Content-Type: application/xml; charset=utf-8' ); header( 'Content-Disposition: attachment; filename=docsstation-export-' . gmdate( 'Y-m-d' ) . '.xml' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- $xml is a generated valid XML export document. echo $xml; exit; } private static function xml_text( $value ) { return htmlspecialchars( wp_check_invalid_utf8( (string) $value, true ), ENT_XML1 | ENT_QUOTES, 'UTF-8' ); } private static function xml_attribute( $value ) { return self::xml_text( $value ); } private static function xml_cdata( $value ) { $value = wp_check_invalid_utf8( (string) $value, true ); return '', ']]]]>', $value ) . ']]>'; } private static function export_terms( $taxonomy ) { $terms = get_terms( array( 'taxonomy' => $taxonomy, 'hide_empty' => false ) ); $output = array(); if ( is_wp_error( $terms ) ) { return $output; } foreach ( $terms as $term ) { $output[] = array( 'name' => $term->name, 'slug' => $term->slug, 'parent' => $term->parent, 'hide_from_global' => get_term_meta( $term->term_id, '_docsstation_hide_from_global', true ), 'isolated_sidebar' => get_term_meta( $term->term_id, '_docsstation_isolated_sidebar', true ), ); } return $output; } private static function term_names( $post_id, $taxonomy ) { $terms = get_the_terms( $post_id, $taxonomy ); return empty( $terms ) || is_wp_error( $terms ) ? array() : wp_list_pluck( $terms, 'name' ); } private static function redirect_import_notice( $redirect, $args ) { $args['docsstation_notice_nonce'] = wp_create_nonce( 'docsstation_import_notice' ); wp_safe_redirect( add_query_arg( $args, $redirect ) ); exit; } public static function import() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You are not allowed to import DocsStation content.', 'docsstation' ) ); } check_admin_referer( 'docsstation_import' ); $redirect = admin_url( 'edit.php?post_type=docsstation_doc&page=docsstation-import-export' ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized -- Validated via is_uploaded_file and json_decode. $file = isset( $_FILES['docsstation_json'] ) ? $_FILES['docsstation_json'] : array(); if ( empty( $file['tmp_name'] ) || ! isset( $file['error'], $file['size'], $file['name'] ) || UPLOAD_ERR_OK !== (int) $file['error'] || ! is_uploaded_file( $file['tmp_name'] ) ) { self::redirect_import_notice( $redirect, array( 'docsstation_error' => rawurlencode( __( 'Please upload a valid JSON file.', 'docsstation' ) ) ) ); } if ( (int) $file['size'] > 10 * MB_IN_BYTES || 'json' !== strtolower( pathinfo( sanitize_file_name( $file['name'] ), PATHINFO_EXTENSION ) ) ) { self::redirect_import_notice( $redirect, array( 'docsstation_error' => rawurlencode( __( 'The JSON file must be smaller than 10 MB and use the .json extension.', 'docsstation' ) ) ) ); } // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized $json_content = file_get_contents( $file['tmp_name'] ); $data = is_string( $json_content ) && '' !== $json_content ? json_decode( $json_content, true ) : null; $taxonomies = is_array( $data ) && isset( $data['taxonomies'] ) && is_array( $data['taxonomies'] ) ? $data['taxonomies'] : array(); $categories = isset( $taxonomies['docsstation_category'] ) && is_array( $taxonomies['docsstation_category'] ) ? $taxonomies['docsstation_category'] : array(); if ( ! is_array( $data ) || 'docsstation' !== ( isset( $data['format'] ) ? $data['format'] : '' ) || ! is_array( $data['documents'] ?? null ) || count( $data['documents'] ) > 500 || count( $categories ) > 1000 ) { self::redirect_import_notice( $redirect, array( 'docsstation_error' => rawurlencode( __( 'The JSON file is not a valid DocsStation export.', 'docsstation' ) ) ) ); } $term_maps = array(); foreach ( array( 'docsstation_category' ) as $taxonomy ) { $term_list = $categories; $term_maps[ $taxonomy ] = self::import_terms( $taxonomy, $term_list ); } $imported = 0; foreach ( $data['documents'] as $document ) { if ( ! is_array( $document ) || ! isset( $document['title'] ) || ! is_scalar( $document['title'] ) ) { continue; } $title = sanitize_text_field( (string) $document['title'] ); if ( '' === $title ) { continue; } $slug = isset( $document['slug'] ) && is_scalar( $document['slug'] ) ? sanitize_title( (string) $document['slug'] ) : ''; $existing = $slug ? get_page_by_path( $slug, OBJECT, 'docsstation_doc' ) : null; if ( $existing ) { continue; } $post_id = wp_insert_post( array( 'post_type' => 'docsstation_doc', 'post_title' => $title, 'post_name' => $slug ? $slug : sanitize_title( $title ), 'post_content' => wp_kses_post( isset( $document['content'] ) && is_scalar( $document['content'] ) ? (string) $document['content'] : '' ), 'post_excerpt' => sanitize_textarea_field( isset( $document['excerpt'] ) && is_scalar( $document['excerpt'] ) ? (string) $document['excerpt'] : '' ), 'post_status' => isset( $document['status'] ) && is_string( $document['status'] ) && in_array( $document['status'], array( 'publish', 'draft', 'private', 'future' ), true ) ? $document['status'] : 'draft', 'menu_order' => absint( isset( $document['menu_order'] ) ? $document['menu_order'] : 0 ), ), true ); if ( is_wp_error( $post_id ) ) { continue; } foreach ( array( 'docsstation_category' ) as $taxonomy ) { $term_key = 'categories'; $names = isset( $document[ $term_key ] ) && is_array( $document[ $term_key ] ) ? $document[ $term_key ] : array(); $term_ids = array(); foreach ( (array) $names as $name ) { if ( ! is_scalar( $name ) ) { continue; } $name = sanitize_text_field( (string) $name ); if ( isset( $term_maps[ $taxonomy ][ $name ] ) ) { $term_ids[] = $term_maps[ $taxonomy ][ $name ]; } } wp_set_post_terms( $post_id, $term_ids, $taxonomy ); } $imported++; } self::redirect_import_notice( $redirect, array( 'docsstation_imported' => $imported ) ); } private static function import_terms( $taxonomy, $terms ) { $map = array(); foreach ( (array) $terms as $term ) { if ( ! is_array( $term ) || ! isset( $term['name'] ) || ! is_scalar( $term['name'] ) ) { continue; } $name = sanitize_text_field( $term['name'] ); $slug = sanitize_title( isset( $term['slug'] ) && is_scalar( $term['slug'] ) ? (string) $term['slug'] : $name ); if ( '' === $name ) { continue; } $existing = get_term_by( 'slug', $slug, $taxonomy ); if ( $existing ) { $map[ $name ] = $existing->term_id; continue; } $created = wp_insert_term( $name, $taxonomy, array( 'slug' => $slug ) ); if ( ! is_wp_error( $created ) ) { $map[ $name ] = $created['term_id']; if ( ! empty( $term['hide_from_global'] ) ) { update_term_meta( $created['term_id'], '_docsstation_hide_from_global', '1' ); } if ( ! empty( $term['isolated_sidebar'] ) ) { update_term_meta( $created['term_id'], '_docsstation_isolated_sidebar', '1' ); } } } return $map; } public static function register_settings() { register_setting( 'docsstation_settings', 'docsstation_settings', array( 'type' => 'array', 'sanitize_callback' => array( __CLASS__, 'sanitize_settings' ), 'default' => self::defaults(), ) ); add_settings_section( 'docsstation_layout', __( 'Documentation layout', 'docsstation' ), '__return_false', 'docsstation-settings' ); add_settings_field( 'show_toc', __( 'Table of contents', 'docsstation' ), array( __CLASS__, 'checkbox_field' ), 'docsstation-settings', 'docsstation_layout', array( 'label_for' => 'show_toc', 'text' => __( 'Show the automatic table of contents on documents.', 'docsstation' ), ) ); add_settings_field( 'show_audio_player', __( 'Listen to document', 'docsstation' ), array( __CLASS__, 'checkbox_field' ), 'docsstation-settings', 'docsstation_layout', array( 'label_for' => 'show_audio_player', 'text' => __( 'Enable browser-native text-to-speech audio player on documents.', 'docsstation' ), ) ); } public static function defaults() { return array( 'show_toc' => 1, 'show_audio_player' => 1, ); } public static function sanitize_settings( $settings ) { $settings = is_array( $settings ) ? $settings : array(); return array( 'show_toc' => empty( $settings['show_toc'] ) ? 0 : 1, 'show_audio_player' => empty( $settings['show_audio_player'] ) ? 0 : 1, ); } public static function checkbox_field( $args ) { $options = wp_parse_args( get_option( 'docsstation_settings', self::defaults() ), self::defaults() ); printf( '', esc_attr( $args['label_for'] ), checked( 1, (int) $options[ $args['label_for'] ], false ), esc_html( $args['text'] ) ); } public static function settings_page() { if ( ! current_user_can( 'manage_options' ) ) { return; } ?>