Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * [usermeta_if] shortcode.
- *
- * @since 3.36.5
- *
- * @param array $atts Shortcode atts.
- * @param string $content The content to display if the condition matches.
- * @return string The content.
- */
- public function shortcode_user_meta_if( $atts, $content = null ) {
- $atts = shortcode_atts(
- array(
- 'field' => '',
- 'field_format' => '', // format for the value from meta
- 'value' => '',
- 'value_format' => 'strval', // format for the value we enter
- 'compare' => '=',
- ), $atts, 'user_meta_if'
- );
- // Check for curly quotes
- foreach ( $atts as $att ) {
- if ( false !== strpos( $att, '“' ) ) {
- return '<pre>' . __( '<strong>Oops!</strong> Curly quotes were found in a shortcode parameter of the [usermeta_if] shortcode. Curly quotes do not work with shortcode attributes.', 'wp-fusion' ) . '</pre>';
- }
- }
- $user_id = get_current_user_id();
- if ( ! $user_id ) {
- return '';
- }
- if ( ! $atts['field'] || ! $atts['value'] ) {
- return '';
- }
- $user_meta = wp_fusion()->user->get_user_meta( $user_id );
- if ( isset( $user_meta[ $atts['field'] ] ) ) {
- $meta_value = $user_meta[ $atts['field'] ];
- } else {
- $meta_value = '';
- }
- $meta_value = $atts['field_format'] ? call_user_func( $atts['field_format'], $meta_value ) : $meta_value;
- $value = $atts['value_format'] ? call_user_func( $atts['value_format'], $atts['value'] ) : $atts['value'];
- if ( 'strtotime' == $atts['field_format'] && false === $meta_value ) {
- return sprintf( __( '<strong>Oops!</strong> Your input string to the <code>%s</code> attribute was not successfully <a href="https://www.php.net/manual/en/function.strtotime.php" target="_blank">parsed by <code>strtotime()</code></a>.', 'wp-fusion' ), 'userfield' );
- } elseif ( 'strtotime' == $atts['value_format'] && false === $value ) {
- return sprintf( __( '<strong>Oops!</strong> Your input string to the <code>%s</code> attribute was not successfully <a href="https://www.php.net/manual/en/function.strtotime.php" target="_blank">parsed by <code>strtotime()</code></a>.', 'wp-fusion' ), 'value' );
- }
- $atts['compare'] = wp_specialchars_decode( $atts['compare'] );
- $show_content = false;
- switch ( $atts['compare'] ) {
- case '<':
- $show_content = $meta_value < $value;
- break;
- case '<=':
- $show_content = $meta_value <= $value;
- break;
- case '>':
- $show_content = $meta_value > $value;
- break;
- case '>=':
- $show_content = $meta_value >= $value;
- break;
- default:
- $show_content = $meta_value === $value;
- break;
- }
- if ( ! $show_content ) {
- return '';
- }
- return do_shortcode( $content );
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement