Example #1
0
 static function get_the_field($parameters, $id = null)
 {
     $result = '';
     $field = $parameters['field'];
     if (!empty($parameters['id'])) {
         $id = $parameters['id'];
     }
     /*---------------------------------------------
      *
      * Option field
      *
      */
     if (!empty($parameters['option'])) {
         return self::get_option_field($field, $parameters['option']);
     }
     /*---------------------------------------------
      *
      * Attachment field
      *
      */
     if (!empty($parameters['type']) && $parameters['type'] == 'attachment' || CCS_Loop::$state['is_attachment_loop'] || CCS_Attached::$state['is_attachment_loop']) {
         return self::get_the_attachment_field($parameters);
         /*---------------------------------------------
          *
          * Array field
          *
          */
     } elseif (self::$state['is_array_field']) {
         $array = self::$state['current_field_value'];
         if (isset($array[$field])) {
             return $array[$field];
         } elseif ($field == 'value') {
             if (is_array($array)) {
                 $array = implode('', $array);
             }
             return $array;
         }
         // ACF gallery loop
     } elseif (class_exists('CCS_To_ACF') && CCS_To_ACF::$state['is_gallery_loop']) {
         return CCS_To_ACF::get_image_details_from_acf_gallery($parameters);
         /*---------------------------------------------
          *
          * Repeater or flexible content loop
          *
          */
     } elseif (empty($parameters['id']) && empty($parameters['name']) && class_exists('CCS_To_ACF') && CCS_To_ACF::$state['is_repeater_or_flex_loop']) {
         // If not inside relationship loop
         if (!CCS_To_ACF::$state['is_relationship_loop']) {
             // Get sub field
             if (function_exists('get_sub_field')) {
                 return get_sub_field($field);
             } else {
                 return null;
             }
         }
         /*---------------------------------------------
          *
          * Menu loop
          *
          */
     } elseif (CCS_Menu::$state['is_menu_loop']) {
         if (isset(CCS_Menu::$state['current_menu_object'][$field])) {
             return CCS_Menu::$state['current_menu_object'][$field];
         } else {
             if (!isset(CCS_Menu::$state['current_menu_object']['id'])) {
                 return;
             }
             // Get it from the post
             $id = CCS_Menu::$state['current_menu_object']['id'];
             CCS_Menu::$state['is_menu_loop'] = false;
             $result = do_shortcode('[field ' . $field . ' id=' . $id . ']');
             CCS_Menu::$state['is_menu_loop'] = true;
             return $result;
         }
     }
     if (!empty($id)) {
         $post_id = $id;
     } else {
         // Default
         $post_id = get_the_ID();
         /*      global $post;
               if (!empty($post)) {
                 $post_id = $post->ID;
               } */
     }
     if (empty($post_id)) {
         return null;
     }
     // No post ID
     $post = get_post($post_id);
     if (empty($post)) {
         return null;
     }
     // No post
     /*---------------------------------------------
      *
      * Prepare image attributes
      *
      */
     $image_fields = array('image', 'image-full', 'image-link', 'image-link-self', 'thumbnail', 'thumbnail-link', 'thumbnail-link-self', 'gallery');
     if ($field == 'thumbnail' && empty($parameters['size'])) {
         $parameters['size'] = 'thumbnail';
         // Default thumbnail
     }
     $attr = array();
     if (in_array($field, $image_fields)) {
         if (!empty($parameters['width']) || !empty($parameters['height'])) {
             $parameters['size'] = array((int) $parameters['width'], (int) $parameters['height']);
         }
         if (!empty($parameters['image_class'])) {
             $attr['class'] = $parameters['image_class'];
         }
         if (!empty($parameters['nopin'])) {
             $attr['nopin'] = $parameters['nopin'];
         }
         if (!empty($parameters['alt'])) {
             $attr['alt'] = $parameters['alt'];
         }
         //      if (!empty($parameters['title']))
         //        $attr['title'] = $parameters['title'];
     }
     // Custom field only?
     if (!empty($parameters['custom']) && $parameters['custom'] == 'true') {
         $custom = $field;
         $field = 'custom';
         // Skip predefined fields
     }
     /*---------------------------------------------
      *
      * Pre-defined fields
      *
      */
     switch ($field) {
         case 'id':
             $result = $post_id;
             break;
         case 'url':
             $result = get_permalink($post_id);
             break;
         case 'edit-url':
             $result = get_edit_post_link($post_id);
             break;
         case 'edit-link':
             $result = $post->post_title;
             break;
         case 'edit-link-self':
             $result = $post->post_title;
             break;
         case 'slug':
             $result = $post->post_name;
             break;
         case 'post-type':
             $result = $post->post_type;
             break;
         case 'post-type-name':
             $post_type = $post->post_type;
             $obj = get_post_type_object($post_type);
             $result = $obj->labels->singular_name;
             break;
         case 'post-type-plural':
             $post_type = $post->post_type;
             $obj = get_post_type_object($post_type);
             $result = $obj->labels->name;
             break;
         case 'post-status':
             $result = $post->post_status;
             if ($parameters['out'] !== 'slug') {
                 $result = ucwords($result);
             }
             break;
         case 'post-format':
             if (function_exists('get_post_format')) {
                 $result = get_post_format($post_id);
             }
             break;
         case 'post-format-name':
             if (function_exists('get_post_format')) {
                 $result = get_post_format($post_id);
                 $result = ucwords($result);
             }
             break;
         case 'post-format-link':
             if (function_exists('get_post_format')) {
                 $result = get_post_format($post_id);
                 $result = '<a href="' . get_post_format_link($result) . '">' . ucwords($result) . '</a>';
             }
             break;
         case 'post-format-url':
             if (function_exists('get_post_format')) {
                 $result = get_post_format_link(get_post_format($post_id));
             }
             break;
         case 'parent-id':
             $parent_id = isset($post->post_parent) ? $post->post_parent : 0;
             if (!empty($parent_id)) {
                 $result = $parent_id;
             }
             break;
         case 'parent-slug':
             $parent_id = isset($post->post_parent) ? $post->post_parent : 0;
             if (!empty($parent_id)) {
                 $post_data = get_post($parent_id);
                 if (!empty($post_data)) {
                     $result = isset($post_data->post_name) ? $post_data->post_name : '';
                 }
             }
             break;
         case 'link':
         case 'title-link':
         case 'title-link-out':
         case 'title':
             $result = $post->post_title;
             break;
         case 'author':
             $author_id = $post->post_author;
             $user = get_user_by('id', $author_id);
             if (!empty($parameters['meta'])) {
                 $result = get_the_author_meta($parameters['meta'], $author_id);
             } elseif (!empty($user)) {
                 $result = $user->display_name;
             }
             break;
         case 'author-id':
             $result = $post->post_author;
             break;
         case 'author-url':
             $result = get_author_posts_url($post->post_author);
             break;
         case 'author-login':
             $author_id = $post->post_author;
             $result = do_shortcode('[users id=' . $author_id . '][user name][/users]');
             break;
         case 'avatar':
             if (!empty($parameters['size'])) {
                 $result = get_avatar($post->post_author, $parameters['size']);
             } else {
                 $result = get_avatar($post->post_author);
             }
             break;
         case 'date':
             if (!empty($parameters['date_format'])) {
                 if ($parameters['date_format'] == 'relative') {
                     $result = CCS_Format::get_relative_date($post->post_date);
                 } else {
                     $result = mysql2date($parameters['date_format'], $post->post_date);
                 }
             } else {
                 // Default date format under Settings -> General
                 $result = mysql2date(get_option('date_format'), $post->post_date);
             }
             break;
         case 'modified':
             if (!empty($parameters['date_format'])) {
                 $result = get_post_modified_time($parameters['date_format'], $gmt = false, $post_id, $translate = true);
             } else {
                 // Default date format under Settings -> General
                 $result = get_post_modified_time(get_option('date_format'), $gmt = false, $post_id, $translate = true);
             }
             break;
         case 'image-full':
             $parameters['size'] = 'full';
         case 'image':
             // image
         // image
         case 'image-link':
             // image with link to post
         // image with link to post
         case 'image-link-self':
             // image with link to attachment page
             $parameters['size'] = isset($parameters['size']) && !empty($parameters['size']) ? $parameters['size'] : 'full';
             if (empty($attr['alt'])) {
                 $attr['alt'] = self::wp_get_featured_image_field($post_id, 'alt');
             }
             $result = get_the_post_thumbnail($post_id, $parameters['size'], $attr);
             break;
         case 'image-url':
             $parameters['size'] = isset($parameters['size']) && !empty($parameters['size']) ? $parameters['size'] : 'full';
             $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), $parameters['size']);
             $result = $src['0'];
             // $result = wp_get_attachment_url(get_post_thumbnail_id($post_id));
             break;
         case 'image-title':
         case 'image-caption':
         case 'image-alt':
         case 'image-description':
             $image_field_name = substr($field, 6);
             // Remove "image-"
             $result = self::wp_get_featured_image_field($post_id, $image_field_name);
             break;
         case 'thumbnail':
             // thumbnail
         // thumbnail
         case 'thumbnail-link':
             // thumbnail with link to post
         // thumbnail with link to post
         case 'thumbnail-link-self':
             // thumbnail with link to attachment page
             $parameters['size'] = isset($parameters['size']) && !empty($parameters['size']) ? $parameters['size'] : 'thumbnail';
             if (empty($attr['alt'])) {
                 $attr['alt'] = self::wp_get_featured_image_field($post_id, 'alt');
             }
             $result = get_the_post_thumbnail($post_id, $parameters['size'], $attr);
             break;
         case 'thumbnail-url':
             $src = wp_get_attachment_image_src(get_post_thumbnail_id($post_id), 'thumbnail');
             $result = $src['0'];
             break;
         case 'tags':
             $result = implode(' ', wp_get_post_tags($post_id, array('fields' => 'names')));
             break;
         case 'gallery':
             // Get specific image from gallery field
             if (class_exists('CCS_Gallery_Field')) {
                 // Check if gallery field is enabled
                 $attachment_ids = CCS_Gallery_Field::get_image_ids($post_id);
                 if (empty($parameters['num'])) {
                     $parameters['num'] = 1;
                 }
                 if (empty($parameters['size'])) {
                     $parameters['size'] = 'full';
                 }
                 $result = wp_get_attachment_image($attachment_ids[$parameters['num'] - 1], $parameters['size'], $icon = false, $attr);
             }
             break;
         case 'excerpt':
             // Get excerpt
             //        $result = get_the_excerpt();
             $result = $post->post_excerpt;
             if (empty($result)) {
                 // If empty, get it from post content
                 $result = $post->post_content;
                 if (empty($parameters['words']) && empty($parameters['length'])) {
                     self::$parameters['words'] = 25;
                 }
             }
             break;
         case 'debug':
             ob_start();
             echo '<pre>';
             print_r(get_post_custom($post_id));
             echo '</pre>';
             if (function_exists('acf_get_fields_by_id')) {
                 echo '<pre>';
                 print_r(acf_get_fields_by_id($post_id));
                 echo '</pre>';
             }
             $result = ob_get_clean();
             break;
         case 'loop-count':
             $result = CCS_Loop::$state['loop_count'];
             break;
         default:
             /*---------------------------------------------
              *
              * Custom field
              *
              */
             if (!empty($parameters['custom']) && $parameters['custom'] == 'true') {
                 $field = $custom;
             }
             $result = get_post_meta($post_id, $field, true);
             if (is_numeric($result) && !empty($parameters['return'])) {
                 if ($parameters['return'] == 'page-link') {
                     // ACF page link: get URL from post ID
                     $result = get_permalink($result);
                 } else {
                     // Get attachment field
                     $parameters['id'] = $result;
                     $parameters['field'] = $parameters['return'];
                     $result = self::get_the_attachment_field($parameters);
                 }
             } elseif (!empty($parameters['property']) && is_object($result)) {
                 $result = self::get_object_property($result, $parameters['property']);
             } elseif (!empty($parameters['currency']) || !empty($parameters['decimals']) || !empty($parameters['point']) || !empty($parameters['thousands'])) {
                 $currency = !empty($parameters['currency']) ? $parameters['currency'] : '';
                 $decimals = !empty($parameters['decimals']) ? $parameters['decimals'] : 2;
                 $point = !empty($parameters['point']) ? $parameters['point'] : '.';
                 $thousands = !empty($parameters['thousands']) ? $parameters['thousands'] : ',';
                 $result = CCS_Format::getCurrency($result, $currency, $decimals, $point, $thousands);
             }
             break;
     }
     return $result;
 }
Example #2
0
 static function format_number($atts, $content)
 {
     $currency = !empty($atts['currency']) ? $atts['currency'] : '';
     $decimals = !empty($atts['decimals']) ? $atts['decimals'] : 0;
     $point = !empty($atts['point']) ? $atts['point'] : '.';
     $thousands = !empty($atts['thousands']) ? $thousands === 'false' ? '' : $atts['thousands'] : '';
     $content = CCS_Format::getCurrency(floatval($content), $currency, $decimals, $point, $thousands);
     return $content;
 }