ich of the editors to display as the current editor for a * user. The 'html' setting is for the "Text" editor tab. * * @since 2.5.0 * * @return string Either 'tinymce', 'html', or 'test' */ function wp_default_editor() { $r = user_can_richedit() ? 'tinymce' : 'html'; // Defaults. if ( wp_get_current_user() ) { // Look for cookie. $ed = get_user_setting( 'editor', 'tinymce' ); $r = ( in_array( $ed, array( 'tinymce', 'html', 'test' ), true ) ) ? $ed : $r; } /** * Filters which editor should be displayed by default. * * @since 2.5.0 * * @param string $r Which editor should be displayed by default. Either 'tinymce', 'html', or 'test'. */ return apply_filters( 'wp_default_editor', $r ); } /** * Renders an editor. * * Using this function is the proper way to output all needed components for both TinyMCE and Quicktags. * _WP_Editors should not be used directly. See https://core.trac.wordpress.org/ticket/17144. * * NOTE: Once initialized the TinyMCE editor cannot be safely moved in the DOM. For that reason * running wp_editor() inside of a meta box is not a good idea unless only Quicktags is used. * On the post edit screen several actions can be used to include additional editors * containing TinyMCE: 'edit_page_form', 'edit_form_advanced' and 'dbx_post_sidebar'. * See https://core.trac.wordpress.org/ticket/19173 for more information. * * @see _WP_Editors::editor() * @see _WP_Editors::parse_settings() * @since 3.3.0 * * @param string $content Initial content for the editor. * @param string $editor_id HTML ID attribute value for the textarea and TinyMCE. * Should not contain square brackets. * @param array $settings See _WP_Editors::parse_settings() for description. */ function wp_editor( $content, $editor_id, $settings = array() ) { if ( ! class_exists( '_WP_Editors', false ) ) { require ABSPATH . WPINC . '/class-wp-editor.php'; } _WP_Editors::editor( $content, $editor_id, $settings ); } /** * Outputs the editor scripts, stylesheets, and default settings. * * The editor can be initialized when needed after page load. * See wp.editor.initialize() in wp-admin/js/editor.js for initialization options. * * @uses _WP_Editors * @since 4.8.0 */ function wp_enqueue_editor() { if ( ! class_exists( '_WP_Editors', false ) ) { require ABSPATH . WPINC . '/class-wp-editor.php'; } _WP_Editors::enqueue_default_editor(); } /** * Enqueues assets needed by the code editor for the given settings. * * @since 4.9.0 * * @see wp_enqueue_editor() * @see wp_get_code_editor_settings(); * @see _WP_Editors::parse_settings() * * @param array $args { * Args. * * @type string $type The MIME type of the file to be edited. * @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param. * @type WP_Theme $theme Theme being edited when on the theme file editor. * @type string $plugin Plugin being edited when on the plugin file editor. * @type array $codemirror Additional CodeMirror setting overrides. * @type array $csslint CSSLint rule overrides. * @type array $jshint JSHint rule overrides. * @type array $htmlhint HTMLHint rule overrides. * } * @return array|false Settings for the enqueued code editor, or false if the editor was not enqueued. */ function wp_enqueue_code_editor( $args ) { if ( is_user_logged_in() && 'false' === wp_get_current_user()->syntax_highlighting ) { return false; } $settings = wp_get_code_editor_settings( $args ); if ( empty( $settings ) || empty( $settings['codemirror'] ) ) { return false; } wp_enqueue_script( 'code-editor' ); wp_enqueue_style( 'code-editor' ); if ( isset( $settings['codemirror']['mode'] ) ) { $mode = $settings['codemirror']['mode']; if ( is_string( $mode ) ) { $mode = array( 'name' => $mode, ); } if ( ! empty( $settings['codemirror']['lint'] ) ) { switch ( $mode['name'] ) { case 'css': case 'text/css': case 'text/x-scss': case 'text/x-less': wp_enqueue_script( 'csslint' ); break; case 'htmlmixed': case 'text/html': case 'php': case 'application/x-httpd-php': case 'text/x-php': wp_enqueue_script( 'htmlhint' ); wp_enqueue_script( 'csslint' ); wp_enqueue_script( 'jshint' ); if ( ! current_user_can( 'unfiltered_html' ) ) { wp_enqueue_script( 'htmlhint-kses' ); } break; case 'javascript': case 'application/ecmascript': case 'application/json': case 'application/javascript': case 'application/ld+json': case 'text/typescript': case 'application/typescript': wp_enqueue_script( 'jshint' ); wp_enqueue_script( 'jsonlint' ); break; } } } wp_add_inline_script( 'code-editor', sprintf( 'jQuery.extend( wp.codeEditor.defaultSettings, %s );', wp_json_encode( $settings ) ) ); /** * Fires when scripts and styles are enqueued for the code editor. * * @since 4.9.0 * * @param array $settings Settings for the enqueued code editor. */ do_action( 'wp_enqueue_code_editor', $settings ); return $settings; } /** * Generates and returns code editor settings. * * @since 5.0.0 * * @see wp_enqueue_code_editor() * * @param array $args { * Args. * * @type string $type The MIME type of the file to be edited. * @type string $file Filename to be edited. Extension is used to sniff the type. Can be supplied as alternative to `$type` param. * @type WP_Theme $theme Theme being edited when on the theme file editor. * @type string $plugin Plugin being edited when on the plugin file editor. * @type array $codemirror Additional CodeMirror setting overrides. * @type array $csslint CSSLint rule overrides. * @type array $jshint JSHint rule overrides. * @type array $htmlhint HTMLHint rule overrides. * } * @return array|false Settings for the code editor. */ function wp_get_code_editor_settings( $args ) { $settings = array( 'codemirror' => array( 'indentUnit' => 4, 'indentWithTabs' => true, 'inputStyle' => 'contenteditable', 'lineNumbers' => true, 'lineWrapping' => true, 'styleActiveLine' => true, 'continueComments' => true, 'extraKeys' => array( 'Ctrl-Space' => 'autocomplete', 'Ctrl-/' => 'toggleComment', 'Cmd-/' => 'toggleComment', 'Alt-F' => 'findPersistent', 'Ctrl-F' => 'findPersistent', 'Cmd-F' => 'findPersistent', ), 'direction' => 'ltr', // Code is shown in LTR even in RTL languages. 'gutters' => array(), ), 'csslint' => array( 'errors' => true, // Parsing errors. 'box-model' => true, 'display-property-grouping' => true, 'duplicate-properties' => true, 'known-properties' => true, 'outline-none' => true, ), 'jshint' => array( // The following are copied from . 'boss' => true, 'curly' => true, 'eqeqeq' => true, 'eqnull' => true, 'es3' => true, 'expr' => true, 'immed' => true, 'noarg' => true, 'nonbsp' => true, 'onevar' => true, 'quotmark' => 'single', 'trailing' => true, 'undef' => true, 'unused' => true, 'browser' => true, 'globals' => array( '_' => false, 'Backbone' => false, 'jQuery' => false, 'JSON' => false, 'wp' => false, ), ), 'htmlhint' => array( 'tagname-lowercase' => true, 'attr-lowercase' => true, 'attr-value-double-quotes' => false, 'doctype-first' => false, 'tag-pair' => true, 'spec-char-escape' => true, 'id-unique' => true, 'src-not-empty' => true, 'attr-no-duplication' => true, 'alt-require' => true, 'space-tab-mixed-disabled' => 'tab', 'attr-unsafe-chars' => true, ), ); $type = ''; if ( isset( $args['type'] ) ) { $type = $args['type']; // Remap MIME types to ones that CodeMirror modes will recognize. if ( 'application/x-patch' === $type || 'text/x-patch' === $type ) { $type = 'text/x-diff'; } } elseif ( isset( $args['file'] ) && str_contains( basename( $args['file'] ), '.' ) ) { $extension = strtolower( pathinfo( $args['file'], PATHINFO_EXTENSION ) ); foreach ( wp_get_mime_types() as $exts => $mime ) { if ( preg_match( '!^(' . $exts . ')$!i', $extension ) ) { $type = $mime; break; } } // Supply any types that are not matched by wp_get_mime_types(). if ( empty( $type ) ) { switch ( $extension ) { case 'conf': $type = 'text/nginx'; break; case 'css': $type = 'text/css'; break; case 'diff': case 'patch': $type = 'text/x-diff'; break; case 'html': case 'htm': $type = 'text/html'; break; case 'http': $type = 'message/http'; break; case 'js': $type = 'text/javascript'; break; case 'json': $type = 'application/json'; break; case 'jsx': $type = 'text/jsx'; break; case 'less': $type = 'text/x-less'; break; case 'md': $type = 'text/x-gfm'; break; case 'php': case 'phtml': case 'php3': case 'php4': case 'php5': case 'php7': case 'phps': $type = 'application/x-httpd-php'; break; case 'scss': $type = 'text/x-scss'; break; case 'sass': $type = 'text/x-sass'; break; case 'sh': case 'bash': $type = 'text/x-sh'; break; case 'sql': $type = 'text/x-sql'; break; case 'svg': $type = 'application/svg+xml'; break; case 'xml': $type = 'text/xml'; break; case 'yml': case 'yaml': $type = 'text/x-yaml'; break; case 'txt': default: $type = 'text/plain'; break; } } } if ( in_array( $type, array( 'text/css', 'text/x-scss', 'text/x-less', 'text/x-sass' ), true ) ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => $type, 'lint' => false, 'autoCloseBrackets' => true, 'matchBrackets' => true, ) ); } elseif ( 'text/x-diff' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'diff', ) ); } elseif ( 'text/html' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'htmlmixed', 'lint' => true, 'autoCloseBrackets' => true, 'autoCloseTags' => true, 'matchTags' => array( 'bothTags' => true, ), ) ); if ( ! current_user_can( 'unfiltered_html' ) ) { $settings['htmlhint']['kses'] = wp_kses_allowed_html( 'post' ); } } elseif ( 'text/x-gfm' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'gfm', 'highlightFormatting' => true, ) ); } elseif ( 'application/javascript' === $type || 'text/javascript' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'javascript', 'lint' => true, 'autoCloseBrackets' => true, 'matchBrackets' => true, ) ); } elseif ( str_contains( $type, 'json' ) ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => array( 'name' => 'javascript', ), 'lint' => true, 'autoCloseBrackets' => true, 'matchBrackets' => true, ) ); if ( 'application/ld+json' === $type ) { $settings['codemirror']['mode']['jsonld'] = true; } else { $settings['codemirror']['mode']['json'] = true; } } elseif ( str_contains( $type, 'jsx' ) ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'jsx', 'autoCloseBrackets' => true, 'matchBrackets' => true, ) ); } elseif ( 'text/x-markdown' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'markdown', 'highlightFormatting' => true, ) ); } elseif ( 'text/nginx' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'nginx', ) ); } elseif ( 'application/x-httpd-php' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'php', 'autoCloseBrackets' => true, 'autoCloseTags' => true, 'matchBrackets' => true, 'matchTags' => array( 'bothTags' => true, ), ) ); } elseif ( 'text/x-sql' === $type || 'text/x-mysql' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'sql', 'autoCloseBrackets' => true, 'matchBrackets' => true, ) ); } elseif ( str_contains( $type, 'xml' ) ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'xml', 'autoCloseBrackets' => true, 'autoCloseTags' => true, 'matchTags' => array( 'bothTags' => true, ), ) ); } elseif ( 'text/x-yaml' === $type ) { $settings['codemirror'] = array_merge( $settings['codemirror'], array( 'mode' => 'yaml', ) ); } else { $settings['codemirror']['mode'] = $type; } if ( ! empty( $settings['codemirror']['lint'] ) ) { $settings['codemirror']['gutters'][] = 'CodeMirror-lint-markers'; } // Let settings supplied via args override any defaults. foreach ( wp_array_slice_assoc( $args, array( 'codemirror', 'csslint', 'jshint', 'htmlhint' ) ) as $key => $value ) { $settings[ $key ] = array_merge( $settings[ $key ], $value ); } /** * Filters settings that are passed into the code editor. * * Returning a falsey value will disable the syntax-highlighting code editor. * * @since 4.9.0 * * @param array $settings The array of settings passed to the code editor. * A falsey value disables the editor. * @param array $args { * Args passed when calling `get_code_editor_settings()`. * * @type string $type The MIME type of the file to be edited. * @type string $file Filename being edited. * @type WP_Theme $theme Theme being edited when on the theme file editor. * @type string $plugin Plugin being edited when on the plugin file editor. * @type array $codemirror Additional CodeMirror setting overrides. * @type array $csslint CSSLint rule overrides. * @type array $jshint JSHint rule overrides. * @type array $htmlhint HTMLHint rule overrides. * } */ return apply_filters( 'wp_code_editor_settings', $settings, $args ); } /** * Retrieves the contents of the search WordPress query variable. * * The search query string is passed through esc_attr() to ensure that it is safe * for placing in an HTML attribute. * * @since 2.3.0 * * @param bool $escaped Whether the result is escaped. Default true. * Only use when you are later escaping it. Do not use unescaped. * @return string */ function get_search_query( $escaped = true ) { /** * Filters the contents of the search query variable. * * @since 2.3.0 * * @param mixed $search Contents of the search query variable. */ $query = apply_filters( 'get_search_query', get_query_var( 's' ) ); if ( $escaped ) { $query = esc_attr( $query ); } return $query; } /** * Displays the contents of the search query variable. * * The search query string is passed through esc_attr() to ensure that it is safe * for placing in an HTML attribute. * * @since 2.1.0 */ function the_search_query() { /** * Filters the contents of the search query variable for display. * * @since 2.3.0 * * @param mixed $search Contents of the search query variable. */ echo esc_attr( apply_filters( 'the_search_query', get_search_query( false ) ) ); } /** * Gets the language attributes for the 'html' tag. * * Builds up a set of HTML attributes containing the text direction and language * information for the page. * * @since 4.3.0 * * @param string $doctype Optional. The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'. * @return string A space-separated list of language attributes. */ function get_language_attributes( $doctype = 'html' ) { $attributes = array(); if ( function_exists( 'is_rtl' ) && is_rtl() ) { $attributes[] = 'dir="rtl"'; } $lang = get_bloginfo( 'language' ); if ( $lang ) { if ( 'text/html' === get_option( 'html_type' ) || 'html' === $doctype ) { $attributes[] = 'lang="' . esc_attr( $lang ) . '"'; } if ( 'text/html' !== get_option( 'html_type' ) || 'xhtml' === $doctype ) { $attributes[] = 'xml:lang="' . esc_attr( $lang ) . '"'; } } $output = implode( ' ', $attributes ); /** * Filters the language attributes for display in the 'html' tag. * * @since 2.5.0 * @since 4.3.0 Added the `$doctype` parameter. * * @param string $output A space-separated list of language attributes. * @param string $doctype The type of HTML document (xhtml|html). */ return apply_filters( 'language_attributes', $output, $doctype ); } /** * Displays the language attributes for the 'html' tag. * * Builds up a set of HTML attributes containing the text direction and language * information for the page. * * @since 2.1.0 * @since 4.3.0 Converted into a wrapper for get_language_attributes(). * * @param string $doctype Optional. The type of HTML document. Accepts 'xhtml' or 'html'. Default 'html'. */ function language_attributes( $doctype = 'html' ) { echo get_language_attributes( $doctype ); } /** * Retrieves paginated links for archive post pages. * * Technically, the function can be used to create paginated link list for any * area. The 'base' argument is used to reference the url, which will be used to * create the paginated links. The 'format' argument is then used for replacing * the page number. It is however, most likely and by default, to be used on the * archive post pages. * * The 'type' argument controls format of the returned value. The default is * 'plain', which is just a string with the links separated by a newline * character. The other possible values are either 'array' or 'list'. The * 'array' value will return an array of the paginated link list to offer full * control of display. The 'list' value will place all of the paginated links in * an unordered HTML list. * * The 'total' argument is the total amount of pages and is an integer. The * 'current' argument is the current page number and is also an integer. * * An example of the 'base' argument is "http://example.com/all_posts.php%_%" * and the '%_%' is required. The '%_%' will be replaced by the contents of in * the 'format' argument. An example for the 'format' argument is "?page=%#%" * and the '%#%' is also required. The '%#%' will be replaced with the page * number. * * You can include the previous and next links in the list by setting the * 'prev_next' argument to true, which it is by default. You can set the * previous text, by using the 'prev_text' argument. You can set the next text * by setting the 'next_text' argument. * * If the 'show_all' argument is set to true, then it will show all of the pages * instead of a short list of the pages near the current page. By default, the * 'show_all' is set to false and controlled by the 'end_size' and 'mid_size' * arguments. The 'end_size' argument is how many numbers on either the start * and the end list edges, by default is 1. The 'mid_size' argument is how many * numbers to either side of current page, but not including current page. * * It is possible to add query vars to the link by using the 'add_args' argument * and see add_query_arg() for more information. * * The 'before_page_number' and 'after_page_number' arguments allow users to * augment the links themselves. Typically this might be to add context to the * numbered links so that screen reader users understand what the links are for. * The text strings are added before and after the page number - within the * anchor tag. * * @since 2.1.0 * @since 4.9.0 Added the `aria_current` argument. * * @global WP_Query $wp_query WordPress Query object. * @global WP_Rewrite $wp_rewrite WordPress rewrite component. * * @param string|array $args { * Optional. Array or string of arguments for generating paginated links for archives. * * @type string $base Base of the paginated url. Default empty. * @type string $format Format for the pagination structure. Default empty. * @type int $total The total amount of pages. Default is the value WP_Query's * `max_num_pages` or 1. * @type int $current The current page number. Default is 'paged' query var or 1. * @type string $aria_current The value for the aria-current attribute. Possible values are 'page', * 'step', 'location', 'date', 'time', 'true', 'false'. Default is 'page'. * @type bool $show_all Whether to show all pages. Default false. * @type int $end_size How many numbers on either the start and the end list edges. * Default 1. * @type int $mid_size How many numbers to either side of the current pages. Default 2. * @type bool $prev_next Whether to include the previous and next links in the list. Default true. * @type string $prev_text The previous page text. Default '« Previous'. * @type string $next_text The next page text. Default 'Next »'. * @type string $type Controls format of the returned value. Possible values are 'plain', * 'array' and 'list'. Default is 'plain'. * @type array $add_args An array of query args to add. Default false. * @type string $add_fragment A string to append to each link. Default empty. * @type string $before_page_number A string to appear before the page number. Default empty. * @type string $after_page_number A string to append after the page number. Default empty. * } * @return string|string[]|void String of page links or array of page links, depending on 'type' argument. * Void if total number of pages is less than 2. */ function paginate_links( $args = '' ) { global $wp_query, $wp_rewrite; // Setting up default values based on the current URL. $pagenum_link = html_entity_decode( get_pagenum_link() ); $url_parts = explode( '?', $pagenum_link ); // Get max pages and current page out of the current query, if available. $total = isset( $wp_query->max_num_pages ) ? $wp_query->max_num_pages : 1; $current = get_query_var( 'paged' ) ? (int) get_query_var( 'paged' ) : 1; // Append the format placeholder to the base URL. $pagenum_link = trailingslashit( $url_parts[0] ) . '%_%'; // URL base depends on permalink settings. $format = $wp_rewrite->using_index_permalinks() && ! strpos( $pagenum_link, 'index.php' ) ? 'index.php/' : ''; $format .= $wp_rewrite->using_permalinks() ? user_trailingslashit( $wp_rewrite->pagination_base . '/%#%', 'paged' ) : '?paged=%#%'; $defaults = array( 'base' => $pagenum_link, // http://example.com/all_posts.php%_% : %_% is replaced by format (below). 'format' => $format, // ?page=%#% : %#% is replaced by the page number. 'total' => $total, 'current' => $current, 'aria_current' => 'page', 'show_all' => false, 'prev_next' => true, 'prev_text' => __( '« Previous' ), 'next_text' => __( 'Next »' ), 'end_size' => 1, 'mid_size' => 2, 'type' => 'plain', 'add_args' => array(), // Array of query args to add. 'add_fragment' => '', 'before_page_number' => '', 'after_page_number' => '', ); $args = wp_parse_args( $args, $defaults ); if ( ! is_array( $args['add_args'] ) ) { $args['add_args'] = array(); } // Merge additional query vars found in the original URL into 'add_args' array. if ( isset( $url_parts[1] ) ) { // Find the format argument. $format = explode( '?', str_replace( '%_%', $args['format'], $args['base'] ) ); $format_query = isset( $format[1] ) ? $format[1] : ''; wp_parse_str( $format_query, $format_args ); // Find the query args of the requested URL. wp_parse_str( $url_parts[1], $url_query_args ); // Remove the format argument from the array of query arguments, to avoid overwriting custom format. foreach ( $format_args as $format_arg => $format_arg_value ) { unset( $url_query_args[ $format_arg ] ); } $args['add_args'] = array_merge( $args['add_args'], urlencode_deep( $url_query_args ) ); } // Who knows what else people pass in $args. $total = (int) $args['total']; if ( $total < 2 ) { return; } $current = (int) $args['current']; $end_size = (int) $args['end_size']; // Out of bounds? Make it the default. if ( $end_size < 1 ) { $end_size = 1; } $mid_size = (int) $args['mid_size']; if ( $mid_size < 0 ) { $mid_size = 2; } $add_args = $args['add_args']; $r = ''; $page_links = array(); $dots = false; if ( $args['prev_next'] && $current && 1 < $current ) : $link = str_replace( '%_%', 2 == $current ? '' : $args['format'], $args['base'] ); $link = str_replace( '%#%', $current - 1, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); } $link .= $args['add_fragment']; $page_links[] = sprintf( '', /** * Filters the paginated links for the given archive pages. * * @since 3.0.0 * * @param string $link The paginated link URL. */ esc_url( apply_filters( 'paginate_links', $link ) ), $args['prev_text'] ); endif; for ( $n = 1; $n <= $total; $n++ ) : if ( $n == $current ) : $page_links[] = sprintf( '%s', esc_attr( $args['aria_current'] ), $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] ); $dots = true; else : if ( $args['show_all'] || ( $n <= $end_size || ( $current && $n >= $current - $mid_size && $n <= $current + $mid_size ) || $n > $total - $end_size ) ) : $link = str_replace( '%_%', 1 == $n ? '' : $args['format'], $args['base'] ); $link = str_replace( '%#%', $n, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); } $link .= $args['add_fragment']; $page_links[] = sprintf( '%s', /** This filter is documented in wp-includes/general-template.php */ esc_url( apply_filters( 'paginate_links', $link ) ), $args['before_page_number'] . number_format_i18n( $n ) . $args['after_page_number'] ); $dots = true; elseif ( $dots && ! $args['show_all'] ) : $page_links[] = '' . __( '…' ) . ''; $dots = false; endif; endif; endfor; if ( $args['prev_next'] && $current && $current < $total ) : $link = str_replace( '%_%', $args['format'], $args['base'] ); $link = str_replace( '%#%', $current + 1, $link ); if ( $add_args ) { $link = add_query_arg( $add_args, $link ); } $link .= $args['add_fragment']; $page_links[] = sprintf( '', /** This filter is documented in wp-includes/general-template.php */ esc_url( apply_filters( 'paginate_links', $link ) ), $args['next_text'] ); endif; switch ( $args['type'] ) { case 'array': return $page_links; case 'list': $r .= "\n"; break; default: $r = implode( "\n", $page_links ); break; } /** * Filters the HTML output of paginated links for archives. * * @since 5.7.0 * * @param string $r HTML output. * @param array $args An array of arguments. See paginate_links() * for information on accepted arguments. */ $r = apply_filters( 'paginate_links_output', $r, $args ); return $r; } /** * Registers an admin color scheme css file. * * Allows a plugin to register a new admin color scheme. For example: * * wp_admin_css_color( 'classic', __( 'Classic' ), admin_url( "css/colors-classic.css" ), array( * '#07273E', '#14568A', '#D54E21', '#2683AE' * ) ); * * @since 2.5.0 * * @global array $_wp_admin_css_colors * * @param string $key The unique key for this theme. * @param string $name The name of the theme. * @param string $url The URL of the CSS file containing the color scheme. * @param array $colors Optional. An array of CSS color definition strings which are used * to give the user a feel for the theme. * @param array $icons { * Optional. CSS color definitions used to color any SVG icons. * * @type string $base SVG icon base color. * @type string $focus SVG icon color on focus. * @type string $current SVG icon color of current admin menu link. * } */ function wp_admin_css_color( $key, $name, $url, $colors = array(), $icons = array() ) { global $_wp_admin_css_colors; if ( ! isset( $_wp_admin_css_colors ) ) { $_wp_admin_css_colors = array(); } $_wp_admin_css_colors[ $key ] = (object) array( 'name' => $name, 'url' => $url, 'colors' => $colors, 'icon_colors' => $icons, ); } /** * Registers the default admin color schemes. * * Registers the initial set of eight color schemes in the Profile section * of the dashboard which allows for styling the admin menu and toolbar. * * @see wp_admin_css_color() * * @since 3.0.0 */ function register_admin_color_schemes() { $suffix = is_rtl() ? '-rtl' : ''; $suffix .= SCRIPT_DEBUG ? '' : '.min'; wp_admin_css_color( 'fresh', _x( 'Default', 'admin color scheme' ), false, array( '#1d2327', '#2c3338', '#2271b1', '#72aee6' ), array( 'base' => '#a7aaad', 'focus' => '#72aee6', 'current' => '#fff', ) ); wp_admin_css_color( 'light', _x( 'Light', 'admin color scheme' ), admin_url( "css/colors/light/colors$suffix.css" ), array( '#e5e5e5', '#999', '#d64e07', '#04a4cc' ), array( 'base' => '#999', 'focus' => '#ccc', 'current' => '#ccc', ) ); wp_admin_css_color( 'modern', _x( 'Modern', 'admin color scheme' ), admin_url( "css/colors/modern/colors$suffix.css" ), array( '#1e1e1e', '#3858e9', '#33f078' ), array( 'base' => '#f3f1f1', 'focus' => '#fff', 'current' => '#fff', ) ); wp_admin_css_color( 'blue', _x( 'Blue', 'admin color scheme' ), admin_url( "css/colors/blue/colors$suffix.css" ), array( '#096484', '#4796b3', '#52accc', '#74B6CE' ), array( 'base' => '#e5f8ff', 'focus' => '#fff', 'current' => '#fff', ) ); wp_admin_css_color( 'midnight', _x( 'Midnight', 'admin color scheme' ), admin_url( "css/colors/midnight/colors$suffix.css" ), array( '#25282b', '#363b3f', '#69a8bb', '#e14d43' ), array( 'base' => '#f1f2f3', 'focus' => '#fff', 'current' => '#fff', ) ); wp_admin_css_color( 'sunrise', _x( 'Sunrise', 'admin color scheme' ), admin_url( "css/colors/sunrise/colors$suffix.css" ), array( '#b43c38', '#cf4944', '#dd823b', '#ccaf0b' ), array( 'base' => '#f3f1f1', 'focus' => '#fff', 'current' => '#fff', ) ); wp_admin_css_color( 'ectoplasm', _x( 'Ectoplasm', 'admin color scheme' ), admin_url( "css/colors/ectoplasm/colors$suffix.css" ), array( '#413256', '#523f6d', '#a3b745', '#d46f15' ), array( 'base' => '#ece6f6', 'focus' => '#fff', 'current' => '#fff', ) ); wp_admin_css_color( 'ocean', _x( 'Ocean', 'admin color scheme' ), admin_url( "css/colors/ocean/colors$suffix.css" ), array( '#627c83', '#738e96', '#9ebaa0', '#aa9d88' ), array( 'base' => '#f2fcff', 'focus' => '#fff', 'current' => '#fff', ) ); wp_admin_css_color( 'coffee', _x( 'Coffee', 'admin color scheme' ), admin_url( "css/colors/coffee/colors$suffix.css" ), array( '#46403c', '#59524c', '#c7a589', '#9ea476' ), array( 'base' => '#f3f2f1', 'focus' => '#fff', 'current' => '#fff', ) ); } /** * Displays the URL of a WordPress admin CSS file. * * @see WP_Styles::_css_href() and its {@see 'style_loader_src'} filter. * * @since 2.3.0 * * @param string $file file relative to wp-admin/ without its ".css" extension. * @return string */ function wp_admin_css_uri( $file = 'wp-admin' ) { if ( defined( 'WP_INSTALLING' ) ) { $_file = "./$file.css"; } else { $_file = admin_url( "$file.css" ); } $_file = add_query_arg( 'version', get_bloginfo( 'version' ), $_file ); /** * Filters the URI of a WordPress admin CSS file. * * @since 2.3.0 * * @param string $_file Relative path to the file with query arguments attached. * @param string $file Relative path to the file, minus its ".css" extension. */ return apply_filters( 'wp_admin_css_uri', $_file, $file ); } /** * Enqueues or directly prints a stylesheet link to the specified CSS file. * * "Intelligently" decides to enqueue or to print the CSS file. If the * {@see 'wp_print_styles'} action has *not* yet been called, the CSS file will be * enqueued. If the {@see 'wp_print_styles'} action has been called, the CSS link will * be printed. Printing may be forced by passing true as the $force_echo * (second) parameter. * * For backward compatibility with WordPress 2.3 calling method: If the $file * (first) parameter does not correspond to a registered CSS file, we assume * $file is a file relative to wp-admin/ without its ".css" extension. A * stylesheet link to that generated URL is printed. * * @since 2.3.0 * * @param string $file Optional. Style handle name or file name (without ".css" extension) relative * to wp-admin/. Defaults to 'wp-admin'. * @param bool $force_echo Optional. Force the stylesheet link to be printed rather than enqueued. */ function wp_admin_css( $file = 'wp-admin', $force_echo = false ) { // For backward compatibility. $handle = str_starts_with( $file, 'css/' ) ? substr( $file, 4 ) : $file; if ( wp_styles()->query( $handle ) ) { if ( $force_echo || did_action( 'wp_print_styles' ) ) { // We already printed the style queue. Print this one immediately. wp_print_styles( $handle ); } else { // Add to style queue. wp_enqueue_style( $handle ); } return; } $stylesheet_link = sprintf( "\n", esc_url( wp_admin_css_uri( $file ) ) ); /** * Filters the stylesheet link to the specified CSS file. * * If the site is set to display right-to-left, the RTL stylesheet link * will be used instead. * * @since 2.3.0 * @param string $stylesheet_link HTML link element for the stylesheet. * @param string $file Style handle name or filename (without ".css" extension) * relative to wp-admin/. Defaults to 'wp-admin'. */ echo apply_filters( 'wp_admin_css', $stylesheet_link, $file ); if ( function_exists( 'is_rtl' ) && is_rtl() ) { $rtl_stylesheet_link = sprintf( "\n", esc_url( wp_admin_css_uri( "$file-rtl" ) ) ); /** This filter is documented in wp-includes/general-template.php */ echo apply_filters( 'wp_admin_css', $rtl_stylesheet_link, "$file-rtl" ); } } /** * Enqueues the default ThickBox js and css. * * If any of the settings need to be changed, this can be done with another js * file similar to media-upload.js. That file should * require array('thickbox') to ensure it is loaded after. * * @since 2.5.0 */ function add_thickbox() { wp_enqueue_script( 'thickbox' ); wp_enqueue_style( 'thickbox' ); if ( is_network_admin() ) { add_action( 'admin_head', '_thickbox_path_admin_subfolder' ); } } /** * Displays the XHTML generator that is generated on the wp_head hook. * * See {@see 'wp_head'}. * * @since 2.5.0 */ function wp_generator() { /** * Filters the output of the XHTML generator tag. * * @since 2.5.0 * * @param string $generator_type The XHTML generator. */ the_generator( apply_filters( 'wp_generator_type', 'xhtml' ) ); } /** * Displays the generator XML or Comment for RSS, ATOM, etc. * * Returns the correct generator type for the requested output format. Allows * for a plugin to filter generators overall the {@see 'the_generator'} filter. * * @since 2.5.0 * * @param string $type The type of generator to output - (html|xhtml|atom|rss2|rdf|comment|export). */ function the_generator( $type ) { /** * Filters the output of the XHTML generator tag for display. * * @since 2.5.0 * * @param string $generator_type The generator output. * @param string $type The type of generator to output. Accepts 'html', * 'xhtml', 'atom', 'rss2', 'rdf', 'comment', 'export'. */ echo apply_filters( 'the_generator', get_the_generator( $type ), $type ) . "\n"; } /** * Creates the generator XML or Comment for RSS, ATOM, etc. * * Returns the correct generator type for the requested output format. Allows * for a plugin to filter generators on an individual basis using the * {@see 'get_the_generator_$type'} filter. * * @since 2.5.0 * * @param string $type The type of generator to return - (html|xhtml|atom|rss2|rdf|comment|export). * @return string|void The HTML content for the generator. */ function get_the_generator( $type = '' ) { if ( empty( $type ) ) { $current_filter = current_filter(); if ( empty( $current_filter ) ) { return; } switch ( $current_filter ) { case 'rss2_head': case 'commentsrss2_head': $type = 'rss2'; break; case 'rss_head': case 'opml_head': $type = 'comment'; break; case 'rdf_header': $type = 'rdf'; break; case 'atom_head': case 'comments_atom_head': case 'app_head': $type = 'atom'; break; } } switch ( $type ) { case 'html': $gen = ''; break; case 'xhtml': $gen = ''; break; case 'atom': $gen = 'WordPress'; break; case 'rss2': $gen = '' . sanitize_url( 'https://wordpress.org/?v=' . get_bloginfo_rss( 'version' ) ) . ''; break; case 'rdf': $gen = ''; break; case 'comment': $gen = ''; break; case 'export': $gen = ''; break; } /** * Filters the HTML for the retrieved generator type. * * The dynamic portion of the hook name, `$type`, refers to the generator type. * * Possible hook names include: * * - `get_the_generator_atom` * - `get_the_generator_comment` * - `get_the_generator_export` * - `get_the_generator_html` * - `get_the_generator_rdf` * - `get_the_generator_rss2` * - `get_the_generator_xhtml` * * @since 2.5.0 * * @param string $gen The HTML markup output to wp_head(). * @param string $type The type of generator. Accepts 'html', 'xhtml', 'atom', * 'rss2', 'rdf', 'comment', 'export'. */ return apply_filters( "get_the_generator_{$type}", $gen, $type ); } /** * Outputs the HTML checked attribute. * * Compares the first two arguments and if identical marks as checked. * * @since 1.0.0 * * @param mixed $checked One of the values to compare. * @param mixed $current Optional. The other value to compare if not just true. * Default true. * @param bool $display Optional. Whether to echo or just return the string. * Default true. * @return string HTML attribute or empty string. */ function checked( $checked, $current = true, $display = true ) { return __checked_selected_helper( $checked, $current, $display, 'checked' ); } /** * Outputs the HTML selected attribute. * * Compares the first two arguments and if identical marks as selected. * * @since 1.0.0 * * @param mixed $selected One of the values to compare. * @param mixed $current Optional. The other value to compare if not just true. * Default true. * @param bool $display Optional. Whether to echo or just return the string. * Default true. * @return string HTML attribute or empty string. */ function selected( $selected, $current = true, $display = true ) { return __checked_selected_helper( $selected, $current, $display, 'selected' ); } /** * Outputs the HTML disabled attribute. * * Compares the first two arguments and if identical marks as disabled. * * @since 3.0.0 * * @param mixed $disabled One of the values to compare. * @param mixed $current Optional. The other value to compare if not just true. * Default true. * @param bool $display Optional. Whether to echo or just return the string. * Default true. * @return string HTML attribute or empty string. */ function disabled( $disabled, $current = true, $display = true ) { return __checked_selected_helper( $disabled, $current, $display, 'disabled' ); } /** * Outputs the HTML readonly attribute. * * Compares the first two arguments and if identical marks as readonly. * * @since 5.9.0 * * @param mixed $readonly_value One of the values to compare. * @param mixed $current Optional. The other value to compare if not just true. * Default true. * @param bool $display Optional. Whether to echo or just return the string. * Default true. * @return string HTML attribute or empty string. */ function wp_readonly( $readonly_value, $current = true, $display = true ) { return __checked_selected_helper( $readonly_value, $current, $display, 'readonly' ); } /* * Include a compat `readonly()` function on PHP < 8.1. Since PHP 8.1, * `readonly` is a reserved keyword and cannot be used as a function name. * In order to avoid PHP parser errors, this function was extracted * to a separate file and is only included conditionally on PHP < 8.1. */ if ( PHP_VERSION_ID < 80100 ) { require_once __DIR__ . '/php-compat/readonly.php'; } /** * Private helper function for checked, selected, disabled and readonly. * * Compares the first two arguments and if identical marks as `$type`. * * @since 2.8.0 * @access private * * @param mixed $helper One of the values to compare. * @param mixed $current The other value to compare if not just true. * @param bool $display Whether to echo or just return the string. * @param string $type The type of checked|selected|disabled|readonly we are doing. * @return string HTML attribute or empty string. */ function __checked_selected_helper( $helper, $current, $display, $type ) { // phpcs:ignore WordPress.NamingConventions.ValidFunctionName.FunctionDoubleUnderscore,PHPCompatibility.FunctionNameRestrictions.ReservedFunctionNames.FunctionDoubleUnderscore if ( (string) $helper === (string) $current ) { $result = " $type='$type'"; } else { $result = ''; } if ( $display ) { echo $result; } return $result; } /** * Assigns a visual indicator for required form fields. * * @since 6.1.0 * * @return string Indicator glyph wrapped in a `span` tag. */ function wp_required_field_indicator() { /* translators: Character to identify required form fields. */ $glyph = __( '*' ); $indicator = '' . esc_html( $glyph ) . ''; /** * Filters the markup for a visual indicator of required form fields. * * @since 6.1.0 * * @param string $indicator Markup for the indicator element. */ return apply_filters( 'wp_required_field_indicator', $indicator ); } /** * Creates a message to explain required form fields. * * @since 6.1.0 * * @return string Message text and glyph wrapped in a `span` tag. */ function wp_required_field_message() { $message = sprintf( '%s', /* translators: %s: Asterisk symbol (*). */ sprintf( __( 'Required fields are marked %s' ), wp_required_field_indicator() ) ); /** * Filters the message to explain required form fields. * * @since 6.1.0 * * @param string $message Message text and glyph wrapped in a `span` tag. */ return apply_filters( 'wp_required_field_message', $message ); } /** * Default settings for heartbeat. * * Outputs the nonce used in the heartbeat XHR. * * @since 3.6.0 * * @param array $settings * @return array Heartbeat settings. */ function wp_heartbeat_settings( $settings ) { if ( ! is_admin() ) { $settings['ajaxurl'] = admin_url( 'admin-ajax.php', 'relative' ); } if ( is_user_logged_in() ) { $settings['nonce'] = wp_create_nonce( 'heartbeat-nonce' ); } return $settings; } hook ] ); if ( empty( $crons[ $timestamp ] ) ) { unset( $crons[ $timestamp ] ); } } /* * If the results are empty (zero events to unschedule), no attempt * to update the cron array is required. */ if ( empty( $results ) ) { return 0; } $set = _set_cron_array( $crons, $wp_error ); if ( true === $set ) { return array_sum( $results ); } return $set; } /** * Retrieves a scheduled event. * * Retrieves the full event object for a given event, if no timestamp is specified the next * scheduled event is returned. * * @since 5.1.0 * * @param string $hook Action hook of the event. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. * Although not passed to a callback, these arguments are used to uniquely identify the * event, so they should be the same as those used when originally scheduling the event. * Default empty array. * @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event * is returned. Default null. * @return object|false { * The event object. False if the event does not exist. * * @type string $hook Action hook to execute when the event is run. * @type int $timestamp Unix timestamp (UTC) for when to next run the event. * @type string|false $schedule How often the event should subsequently recur. * @type array $args Array containing each separate argument to pass to the hook's callback function. * @type int $interval Optional. The interval time in seconds for the schedule. Only present for recurring events. * } */ function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) { /** * Filter to override retrieving a scheduled event. * * Returning a non-null value will short-circuit the normal process, * returning the filtered value instead. * * Return false if the event does not exist, otherwise an event object * should be returned. * * @since 5.1.0 * * @param null|false|object $pre Value to return instead. Default null to continue retrieving the event. * @param string $hook Action hook of the event. * @param array $args Array containing each separate argument to pass to the hook's callback function. * Although not passed to a callback, these arguments are used to uniquely identify * the event. * @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event. */ $pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp ); if ( null !== $pre ) { return $pre; } if ( null !== $timestamp && ! is_numeric( $timestamp ) ) { return false; } $crons = _get_cron_array(); if ( empty( $crons ) ) { return false; } $key = md5( serialize( $args ) ); if ( ! $timestamp ) { // Get next event. $next = false; foreach ( $crons as $timestamp => $cron ) { if ( isset( $cron[ $hook ][ $key ] ) ) { $next = $timestamp; break; } } if ( ! $next ) { return false; } $timestamp = $next; } elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) { return false; } $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $crons[ $timestamp ][ $hook ][ $key ]['schedule'], 'args' => $args, ); if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) { $event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval']; } return $event; } /** * Retrieves the next timestamp for an event. * * @since 2.1.0 * * @param string $hook Action hook of the event. * @param array $args Optional. Array containing each separate argument to pass to the hook's callback function. * Although not passed to a callback, these arguments are used to uniquely identify the * event, so they should be the same as those used when originally scheduling the event. * Default empty array. * @return int|false The Unix timestamp of the next time the event will occur. False if the event doesn't exist. */ function wp_next_scheduled( $hook, $args = array() ) { $next_event = wp_get_scheduled_event( $hook, $args ); if ( ! $next_event ) { return false; } return $next_event->timestamp; } /** * Sends a request to run cron through HTTP request that doesn't halt page loading. * * @since 2.1.0 * @since 5.1.0 Return values added. * * @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used). * @return bool True if spawned, false if no events spawned. */ function spawn_cron( $gmt_time = 0 ) { if ( ! $gmt_time ) { $gmt_time = microtime( true ); } if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) { return false; } /* * Get the cron lock, which is a Unix timestamp of when the last cron was spawned * and has not finished running. * * Multiple processes on multiple web servers can run this code concurrently, * this lock attempts to make spawning as atomic as possible. */ $lock = (float) get_transient( 'doing_cron' ); if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) { $lock = 0; } // Don't run if another process is currently running it or more than once every 60 sec. if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) { return false; } // Confidence check. $crons = wp_get_ready_cron_jobs(); if ( empty( $crons ) ) { return false; } $keys = array_keys( $crons ); if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { return false; } if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { return false; } $doing_wp_cron = sprintf( '%.22F', $gmt_time ); set_transient( 'doing_cron', $doing_wp_cron ); ob_start(); wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) ); echo ' '; // Flush any buffers and send the headers. wp_ob_end_flush_all(); flush(); require_once ABSPATH . 'wp-cron.php'; return true; } // Set the cron lock with the current unix timestamp, when the cron is being spawned. $doing_wp_cron = sprintf( '%.22F', $gmt_time ); set_transient( 'doing_cron', $doing_wp_cron ); /** * Filters the cron request arguments. * * @since 3.5.0 * @since 4.5.0 The `$doing_wp_cron` parameter was added. * * @param array $cron_request_array { * An array of cron request URL arguments. * * @type string $url The cron request URL. * @type int $key The 22 digit GMT microtime. * @type array $args { * An array of cron request arguments. * * @type int $timeout The request timeout in seconds. Default .01 seconds. * @type bool $blocking Whether to set blocking for the request. Default false. * @type bool $sslverify Whether SSL should be verified for the request. Default false. * } * } * @param string $doing_wp_cron The unix timestamp of the cron lock. */ $cron_request = apply_filters( 'cron_request', array( 'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ), 'key' => $doing_wp_cron, 'args' => array( 'timeout' => 0.01, 'blocking' => false, /** This filter is documented in wp-includes/class-wp-http-streams.php */ 'sslverify' => apply_filters( 'https_local_ssl_verify', false ), ), ), $doing_wp_cron ); $result = wp_remote_post( $cron_request['url'], $cron_request['args'] ); return ! is_wp_error( $result ); } /** * Registers _wp_cron() to run on the {@see 'wp_loaded'} action. * * If the {@see 'wp_loaded'} action has already fired, this function calls * _wp_cron() directly. * * Warning: This function may return Boolean FALSE, but may also return a non-Boolean * value which evaluates to FALSE. For information about casting to booleans see the * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use * the `===` operator for testing the return value of this function. * * @since 2.1.0 * @since 5.1.0 Return value added to indicate success or failure. * @since 5.7.0 Functionality moved to _wp_cron() to which this becomes a wrapper. * * @return false|int|void On success an integer indicating number of events spawned (0 indicates no * events needed to be spawned), false if spawning fails for one or more events or * void if the function registered _wp_cron() to run on the action. */ function wp_cron() { if ( did_action( 'wp_loaded' ) ) { return _wp_cron(); } add_action( 'wp_loaded', '_wp_cron', 20 ); } /** * Runs scheduled callbacks or spawns cron for all scheduled events. * * Warning: This function may return Boolean FALSE, but may also return a non-Boolean * value which evaluates to FALSE. For information about casting to booleans see the * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use * the `===` operator for testing the return value of this function. * * @since 5.7.0 * @access private * * @return int|false On success an integer indicating number of events spawned (0 indicates no * events needed to be spawned), false if spawning fails for one or more events. */ function _wp_cron() { // Prevent infinite loops caused by lack of wp-cron.php. if ( str_contains( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) { return 0; } $crons = wp_get_ready_cron_jobs(); if ( empty( $crons ) ) { return 0; } $gmt_time = microtime( true ); $keys = array_keys( $crons ); if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { return 0; } $schedules = wp_get_schedules(); $results = array(); foreach ( $crons as $timestamp => $cronhooks ) { if ( $timestamp > $gmt_time ) { break; } foreach ( (array) $cronhooks as $hook => $args ) { if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) { continue; } $results[] = spawn_cron( $gmt_time ); break 2; } } if ( in_array( false, $results, true ) ) { return false; } return count( $results ); } /** * Retrieves supported event recurrence schedules. * * The default supported recurrences are 'hourly', 'twicedaily', 'daily', and 'weekly'. * A plugin may add more by hooking into the {@see 'cron_schedules'} filter. * The filter accepts an array of arrays. The outer array has a key that is the name * of the schedule, for example 'monthly'. The value is an array with two keys, * one is 'interval' and the other is 'display'. * * The 'interval' is a number in seconds of when the cron job should run. * So for 'hourly' the time is `HOUR_IN_SECONDS` (`60 * 60` or `3600`). For 'monthly', * the value would be `MONTH_IN_SECONDS` (`30 * 24 * 60 * 60` or `2592000`). * * The 'display' is the description. For the 'monthly' key, the 'display' * would be `__( 'Once Monthly' )`. * * For your plugin, you will be passed an array. You can add your * schedule by doing the following: * * // Filter parameter variable name is 'array'. * $array['monthly'] = array( * 'interval' => MONTH_IN_SECONDS, * 'display' => __( 'Once Monthly' ) * ); * * @since 2.1.0 * @since 5.4.0 The 'weekly' schedule was added. * * @return array { * The array of cron schedules keyed by the schedule name. * * @type array ...$0 { * Cron schedule information. * * @type int $interval The schedule interval in seconds. * @type string $display The schedule display name. * } * } */ function wp_get_schedules() { $schedules = array( 'hourly' => array( 'interval' => HOUR_IN_SECONDS, 'display' => __( 'Once Hourly' ), ), 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ), ), 'daily' => array( 'interval' => DAY_IN_SECONDS, 'display' => __( 'Once Daily' ), ), 'weekly' => array( 'interval' => WEEK_IN_SECONDS, 'display' => __( 'Once Weekly' ), ), ); /** * Filters the non-default cron schedules. * * @since 2.1.0 * * @param array $new_schedules { * An array of non-default cron schedules keyed by the schedule name. Default empty array. * * @type array ...$0 { * Cron schedule information. * * @type int $interval The schedule interval in seconds. * @type string $display The schedule display name. * } * } */ return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); } /** * Retrieves the name of the recurrence schedule for an event. * * @see wp_get_schedules() for available schedules. * * @since 2.1.0 * @since 5.1.0 {@see 'get_schedule'} filter added. * * @param string $hook Action hook to identify the event. * @param array $args Optional. Arguments passed to the event's callback function. * Default empty array. * @return string|false Schedule name on success, false if no schedule. */ function wp_get_schedule( $hook, $args = array() ) { $schedule = false; $event = wp_get_scheduled_event( $hook, $args ); if ( $event ) { $schedule = $event->schedule; } /** * Filters the schedule name for a hook. * * @since 5.1.0 * * @param string|false $schedule Schedule for the hook. False if not found. * @param string $hook Action hook to execute when cron is run. * @param array $args Arguments to pass to the hook's callback function. */ return apply_filters( 'get_schedule', $schedule, $hook, $args ); } /** * Retrieves cron jobs ready to be run. * * Returns the results of _get_cron_array() limited to events ready to be run, * ie, with a timestamp in the past. * * @since 5.1.0 * * @return array[] Array of cron job arrays ready to be run. */ function wp_get_ready_cron_jobs() { /** * Filter to override retrieving ready cron jobs. * * Returning an array will short-circuit the normal retrieval of ready * cron jobs, causing the function to return the filtered value instead. * * @since 5.1.0 * * @param null|array[] $pre Array of ready cron tasks to return instead. Default null * to continue using results from _get_cron_array(). */ $pre = apply_filters( 'pre_get_ready_cron_jobs', null ); if ( null !== $pre ) { return $pre; } $crons = _get_cron_array(); $gmt_time = microtime( true ); $results = array(); foreach ( $crons as $timestamp => $cronhooks ) { if ( $timestamp > $gmt_time ) { break; } $results[ $timestamp ] = $cronhooks; } return $results; } // // Private functions. // /** * Retrieves cron info array option. * * @since 2.1.0 * @since 6.1.0 Return type modified to consistently return an array. * @access private * * @return array[] Array of cron events. */ function _get_cron_array() { $cron = get_option( 'cron' ); if ( ! is_array( $cron ) ) { return array(); } if ( ! isset( $cron['version'] ) ) { $cron = _upgrade_cron_array( $cron ); } unset( $cron['version'] ); return $cron; } /** * Updates the cron option with the new cron array. * * @since 2.1.0 * @since 5.1.0 Return value modified to outcome of update_option(). * @since 5.7.0 The `$wp_error` parameter was added. * * @access private * * @param array[] $cron Array of cron info arrays from _get_cron_array(). * @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false. * @return bool|WP_Error True if cron array updated. False or WP_Error on failure. */ function _set_cron_array( $cron, $wp_error = false ) { if ( ! is_array( $cron ) ) { $cron = array(); } $cron['version'] = 2; $result = update_option( 'cron', $cron ); if ( $wp_error && ! $result ) { return new WP_Error( 'could_not_set', __( 'The cron event list could not be saved.' ) ); } return $result; } /** * Upgrades a cron info array. * * This function upgrades the cron info array to version 2. * * @since 2.1.0 * @access private * * @param array $cron Cron info array from _get_cron_array(). * @return array An upgraded cron info array. */ function _upgrade_cron_array( $cron ) { if ( isset( $cron['version'] ) && 2 === $cron['version'] ) { return $cron; } $new_cron = array(); foreach ( (array) $cron as $timestamp => $hooks ) { foreach ( (array) $hooks as $hook => $args ) { $key = md5( serialize( $args['args'] ) ); $new_cron[ $timestamp ][ $hook ][ $key ] = $args; } } $new_cron['version'] = 2; update_option( 'cron', $new_cron ); return $new_cron; }