) { return $matches[0]; } } if ( ! is_callable( $cb ) ) { return $matches[0]; } $args = isset( $matches[2] ) ? ltrim( $matches[2], '|' ) : false; $config = isset( $matches[3] ) ? json_decode( $matches[3], true ) : false; // Store the initial configs $initial_fallback = $this->get_fallback(); $initial_context = $this->get_macros_context(); $initial_before = $this->get_before(); $initial_after = $this->get_after(); // Reset the configs except macros context. $this->set_fallback( null ); $this->set_before( null ); $this->set_after( null ); // Set the config of current macro if ( $config ) { if ( ! empty( $config['context'] ) ) { $this->set_macros_context( $config['context'] ); } if ( ! Jet_Engine_Tools::is_empty( $config, 'fallback' ) ) { $this->set_fallback( $config['fallback'] ); } if ( ! Jet_Engine_Tools::is_empty( $config, 'before' ) ) { $this->set_before( $config['before'] ); } if ( ! Jet_Engine_Tools::is_empty( $config, 'after' ) ) { $this->set_after( $config['after'] ); } } $result = call_user_func( $cb, $field_value, $args ); $fallback = $this->get_fallback(); $before = $this->get_before(); $after = $this->get_after(); $is_empty_result = empty( $result ); /* * If the result of macro is `not-found` or array( 'not-found' ) * and the fallback value is not empty, then the fallback value is returned. * * See: https://github.com/Crocoblock/issues-tracker/issues/3243 */ if ( ! $is_empty_result ) { $is_not_found_result = ( ( is_array( $result ) && in_array( 'not-found', $result ) ) || ( ! is_array( $result ) && 'not-found' === $result ) ); if ( $is_not_found_result && ! Jet_Engine_Tools::is_empty( $fallback ) ) { $is_empty_result = true; } } if ( ! $is_empty_result ) { if ( is_array( $result ) ) { $result = implode( ',', $result ); } if ( $before ) { $result = $before . $result; } if ( $after ) { $result .= $after; } } elseif ( ! Jet_Engine_Tools::is_empty( $fallback ) ) { $result = $fallback; } // Set the initial configs $this->set_fallback( $initial_fallback ); $this->set_macros_context( $initial_context ); $this->set_before( $initial_before ); $this->set_after( $initial_after ); return $result; }, $string ); } } }