' . $post->post_excerpt . '
'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { esc_html_e( 'There is no excerpt because this is a protected post.', 'relevanssi' ); } } /** * Echoes out the permalink to the current post within Loop. * * Uses get_permalink() to get the permalink, then adds the 'highlight' * parameter if necessary using relevanssi_add_highlight(), then echoes it out. * * @param int|WP_Post $post Post ID or post object. Default is the global $post. * * @uses relevanssi_get_permalink() Fetches the current post permalink. */ function relevanssi_the_permalink( $post = 0 ) { echo esc_url( relevanssi_get_permalink( $post ) ); } /** * Prints out a list of tags for post. * * Replacement for the_tags() that does the same, but applies Relevanssi search term * highlighting on the results. * * @param string $before What is printed before the tags, default ''. * @param string $separator The separator between items, default ', '. * @param string $after What is printed after the tags, default ''. * @param boolean $echoed If true, echo, otherwise return the result. Default true. * @param int $post_id The post ID. Default current post ID (in the Loop). */ function relevanssi_the_tags( string $before = '', string $separator = ', ', string $after = '', bool $echoed = true, int $post_id = 0 ) { $tag_list = get_the_tag_list( $before, $separator, $after, $post_id ); $found = preg_match_all( '~(.*?)~', $tag_list, $matches ); if ( $found ) { $originals = $matches[0]; $tag_names = $matches[1]; $highlighted = array(); $count = count( $matches[0] ); for ( $i = 0; $i < $count; $i++ ) { $highlighted_tag_name = relevanssi_highlight_terms( $tag_names[ $i ], get_search_query(), true ); $highlighted[ $i ] = str_replace( '>' . $tag_names[ $i ] . '<', '>' . $highlighted_tag_name . '<', $originals[ $i ] ); } $tag_list = str_replace( $originals, $highlighted, $tag_list ); } if ( $echoed ) { echo $tag_list; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { return $tag_list; } } /** * Prints out post title with highlighting. * * Uses the global $post object. Reads the highlighted title from * $post->post_highlighted_title. This used to accept one parameter, the * `$echo` boolean, but in 2.12.3 / 4.10.3 the function signature was matched * to copy `the_title()` function in WordPress core. The original behaviour is * still supported: `relevanssi_the_title()` without arguments works exactly as * before and `relevanssi_the_title( false )` returns the title. * * @global object $post The global post object. * * @param boolean|string $before Markup to prepend to the title. Can also be a * boolean for whether to echo or return the title. * @param string $after Markup to append to the title. * @param boolean $echoed Whether to echo or return the title. Default * true for echo. * * @return void|string Void if $echoed argument is true, current post title with * highlights if $echoed is false. */ function relevanssi_the_title( $before = true, string $after = '', bool $echoed = true ) { if ( true === $before ) { $before = ''; $echoed = true; } elseif ( false === $before ) { $before = ''; $echoed = false; } global $post; if ( empty( $post->post_highlighted_title ) ) { $post->post_highlighted_title = $post->post_title; } if ( relevanssi_strlen( $post->post_highlighted_title ) === 0 ) { return; } $title = $before . $post->post_highlighted_title . $after; if ( $echoed ) { echo $title; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped } else { return $title; } } /** * Turns off options, ie. sets them to "off". * * If the specified options don't exist in the request array, they are set to * "off". * * @param array $request The _REQUEST array, passed as reference. * @param array $options An array of option names. */ function relevanssi_turn_off_options( array &$request, array $options ) { array_walk( $options, function ( $option ) use ( &$request ) { if ( ! isset( $request[ $option ] ) ) { $request[ $option ] = 'off'; } } ); } /** * Sets an option after doing floatval. * * @param array $request An array of option values. * @param string $option The key to check. * @param boolean $autoload Should the option autoload, default true. * @param float $def_val The default value if floatval() fails, default 0. * @param boolean $positive If true, replace negative values and zeroes with * $def_val. */ function relevanssi_update_floatval( array $request, string $option, bool $autoload = true, float $def_val = 0, bool $positive = false ) { if ( isset( $request[ $option ] ) ) { $value = floatval( $request[ $option ] ); if ( ! $value ) { $value = $def_val; } if ( $positive && $value <= 0 ) { $value = $def_val; } update_option( $option, $value, $autoload ); } } /** * Sets an option after doing intval. * * @param array $request An array of option values. * @param string $option The key to check. * @param boolean $autoload Should the option autoload, default true. * @param int $def_val The default value if intval() fails, default 0. */ function relevanssi_update_intval( array $request, string $option, bool $autoload = true, int $def_val = 0 ) { if ( isset( $request[ $option ] ) ) { $value = intval( $request[ $option ] ); if ( ! $value ) { $value = $def_val; } update_option( $option, $value, $autoload ); } } /** * Sets an option with one of the listed legal values. * * @param array $request An array of option values. * @param string $option The key to check. * @param array $values The legal values. * @param string $def_val The default value. * @param boolean $autoload Should the option autoload, default true. */ function relevanssi_update_legal_value( array $request, string $option, array $values, string $def_val, bool $autoload = true ) { if ( isset( $request[ $option ] ) ) { $value = $def_val; if ( in_array( $request[ $option ], $values, true ) ) { $value = $request[ $option ]; } update_option( $option, $value, $autoload ); } } /** * Sets an on/off option according to the request value. * * @param array $request An array of option values. * @param string $option The key to check. * @param boolean $autoload Should the option autoload, default true. */ function relevanssi_update_off_or_on( array $request, string $option, bool $autoload = true ) { relevanssi_update_legal_value( $request, $option, array( 'off', 'on' ), 'off', $autoload ); } /** * Sets an option after sanitizing and unslashing the value. * * @param array $request An array of option values. * @param string $option The key to check. * @param boolean $autoload Should the option autoload, default true. */ function relevanssi_update_sanitized( array $request, string $option, bool $autoload = true ) { if ( isset( $request[ $option ] ) ) { $value = sanitize_text_field( wp_unslash( $request[ $option ] ) ); update_option( $option, $value, $autoload ); } } /** * Returns true if $_SERVER['HTTP_USER_AGENT'] is on the bot block list. * * Looks for bot user agents in the $_SERVER['HTTP_USER_AGENT'] and returns true * if a match is found. * * @return bool True if $_SERVER['HTTP_USER_AGENT'] is a bot. */ function relevanssi_user_agent_is_bot(): bool { if ( isset( $_SERVER['HTTP_USER_AGENT'] ) ) { /** * Filters the bots Relevanssi should block from search queries. * * Lets you filter the bots that are blocked from Relevanssi search * queries. * * @param array $bots An array of bot user agents. */ $bots = apply_filters( 'relevanssi_bots_to_block', relevanssi_bot_block_list() ); foreach ( array_values( $bots ) as $lookfor ) { if ( false !== stristr( $_SERVER['HTTP_USER_AGENT'], $lookfor ) ) { return true; } } } return false; } /** * Validates that the parameter is a valid taxonomy type. * * @parameter string $taxonomy The taxonomy to validate. * * @return string The validated taxonomy, empty string if invalid. */ function relevanssi_validate_taxonomy( $taxonomy ) { $taxonomy = sanitize_text_field( $taxonomy ); if ( taxonomy_exists( $taxonomy ) ) { return $taxonomy; } return ''; }