Example #1
0
 /**
  * replace all metas with corresponding text
  */
 public function replace_all_meta_in_text($post_id, $text)
 {
     if (trim($text) === '' || intval($post_id) === 0) {
         return '';
     }
     $base = new Essential_Grid_Base();
     $meta_link = new Essential_Grid_Meta_Linking();
     $cmeta = $this->get_all_meta();
     //process meta tags:
     $arr_matches = array();
     preg_match_all("/%[^%]*%/", $text, $arr_matches);
     if (!empty($arr_matches)) {
         $my_post = get_post($post_id, ARRAY_A);
         foreach ($arr_matches as $matches) {
             if (is_array($matches)) {
                 foreach ($matches as $match) {
                     $meta = trim(str_replace('%', '', $match));
                     $meta_value = get_post_meta($post_id, $meta, true);
                     if (!empty($cmeta)) {
                         foreach ($cmeta as $me) {
                             if ('eg-' . $me['handle'] == $meta) {
                                 if ($me['type'] == 'image') {
                                     if (intval($meta_value) > 0) {
                                         //get URL to Image
                                         $img = wp_get_attachment_image_src($meta_value, 'full');
                                         if ($img !== false) {
                                             $meta_value = $img[0];
                                         } else {
                                             $meta_value = '';
                                         }
                                     } else {
                                         $meta_value = '';
                                     }
                                 }
                                 if ($meta_value == '' && isset($me['default'])) {
                                     $meta_value = $me['default'];
                                 }
                                 break;
                             }
                         }
                     }
                     //check woocommerce
                     if (Essential_Grid_Woocommerce::is_woo_exists()) {
                         $wc_text = Essential_Grid_Woocommerce::get_value_by_meta($post_id, $meta);
                         if ($wc_text !== '') {
                             $meta_value = $wc_text;
                         }
                     }
                     if (empty($meta_value) && !empty($my_post)) {
                         //try to get from post
                         switch ($meta) {
                             //Post elements
                             case 'post_url':
                                 $post_id = $base->getVar($my_post, 'ID', '');
                                 $meta_value = get_permalink($post_id);
                                 break;
                             case 'post_id':
                                 $meta_value = $base->getVar($my_post, 'ID', '');
                                 break;
                             case 'title':
                                 $meta_value = $base->getVar($my_post, 'post_title', '');
                                 break;
                             case 'excerpt':
                                 $meta_value = trim($base->getVar($my_post, 'post_excerpt'));
                                 if (empty($meta_value)) {
                                     $meta_value = trim($base->getVar($my_post, 'post_content'));
                                 }
                                 $meta_value = strip_tags($meta_value);
                                 //,"<b><br><br/><i><strong><small>"
                                 break;
                             case 'meta':
                                 $m = new Essential_Grid_Meta();
                                 $meta_value = $m->get_meta_value_by_handle($my_post['ID'], $meta);
                                 break;
                             case 'alias':
                                 $meta_value = $base->getVar($my_post, 'post_name');
                                 break;
                             case 'content':
                                 $meta_value = $base->getVar($my_post, 'post_content');
                                 break;
                             case 'link':
                                 $meta_value = get_permalink($my_post['ID']);
                                 break;
                             case 'date':
                                 $postDate = $base->getVar($my_post, "post_date_gmt");
                                 $meta_value = $base->convert_post_date($postDate);
                                 break;
                             case 'date_modified':
                                 $dateModified = $base->getVar($my_post, "post_modified");
                                 $meta_value = $base->convert_post_date($dateModified);
                                 break;
                             case 'author_name':
                                 $authorID = $base->getVar($my_post, 'post_author');
                                 $meta_value = get_the_author_meta('display_name', $authorID);
                                 break;
                             case 'num_comments':
                                 $meta_value = $base->getVar($my_post, 'comment_count');
                                 break;
                             case 'cat_list':
                                 $use_taxonomies = false;
                                 $postCatsIDs = $base->getVar($my_post, 'post_category');
                                 if (empty($postCatsIDs) && isset($my_post['post_type'])) {
                                     $postCatsIDs = array();
                                     $obj = get_object_taxonomies($my_post['post_type']);
                                     if (!empty($obj) && is_array($obj)) {
                                         foreach ($obj as $tax) {
                                             $use_taxonomies[] = $tax;
                                             $new_terms = get_the_terms($my_post['ID'], $tax);
                                             if (is_array($new_terms) && !empty($new_terms)) {
                                                 foreach ($new_terms as $term) {
                                                     $postCatsIDs[$term->term_id] = $term->term_id;
                                                 }
                                             }
                                         }
                                     }
                                 }
                                 $meta_value = $base->get_categories_html_list($postCatsIDs, true, ',', $use_taxonomies);
                                 break;
                             case 'tag_list':
                                 $meta_value = $base->get_tags_html_list($my_post['ID']);
                                 break;
                             default:
                                 $meta_value = apply_filters('essgrid_post_meta_content', $meta_value, $meta, $my_post['ID'], $my_post);
                                 break;
                                 break;
                         }
                         if (empty($meta_value)) {
                             //check if its linking
                             $meta_value = $meta_link->get_link_meta_value_by_handle($my_post['ID'], $meta);
                         }
                     }
                     $text = str_replace($match, $meta_value, $text);
                 }
             }
         }
     }
     return $text;
 }
 /**
  * Retrieve the value of woocommerce elements
  */
 public function get_woocommerce_value($meta, $separator)
 {
     $text = '';
     if (isset($this->post['ID'])) {
         if (Essential_Grid_Woocommerce::is_woo_exists()) {
             $base = new Essential_Grid_Base();
             $m = new Essential_Grid_Meta();
             $text = Essential_Grid_Woocommerce::get_value_by_meta($this->post['ID'], $meta, $separator);
         }
     }
     return $text;
 }