Example #1
0
 /**
  * Clean up the input string.
  *
  * @param string $inputString              String to clean up.
  * @param bool   $removeOptionalCharacters Whether or not to do a cleanup of optional chars too.
  *
  * @return string
  */
 function strip_separators_and_fold($inputString, $removeOptionalCharacters = false)
 {
     $keywordCharactersAlwaysReplacedBySpace = array(',', "'", '"', '?', '’', '“', '”', '|', '/');
     $keywordCharactersRemovedOrReplaced = array('_', '-');
     $keywordWordsRemoved = array(' a ', ' in ', ' an ', ' on ', ' for ', ' the ', ' and ');
     // lower
     $inputString = $this->strtolower_utf8($inputString);
     // default characters replaced by space
     $inputString = str_replace($keywordCharactersAlwaysReplacedBySpace, ' ', $inputString);
     // standardise whitespace
     $inputString = wpseo_standardize_whitespace($inputString);
     // deal with the separators that can be either removed or replaced by space
     if ($removeOptionalCharacters) {
         // remove word separators with a space
         $inputString = str_replace($keywordWordsRemoved, ' ', $inputString);
         $inputString = str_replace($keywordCharactersRemovedOrReplaced, '', $inputString);
     } else {
         $inputString = str_replace($keywordCharactersRemovedOrReplaced, ' ', $inputString);
     }
     // standardise whitespace again
     $inputString = wpseo_standardize_whitespace($inputString);
     return trim($inputString);
 }
 /**
  * Replace `%%variable_placeholders%%` with their real value based on the current requested page/post/cpt/etc
  *
  * @param string $string the string to replace the variables in.
  * @param array  $args   the object some of the replacement values might come from,
  *                       could be a post, taxonomy or term.
  * @param array  $omit   variables that should not be replaced by this function.
  *
  * @return string
  */
 public function replace($string, $args, $omit = array())
 {
     $string = strip_tags($string);
     // Let's see if we can bail super early.
     if (strpos($string, '%%') === false) {
         return wpseo_standardize_whitespace($string);
     }
     $args = (array) $args;
     if (isset($args['post_content']) && !empty($args['post_content'])) {
         $args['post_content'] = wpseo_strip_shortcode($args['post_content']);
     }
     if (isset($args['post_excerpt']) && !empty($args['post_excerpt'])) {
         $args['post_excerpt'] = wpseo_strip_shortcode($args['post_excerpt']);
     }
     $this->args = (object) wp_parse_args($args, $this->defaults);
     // Clean $omit array
     if (is_array($omit) && $omit !== array()) {
         $omit = array_map(array(__CLASS__, 'remove_var_delimiter'), $omit);
     }
     $replacements = array();
     if (preg_match_all('`%%([^%]+(%%single)?)%%?`iu', $string, $matches)) {
         $replacements = $this->set_up_replacements($matches, $omit);
     }
     /**
      * Filter: 'wpseo_replacements' - Allow customization of the replacements before they are applied
      *
      * @api array $replacements The replacements
      */
     $replacements = apply_filters('wpseo_replacements', $replacements);
     // Do the actual replacements
     if (is_array($replacements) && $replacements !== array()) {
         $string = str_replace(array_keys($replacements), array_values($replacements), $string);
     }
     /**
      * Filter: 'wpseo_replacements_final' - Allow overruling of whether or not to remove placeholders
      * which didn't yield a replacement
      *
      * @example <code>add_filter( 'wpseo_replacements_final', '__return_false' );</code>
      *
      * @api     bool $final
      */
     if (apply_filters('wpseo_replacements_final', true) === true && (isset($matches[1]) && is_array($matches[1]))) {
         // Remove non-replaced variables
         $remove = array_diff($matches[1], $omit);
         // Make sure the $omit variables do not get removed
         $remove = array_map(array(__CLASS__, 'add_var_delimiter'), $remove);
         $string = str_replace($remove, '', $string);
     }
     // Undouble separators which have nothing between them, i.e. where a non-replaced variable was removed
     if (isset($replacements['%%sep%%']) && (is_string($replacements['%%sep%%']) && $replacements['%%sep%%'] !== '')) {
         $q_sep = preg_quote($replacements['%%sep%%'], '`');
         $string = preg_replace('`' . $q_sep . '(?:\\s*' . $q_sep . ')*`u', $replacements['%%sep%%'], $string);
     }
     // Remove superfluous whitespace
     $string = wpseo_standardize_whitespace($string);
     return trim($string);
 }