'Default Value', 'required' => true, 'submit_button' => true ] * * Text $args = [ 'width' => '100%' 'pattern' => '^[a-zA-Z0-9_.-]*$' ] * * Color $args = [ 'width' => '20rem' ] * * Textarea $args = [ 'rows' => 6, 'cols' => 50 ] * * Select $args = [ * 'blank' => '-- Select One --', * 'options' => [ * [ 'value' => 'the_value', 'label' => 'Label Name' ], * [ 'value' => 'the_value', 'label' => 'Label Name' ] * ] * ] * OR if value and label are the same * $args = [ * 'options' => [ * 'Value/Label', * 'Value/Label', * ] * ] * * @param string $option_name * @param string $label * @param string $type * @param string $comments // Use 'get_option' for click to copy get_option() * @return string */ function helpdocs_options_tr( $option_name, $label, $type, $comments = null, $args = null ) { // Add the prefix to the option name $option_name = HELPDOCS_GO_PF.$option_name; // Get default if ( get_option( $option_name ) ) { $value = get_option( $option_name ); } elseif ( !is_null( $args ) && isset( $args[ 'default' ]) && $args[ 'default' ] != '' ) { $value = $args[ 'default' ]; } else { $value = ''; } // Make exception for Footer Right if ( $option_name == 'footer_right' && str_starts_with( $value, 'Version ' ) ) { $value = apply_filters( 'update_footer', '' ); } // Mark required? if ( !is_null( $args ) && isset( $args[ 'required' ] ) && $args[ 'required' ] == true ) { $required = ' required'; } else { $required = ''; } // Checkbox if ( $type == 'checkbox' ) { $input = ''; // Checkboxes } elseif ( $type == 'checkboxes' ) { if ( !is_null( $args ) ) { $options = $args[ 'options' ]; $class = isset( $args[ 'class' ] ) ? ' class="'.esc_attr( $args[ 'class' ] ).'"' : ''; } else { return false; } // Sort by label usort( $options, function ( $item1, $item2 ) { return strtolower( $item1[ 'label' ] ) <=> strtolower( $item2[ 'label' ] ); }); // Iter the options $input = ''; foreach ( $options as $option ) { if ( isset( $option[ 'value' ] ) && isset( $option[ 'label' ] ) ) { $option_value = $option[ 'value' ]; $option_label = $option[ 'label' ]; } elseif ( !is_array( $option ) ) { $option_value = $option; $option_label = $option; } if ( !empty( $value ) && array_key_exists( $option_value, $value ) ) { $checked = ' checked="checked"'; } else { $checked = ''; } $input .= '
'; } // Text Field } elseif ( $type == 'text' ) { if ( !is_null( $args ) && isset( $args[ 'width' ] ) ) { $width = $args[ 'width' ]; } else { $width = '43.75rem'; } if ( !is_null( $args ) && isset( $args[ 'pattern' ] ) ) { $pattern = ' pattern="'.$args[ 'pattern' ].'"'; $autocomplete = ' autocomplete="off"'; } else { $pattern = ''; $autocomplete = ''; } $input = ''; // Number Field } elseif ( $type == 'number' ) { if ( !is_null( $args ) && isset( $args[ 'width' ] ) ) { $width = $args[ 'width' ]; } else { $width = '43.75rem'; } if ( !is_null( $args ) && isset( $args[ 'pattern' ] ) ) { $pattern = ' pattern="'.$args[ 'pattern' ].'"'; $autocomplete = ' autocomplete="off"'; } else { $pattern = ''; $autocomplete = ''; } $input = ''; // Text with Color Field } elseif ( $type == 'color' ) { if ( !is_null( $args ) && isset( $args[ 'width' ] ) ) { $width = $args[ 'width' ]; } else { $width = '10rem'; } $input = ''; // Textarea } elseif ( $type == 'textarea' ) { if ( !is_null( $args ) && isset( $args[ 'rows' ] ) && isset( $args[ 'cols' ] ) ) { $rows = $args[ 'rows' ]; $cols = $args[ 'cols' ]; } else { $rows = 6; $cols = 50; } $input = ''; // Select } elseif ( $type == 'select' ) { if ( !is_null( $args ) ) { $options = $args[ 'options' ]; if ( isset( $args[ 'blank' ] ) ) { $blank = ''; } else { $blank = ''; } if ( isset( $args[ 'width' ] ) ) { $width = $args[ 'width' ]; } else { $width = '43.75rem'; } } else { return false; } $input = ''; // Text+ Field } elseif ( $type == 'text+' ) { if ( !is_null( $args ) && isset( $args[ 'width' ] ) ) { $width = $args[ 'width' ]; } else { $width = '43.75rem'; } if ( !is_null( $args ) && isset( $args[ 'pattern' ] ) ) { $pattern = ' pattern="'.$args[ 'pattern' ].'"'; $autocomplete = ' autocomplete="off"'; } else { $pattern = ''; $autocomplete = ''; } if ( !is_array( $value ) ) { $value = [ $value ]; } $input = '
Add New Field +
'; // Add jQuery $js_value = wp_json_encode( $value ); $input .= ''; // Dashicon Picker Field } elseif ( $type == 'dashicon_picker' ) { $input = ' '; // Sorter Field } elseif ( $type == 'sorter' ) { if ( empty( $args ) || ! is_array( $args ) || ! isset( $args[ 'choices' ] ) ) { return false; } if ( ! is_array( $value ) ) { $value = []; } $items_by_value = []; foreach ( $args[ 'choices' ] as $item ) { if ( empty( $item[ 'value' ] ) ) { continue; } $items_by_value[ $item[ 'value' ] ] = [ 'label' => $item[ 'label' ] ?? $item[ 'value' ], 'sublabel' => $item[ 'sublabel' ] ?? '' ]; } if ( ! empty( $value ) ) { $ordered_items = []; foreach ( $value as $slug ) { if ( isset( $items_by_value[ $slug ] ) ) { $ordered_items[ $slug ] = $items_by_value[ $slug ]; unset( $items_by_value[ $slug ] ); } } $items_by_value = $ordered_items + $items_by_value; } $input = ''; // Otherwise return false } else { return false; } // If comments $incl_comments = ''; if ( !is_null( $comments ) ) { if ( $comments == '' ) { $incl_comments = ''; } elseif ( $comments == 'get_option' ) { $incl_comments = 'get_option( '.$option_name.' )'; } elseif ( str_starts_with( $comments, '
' ) ) { $comments = ltrim( $comments, '
' ); $incl_comments = '

'.$comments.'

'; } elseif ( str_starts_with( $comments, ''; } } // Submit button if ( !is_null( $args ) && isset( $args[ 'submit_button' ] ) && $args[ 'submit_button' ] == true ) { $submit_button = get_submit_button( 'Search', 'button button-primary button-large '.$option_name ); } else { $submit_button = ''; } // Build the label HTML with optional checkbox $label_html = '' . esc_html( $label ) . ''; if ( ! empty( $args['sublabel_checkbox'] ) && is_string( $args['sublabel_checkbox'] ) ) { $checkbox_id = 'helpdocs-view-sublabels-toggle-' . esc_attr( str_replace( 'helpdocs-', '', str_replace( '_', '-', $option_name ) ) ); $label_html .= '
'; } // Build the row $row = ' ' . $label_html . ' ' . $input . $submit_button . ' ' . $incl_comments . ' '; // Return the row return $row; } // End helpdocs_options_tr() /** * Allowed html for helpdocs_options_tr() sanitation * * @return array */ function helpdocs_wp_kses_allowed_html() { $allowed_html = [ 'div' => [ 'id' => [], 'class' => [], 'style' => [] ], 'p' => [ 'id' => [], 'class' => [] ], 'pre' => [ 'class' => [] ], 'code' => [ 'class' => [] ], 'span' => [ 'id' => [], 'class' => [], 'style' => [] ], 'a' => [ 'href' => [], 'id' => [], 'class' => [], 'style' => [], 'target' => [], 'rel' => [] ], 'img' => [ 'border' => [], 'id' => [], 'class' => [], 'style' => [], 'src' => [], 'alt' => [] ], 'table' => [ 'class' => [] ], 'tr' => [ 'valign' => [], 'class' => [] ], 'th' => [ 'scope' => [], 'class' => [] ], 'td' => [ 'class' => [] ], 'br' => [], 'form' => [ 'method' => [], 'id' => [], 'action' => [], ], 'label' => [ 'for' => [], ], 'input' => [ 'type' => [], 'id' => [], 'class' => [], 'name' => [], 'value' => [], 'checked' => [], 'required' => [], 'style' => [], 'pattern' => [], 'disabled' => [], 'size' => [], 'autocomplete' => [], 'data-target' => [], 'aria-label' => [] ], 'textarea' => [ 'type' => [], 'id' => [], 'class' => [], 'name' => [], 'rows' => [], 'cols' => [], 'required' => [], 'style' => [], ], 'select' => [ 'id' => [], 'class' => [], 'name' => [], 'required' => [], 'style' => [], 'autocomplete' => [], ], 'option' => [ 'value' => [], 'selected' => [], ], 'button' => [ 'class' => [], 'selected' => [], ], 'script' => [ 'id' => [] ], 'em' => [], 'strong' => [], 'ul' => [ 'class' => [], 'data-option' => [] ], 'li' => [ 'class' => [], 'data-value' => [] ], 'small' => [ 'class' => [] ], ]; return $allowed_html; } // End helpdocs_options_tr_allowed_html() /** * Allow etc in content on docs page only * * @param array $tags * @return array */ function helpdocs_allow_addt_tags( $tags ) { $tags = array_merge( $tags, [ 'script' => [ 'type' => true, 'src' => true, 'async' => true, 'defer' => true, 'crossorigin' => true, 'integrity' => true, ], 'video' => [ 'src' => true, 'controls' => true, 'autoplay' => true, 'loop' => true, 'muted' => true, 'poster' => true, 'width' => true, 'height' => true, ], 'source' => [ 'src' => true, 'type' => true, ], 'iframe' => [ 'src' => true, 'width' => true, 'height' => true, 'frameborder' => true, 'allow' => true, 'allowfullscreen' => true, 'title' => true, 'referrerpolicy' => true, 'webkitallowfullscreen' => true, 'mozallowfullscreen' => true, ], ] ); // Add support for filter $tags = apply_filters( 'helpdocs_allowed_html', $tags ); return $tags; } // End helpdocs_allow_addt_tags() /** * Validate that a date is an actual date * * @param [type] $date * @return bool */ function helpdocs_is_date( $date ) { return (bool)strtotime( $date ); } // End helpdocs_validate_date() /** * Highlighting syntax * * @param string $fl * @param bool $ret * @return void|bool */ function helpdocs_highlight_file2( $filename, $return = false ) { $str = highlight_file( $filename, true ); preg_match_all("/\.*?\<\/span\>/", $str, $mtch); $m = array_unique( $mtch[1] ); $cls = '"; $str2 = str_replace($mtc,$rpl,$str); if ( $return ) return $str2; else echo wp_kses_post( $str2 ); } // End helpdocs_highlight_file2() /** * Add a WP Plugin Info Card * * @param string $slug * @return string */ function helpdocs_plugin_card( $slug ) { // Set the args $args = [ 'slug' => $slug, 'fields' => [ 'last_updated' => true, 'tested' => true, 'active_installs' => true ] ]; // Fetch the plugin info from the wp repository $response = wp_remote_post( 'http://api.wordpress.org/plugins/info/1.0/', [ 'body' => [ 'action' => 'plugin_information', 'request' => serialize( (object)$args ) ] ] ); // If there is no error, continue if ( !is_wp_error( $response ) ) { // Unserialize $returned_object = unserialize( wp_remote_retrieve_body( $response ) ); if ( $returned_object ) { // Last Updated $last_updated = $returned_object->last_updated; $last_updated = helpdocs_time_elapsed_string( $last_updated ); // Compatibility $compatibility = $returned_object->tested; // Add incompatibility class global $wp_version; if ( $compatibility == $wp_version ) { $is_compatible = 'Compatible with your version of WordPress'; } else { $is_compatible = 'Untested with your version of WordPress'; } // Get all the installed plugins $plugins = get_plugins(); // Check if this plugin is installed $is_installed = false; foreach ( $plugins as $key => $plugin ) { if ( $plugin[ 'TextDomain' ] == $slug ) { $is_installed = $key; } } // Check if it is also active $is_active = false; if ( $is_installed && is_plugin_active( $is_installed ) ) { $is_active = true; } // Check if the plugin is already active if ( $is_active ) { $install_link = 'role="link" aria-disabled="true"'; $php_notice = ''; $install_text = 'Active'; // Check if the plugin is installed but not active } elseif ( $is_installed ) { $install_link = 'href="'.admin_url( 'plugins.php' ).'"'; $php_notice = ''; $install_text = 'Go to Activate'; // Check for php requirement } elseif ( phpversion() < $returned_object->requires_php ) { $install_link = 'role="link" aria-disabled="true"'; $php_notice = '
Requires PHP Version '.$returned_object->requires_php.' — You are currently on Version '.phpversion().'
'; $install_text = 'Incompatible'; // If we're good to go, add the link } else { // Get the admin url for the plugin install page if ( is_multisite() ) { $admin_url = network_admin_url( 'plugin-install.php' ); } else { $admin_url = admin_url( 'plugin-install.php' ); } // Vars $install_link = 'href="'.$admin_url.'?s='.esc_attr( $returned_object->name ).'&tab=search&type=term"'; $php_notice = ''; $install_text = 'Get Now'; } // Short Description $pos = strpos( $returned_object->sections[ 'description' ], '.'); $desc = substr( $returned_object->sections[ 'description' ], 0, $pos + 1 ); // Rating $rating = helpdocs_get_five_point_rating( $returned_object->ratings[1], $returned_object->ratings[2], $returned_object->ratings[3], $returned_object->ratings[4], $returned_object->ratings[5] ); $active_installs = $returned_object->active_installs ? number_format( $returned_object->active_installs ) . '+' : '<10'; // Link guts $link_guts = 'href="https://wordpress.org/plugins/'.esc_attr( $slug ).'/" target="_blank" aria-label="More information about '.$returned_object->name.' '.$returned_object->version.'" data-title="'.$returned_object->name.' '.$returned_object->version.'"'; ?>
star rating based on num_ratings ); ?> ratings
Last Updated:
Active Installs
'; $h = ''; $e = ''; $stars = $e.$e.$e.$e.$e; if ( $r > 4.74 ) { $stars = $f.$f.$f.$f.$f; } elseif ( $r > 4.24 && $r < 4.75 ) { $stars = $f.$f.$f.$f.$h; } elseif ( $r > 3.74 && $r < 4.25 ) { $stars = $f.$f.$f.$f.$e; } elseif ( $r > 3.24 && $r < 3.75 ) { $stars = $f.$f.$f.$h.$e; } elseif ( $r > 2.74 && $r < 3.25 ) { $stars = $f.$f.$f.$e.$e; } elseif ( $r > 2.24 && $r < 2.75 ) { $stars = $f.$f.$h.$e.$e; } elseif ( $r > 1.74 && $r < 2.25 ) { $stars = $f.$f.$e.$e.$e; } elseif ( $r > 1.24 && $r < 1.75 ) { $stars = $f.$h.$e.$e.$e; } elseif ( $r > 0.74 && $r < 1.25 ) { $stars = $f.$e.$e.$e.$e; } elseif ( $r > 0.24 && $r < 0.75 ) { $stars = $h.$e.$e.$e.$e; } else { $stars = $stars; } return '
'.$stars.'
'; } // End helpdocs_convert_to_stars() /** * Get 5-point rating from 5 values * * @param int|float $r1 * @param int|float $r2 * @param int|float $r3 * @param int|float $r4 * @param int|float $r5 * @return float */ function helpdocs_get_five_point_rating ( $r1, $r2, $r3, $r4, $r5 ) { // Calculate them on a 5-point rating system $r5b = round( $r5 * 5, 0 ); $r4b = round( $r4 * 4, 0 ); $r3b = round( $r3 * 3, 0 ); $r2b = round( $r2 * 2, 0 ); $r1b = $r1; $total = round( $r1 + $r2 + $r3 + $r4 + $r5, 0 ); if ( $total == 0 ) { $r = 0; } else { $r = round( ( $r1b + $r2b + $r3b + $r4b + $r5b ) / $total, 2 ); } return $r; } // End helpdocs_get_five_point_rating() /** * Create a json file for the settings if updated * * @return void */ function helpdocs_create_json_from_settings() { // Get all of the settings $keys = (new HELPDOCS_GLOBAL_OPTIONS)->settings_general; $keys = array_merge( $keys, [ 'colorize_separators', 'color_admin_menu_sep' ] ); // Store the values here $values = []; // Iter the keys foreach ( $keys as $key ) { // Get the option if ( $key != 'copy_from' && $value = get_option( HELPDOCS_GO_PF.$key ) ) { // Add the value $values[ $key ] = $value; } } // Add the date $values[ 'last_updated' ] = helpdocs_convert_timezone(); // Convert to json $json = wp_json_encode( $values ); // Initialize the filesystem global $wp_filesystem; if ( empty( $wp_filesystem ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; WP_Filesystem(); } if ( !$wp_filesystem ) { error_log( esc_html__( 'Admin Help Docs: Error creating JSON from settings. Unable to initialize the filesystem.', 'admin-help-docs' ) ); return; } // Directories $upload_dir = wp_upload_dir(); $folder = trailingslashit( $upload_dir[ 'basedir' ] . '/' . HELPDOCS_TEXTDOMAIN ); // Check if the directory exists, and if not, create it if ( !$wp_filesystem->is_dir( $folder ) ) { $wp_filesystem->mkdir( $folder ); } // Write to file $file = $folder.'settings.json'; // Write to file if ( !$wp_filesystem->put_contents( $file, $json ) ) { error_log( esc_html__( 'Admin Help Docs: Error creating JSON from settings. Failed to write settings to file.', 'admin-help-docs' ) ); } } // End helpdocs_create_json_from_settings() /** * Import settings from a json link * * @return void */ function helpdocs_import_settings_from_json( $file ) { // Fetch the file $request = wp_remote_get( $file.'?time=' . time() ); // Is there an error? if ( is_wp_error( $request ) || $request[ 'response' ][ 'code' ] == 404 ) { return 'invalid_file'; } // Open the file $json = wp_remote_retrieve_body( $request ); // Check if it's in json format if ( !helpdocs_is_json( $json ) ) { return 'invalid_file'; } // Decode it $settings = json_decode( $json ); // Get our setting meta keys for validation // Get all of the settings $keys = (new HELPDOCS_GLOBAL_OPTIONS)->settings_general; $keys = array_merge( $keys, [ 'colorize_separators', 'color_admin_menu_sep' ] ); // Bools $bools = [ 'admin_bar', 'hide_version', 'disable_user_prefs', 'colorize_separators' ]; // Numbers $nums = [ 'menu_position' ]; // URLs $urls = [ 'logo' ]; // Iter the items foreach ( $settings as $setting => $value ) { // Don't import multisite setting if it's not a multisite if ( !is_multisite() && $setting == 'multisite_sfx' ) { delete_option( HELPDOCS_GO_PF.$setting ); continue; } // validate the key if ( in_array( $setting, $keys ) && $setting !== 'edit_roles' ) { // Sanitize if ( in_array( $setting, $bools ) ) { $value = filter_var( $value, FILTER_VALIDATE_BOOLEAN ); } elseif ( in_array( $setting, $nums ) ) { $value = absint( $value ); } elseif ( in_array( $setting, $urls ) ) { $value = esc_url_raw( $value ); } else { $value = sanitize_text_field( $value ); } // Update update_option( HELPDOCS_GO_PF.$setting, $value ); } } // Return true because it worked return true; } // End helpdocs_import_settings_from_json() /** * Check if a string is json format * * @param string $string * @return bool */ function helpdocs_is_json( $string ) { json_decode( $string ); return json_last_error() === JSON_ERROR_NONE; } // End helpdocs_is_json() /** * Get the plugin title * * @return string */ function helpdocs_title() { if ( get_option( HELPDOCS_GO_PF.'page_title' ) && get_option( HELPDOCS_GO_PF.'page_title' ) != '' ) { return get_option( HELPDOCS_GO_PF.'page_title' ); } else { return HELPDOCS_NAME; } } // End helpdocs_title() /** * Get the plugin menu title * * @return string */ function helpdocs_menu_title() { if ( get_option( HELPDOCS_GO_PF.'menu_title' ) && get_option( HELPDOCS_GO_PF.'menu_title' ) != '' ) { return get_option( HELPDOCS_GO_PF.'menu_title' ); } else { return 'Help Docs'; } } // End helpdocs_menu_title() /** * Get the logo * * @return string */ function helpdocs_logo() { if ( get_option( HELPDOCS_GO_PF.'logo' ) && get_option( HELPDOCS_GO_PF.'logo' ) != '' ) { return get_option( HELPDOCS_GO_PF.'logo' ); } else { return HELPDOCS_PLUGIN_IMG_PATH.'logo.png'; } } // End helpdocs_logo() /** * THE END */