Skip to content

Commit

Permalink
support replacements in output & rename argument to pattern_replace
Browse files Browse the repository at this point in the history
  • Loading branch information
aristath committed May 27, 2017
1 parent 2c9622a commit ec07952
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 3 deletions.
37 changes: 36 additions & 1 deletion modules/css/class-kirki-output.php
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,42 @@ protected function apply_value_pattern( $output, $value ) {

if ( isset( $output['value_pattern'] ) && ! empty( $output['value_pattern'] ) ) {
if ( is_string( $output['value_pattern'] ) ) {
return str_replace( '$', $value, $output['value_pattern'] );
$value = str_replace( '$', $value, $output['value_pattern'] );
if ( isset( $output['pattern_replace'] ) && is_array( $output['pattern_replace'] ) ) {
$option_type = 'theme_mod';
$option_name = false;
if ( isset( Kirki::$config[ $this->config_id ] ) ) {
$config = Kirki::$config[ $this->config_id ];
$option_type = ( isset( $config['option_type'] ) ) ? $config['option_type'] : 'theme_mod';
if ( 'option' === $option_type || 'site_option' === $option_type ) {
$option_name = ( isset( $config['option_name'] ) ) ? $config['option_name'] : false;
}
}
if ( $option_name ) {
$options = ( 'site_option' === $option_type ) ? get_site_option( $option_name ) : get_option( $option_name );
}
foreach ( $output['pattern_replace'] as $search => $replace ) {
$replacement = '';
switch ( $option_type ) {
case 'option':
$replacement = ( is_array( $options ) && isset( $options[ $replace ] ) ) ? $options[ $replace ] : get_option( $replace );
break;
case 'site_option':
$replacement = ( is_array( $options ) && isset( $options[ $replace ] ) ) ? $options[ $replace ] : get_site_option( $replace );
break;
case 'user_meta':
$user_id = get_current_user_id();
if ( $user_id ) {
$replacement = get_user_meta( $user_id, $replace, true );
}
break;
default:
$replacement = get_theme_mod( $replace );
}
$replacement = ( false === $replacement ) ? '' : $replacement;
$value = str_replace( $search, $replacement, $value );
}
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions modules/postmessage/class-kirki-modules-postmessage.php
Original file line number Diff line number Diff line change
Expand Up @@ -374,9 +374,9 @@ private function get_args( $args ) {
private function value_pattern_replacements( $value, $js_vars ) {
$script = '';
$alias = $value;
if ( isset( $js_vars['replacements'] ) ) {
if ( isset( $js_vars['pattern_replace'] ) ) {
$script .= 'settings=window.wp.customize.get();';
foreach ( $js_vars['replacements'] as $search => $replace ) {
foreach ( $js_vars['pattern_replace'] as $search => $replace ) {
$replace = '\'+settings["' . $replace . '"]+\'';
$value = str_replace( $search, $replace, $js_vars['value_pattern'] );
$value = trim( $value, '+' );
Expand Down

0 comments on commit ec07952

Please sign in to comment.