Advertisement
verygoodplugins

Untitled

Jan 26th, 2021
946
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.71 KB | None | 0 0
  1.  
  2.     /**
  3.      * [usermeta_if] shortcode.
  4.      *
  5.      * @since  3.36.5
  6.      *
  7.      * @param  array  $atts    Shortcode atts.
  8.      * @param  string $content The content to display if the condition matches.
  9.      * @return string The content.
  10.      */
  11.     public function shortcode_user_meta_if( $atts, $content = null ) {
  12.  
  13.         $atts = shortcode_atts(
  14.             array(
  15.                 'field'        => '',
  16.                 'field_format' => '', // format for the value from meta
  17.                 'value'        => '',
  18.                 'value_format' => 'strval', // format for the value we enter
  19.                 'compare'      => '=',
  20.             ), $atts, 'user_meta_if'
  21.         );
  22.  
  23.         // Check for curly quotes
  24.  
  25.         foreach ( $atts as $att ) {
  26.  
  27.             if ( false !== strpos( $att, '“' ) ) {
  28.                 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>';
  29.             }
  30.         }
  31.  
  32.         $user_id = get_current_user_id();
  33.  
  34.         if ( ! $user_id ) {
  35.             return '';
  36.         }
  37.  
  38.         if ( ! $atts['field'] || ! $atts['value'] ) {
  39.             return '';
  40.         }
  41.  
  42.         $user_meta = wp_fusion()->user->get_user_meta( $user_id );
  43.  
  44.         if ( isset( $user_meta[ $atts['field'] ] ) ) {
  45.             $meta_value = $user_meta[ $atts['field'] ];
  46.         } else {
  47.             $meta_value = '';
  48.         }
  49.  
  50.         $meta_value = $atts['field_format'] ? call_user_func( $atts['field_format'], $meta_value ) : $meta_value;
  51.         $value      = $atts['value_format'] ? call_user_func( $atts['value_format'], $atts['value'] ) : $atts['value'];
  52.  
  53.         if ( 'strtotime' == $atts['field_format'] && false === $meta_value ) {
  54.             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' );
  55.         } elseif ( 'strtotime' == $atts['value_format'] && false === $value ) {
  56.             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' );
  57.         }
  58.  
  59.         $atts['compare'] = wp_specialchars_decode( $atts['compare'] );
  60.  
  61.         $show_content = false;
  62.         switch ( $atts['compare'] ) {
  63.             case '<':
  64.                 $show_content = $meta_value < $value;
  65.                 break;
  66.             case '<=':
  67.                 $show_content = $meta_value <= $value;
  68.                 break;
  69.             case '>':
  70.                 $show_content = $meta_value > $value;
  71.                 break;
  72.             case '>=':
  73.                 $show_content = $meta_value >= $value;
  74.                 break;
  75.             default:
  76.                 $show_content = $meta_value === $value;
  77.                 break;
  78.         }
  79.  
  80.         if ( ! $show_content ) {
  81.             return '';
  82.         }
  83.  
  84.         return do_shortcode( $content );
  85.  
  86.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement