Advertisement
baliniz

ModuleViewAbstract.php

Jan 15th, 2018
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 20.30 KB | None | 0 0
  1. <?php
  2. /**
  3.  * @author : Jegtheme
  4.  */
  5. namespace JNews\Module;
  6.  
  7. abstract Class ModuleViewAbstract
  8. {
  9.     /**
  10.      * @var array
  11.      */
  12.     protected static $instance;
  13.  
  14.     /**
  15.      * Option Field
  16.      *
  17.      * @var array
  18.      */
  19.     protected $options;
  20.  
  21.     /**
  22.      * @var string
  23.      */
  24.     protected $unique_id;
  25.  
  26.     /**
  27.      * Array of attribute
  28.      *
  29.      * @var array
  30.      */
  31.     protected $attribute;
  32.  
  33.     /**
  34.      * @var ModuleManager
  35.      */
  36.     protected $manager;
  37.  
  38.     /**
  39.      * @var string
  40.      */
  41.     protected $class_name;
  42.  
  43.     /**
  44.      * @var ModuleOptionAbstract
  45.      */
  46.     protected $option_class;
  47.  
  48.     /**
  49.      * @var String
  50.      */
  51.     protected $content;
  52.  
  53.     /**
  54.      * @return ModuleViewAbstract
  55.      * @var $manager
  56.      */
  57.     public static function getInstance()
  58.     {
  59.         $class = get_called_class();
  60.  
  61.         if (!isset(self::$instance[$class]))
  62.         {
  63.             self::$instance[$class] = new $class();
  64.         }
  65.  
  66.         return self::$instance[$class];
  67.     }
  68.  
  69.     /**
  70.      * ModuleViewAbstract constructor.
  71.      */
  72.     protected function __construct()
  73.     {
  74.         $this->class_name = jnews_get_shortcode_name_from_view(get_class($this));
  75.         $this->manager = ModuleManager::getInstance();
  76.  
  77.         // set option class
  78.         $class_option = str_replace('_View', '_Option', get_class($this));
  79.         $this->option_class = call_user_func(array($class_option, 'getInstance'));
  80.  
  81.         $this->set_options();
  82.     }
  83.  
  84.     private function set_options()
  85.     {
  86.         $options = $this->option_class->get_options();
  87.  
  88.         foreach($options as $option)
  89.         {
  90.             $this->options[$option['param_name']] = isset($option['std']) ? $option['std'] : '';
  91.         }
  92.     }
  93.  
  94.     private function compatible_column()
  95.     {
  96.         return $this->option_class->compatible_column();
  97.     }
  98.  
  99.     public function color_scheme()
  100.     {
  101.         return $this->attribute['scheme'];
  102.     }
  103.  
  104.     public function get_vc_class_name()
  105.     {
  106.         $class_name = null;
  107.  
  108.         if(isset($this->attribute['css']))
  109.         {
  110.             $css_exploded = explode('{', $this->attribute['css']);
  111.             $class = $css_exploded[0];
  112.             $class_name = substr($class, 1);
  113.         }
  114.  
  115.         return $class_name;
  116.     }
  117.  
  118.     public function is_compatible_widget()
  119.     {
  120.         $column = $this->compatible_column();
  121.  
  122.         if(in_array(4, $column))
  123.         {
  124.             return true;
  125.         }
  126.  
  127.         return false;
  128.     }
  129.  
  130.     /**
  131.      * @param $attr
  132.      * @return string
  133.      */
  134.     public function get_module_column_class($attr)
  135.     {
  136.         if(isset($attr['column_width']) && $attr['column_width'] !== 'auto')
  137.         {
  138.             switch($attr['column_width']) {
  139.                 case 4 :
  140.                     $class_name = 'jeg_col_1o3';
  141.                     break;
  142.                 case 8 :
  143.                     $class_name = 'jeg_col_2o3';
  144.                     break;
  145.                 case 12 :
  146.                     $class_name = 'jeg_col_3o3';
  147.                     break;
  148.                 default :
  149.                     $class_name = 'jeg_col_3o3';
  150.             }
  151.  
  152.             return $class_name;
  153.         } else {
  154.             return $this->manager->get_column_class();
  155.         }
  156.     }
  157.  
  158.     /**
  159.      * Call from VC to build Module
  160.      *
  161.      * @param $attr
  162.      * @param $content
  163.      * @return string
  164.      */
  165.     public function build_module($attr, $content = null)
  166.     {
  167.         $this->content = $content;
  168.         $this->generate_unique_id();
  169.         $attr = $this->get_attribute($attr);
  170.  
  171.         $column_class = $this->get_module_column_class($attr);
  172.         $output = $this->render_module($attr, $column_class);
  173.  
  174.         if(!$this->is_column_compatible() && ( current_user_can('editor') || current_user_can('administrator') ))
  175.         {
  176.             $output = $output . $this->render_uncompatible();
  177.         }
  178.  
  179.         do_action($this->class_name);
  180.         return $output;
  181.     }
  182.  
  183.     /**
  184.      * Render if module is not compatible
  185.      *
  186.      * @return string
  187.      */
  188.     public function render_uncompatible()
  189.     {
  190.         $compatible = $this->compatible_column();
  191.         $column = $this->manager->get_current_width();
  192.         $text = wp_kses(sprintf(__('This module works best for column <strong>%s</strong> ( current column width <strong>%s</strong> ). This warning will only show if you login as Admin.', 'jnews'), implode(', ', $compatible), $column), wp_kses_allowed_html());
  193.         $element =
  194.             "<div class=\"alert alert-error alert-compatibility\">
  195.                <strong>" . jnews_return_translation('Optimal Column','jnews', 'optimal_column') . "</strong> {$text}
  196.            </div>";
  197.  
  198.         return $element;
  199.     }
  200.  
  201.     /**
  202.      * Check if column is not compatible
  203.      *
  204.      * @return bool
  205.      */
  206.     public function is_column_compatible()
  207.     {
  208.         $compatible = $this->compatible_column();
  209.         $column = $this->manager->get_current_width();
  210.  
  211.         if($column === null) return true;
  212.         return in_array($column, $compatible);
  213.     }
  214.  
  215.     /**
  216.      * @return int
  217.      */
  218.     public function get_post_id()
  219.     {
  220.         global $wp_query;
  221.         if(isset($wp_query->post)) {
  222.             return $wp_query->post->ID ;
  223.         }
  224.         return null;
  225.     }
  226.  
  227.     /**
  228.      * Generate Unique ID For Module
  229.      */
  230.     public function generate_unique_id()
  231.     {
  232.         $this->unique_id = 'jnews_module_' . $this->get_post_id() . '_' . $this->manager->get_module_count() . '_' . uniqid();
  233.         // need to increase module count
  234.         $this->manager->increase_module_count();
  235.     }
  236.  
  237.     /**
  238.      * Render Widget
  239.      *
  240.      * @param $instance
  241.      */
  242.     public function render_widget($instance)
  243.     {
  244.         if($this->is_compatible_widget())
  245.         {
  246.             echo jnews_sanitize_output($this->build_module($instance));
  247.         }
  248.     }
  249.  
  250.     /**
  251.      * Render VC shortcode
  252.      *
  253.      * @param $attr
  254.      * @param $content
  255.      * @return mixed
  256.      */
  257.     public function render_shortcode($attr, $content)
  258.     {
  259.         return $this->build_module($attr, $content);
  260.     }
  261.  
  262.     /**
  263.      * get thumbnail
  264.      * @param $post_id
  265.      * @param $size
  266.      * @return mixed|string
  267.      */
  268.     public function get_thumbnail($post_id, $size)
  269.     {
  270.         return apply_filters('jnews_image_thumbnail', $post_id, $size);
  271.     }
  272.  
  273.     /**
  274.      * Render primary category
  275.      *
  276.      * @param $post_id
  277.      * @return mixed|string
  278.      */
  279.     public function get_primary_category($post_id)
  280.     {
  281.         $cat_id = jnews_get_primary_category($post_id);
  282.         $category = '';
  283.  
  284.         if($cat_id) {
  285.             $category = get_category($cat_id);
  286.             $class = 'class="category-'. $category->slug .'"';
  287.             $category = "<a href=\"" . get_category_link($cat_id) . "\" {$class}>" . $category->name . "</a>";
  288.         }
  289.  
  290.         return $category;
  291.     }
  292.  
  293.     public function except_more()
  294.     {
  295.         return isset($this->attribute['excerpt_ellipsis']) ? $this->attribute['excerpt_ellipsis'] : ' ...';
  296.     }
  297.  
  298.     public function excerpt_length()
  299.     {
  300.         if(isset($this->attribute['excerpt_length']))
  301.         {
  302.             if ( isset( $this->attribute['excerpt_length']['size'] ) && ! empty( $this->attribute['excerpt_length']['size'] ) )
  303.             {
  304.                 return $this->attribute['excerpt_length']['size'];
  305.             }
  306.  
  307.             return $this->attribute['excerpt_length'];
  308.         } else {
  309.             return 20;
  310.         }
  311.     }
  312.  
  313.     public function format_date($post)
  314.     {
  315.         if(isset($this->attribute['date_format']))
  316.         {
  317.             $date_format = $this->attribute['date_format'];
  318.  
  319.             if($date_format === 'ago') {
  320.                 return jnews_ago_time ( human_time_diff( get_the_time('U', $post), current_time('timestamp') ) );
  321.             } else if ($date_format === 'custom') {
  322.                 return get_the_date($this->attribute['date_format_custom'], $post);
  323.             } else if ($date_format) {
  324.                 return get_the_date(null, $post);
  325.             }
  326.         }
  327.  
  328.         return get_the_date(null, $post);
  329.     }
  330.  
  331.     protected function get_excerpt($post)
  332.     {
  333.         $excerpt = $post->post_excerpt;
  334.  
  335.         if(empty($excerpt))
  336.         {
  337.             $excerpt = $post->post_content;
  338.         }
  339.  
  340.         $excerpt = wp_trim_words($excerpt, $this->excerpt_length(), $this->except_more());
  341.         $excerpt = preg_replace( '/\[[^\]]+\]/', '', $excerpt );
  342.  
  343.         return apply_filters('jnews_module_excerpt', $excerpt, $post->ID, $this->excerpt_length(), $this->except_more());
  344.     }
  345.  
  346.     protected function collect_post_id($content)
  347.     {
  348.         $post_ids = array();
  349.         foreach($content['result'] as $result) {
  350.             $post_ids[] = $result->ID;
  351.         }
  352.         return $post_ids;
  353.     }
  354.  
  355.     /**
  356.      * build query
  357.      *
  358.      * @param $attr
  359.      * @return array
  360.      */
  361.     protected function build_query($attr)
  362.     {
  363.         if(isset($attr['unique_content']) && $attr['unique_content'] !== 'disable')
  364.         {
  365.             if(!empty($attr['exclude_post'])) {
  366.                 $exclude_post = explode(',', $attr['exclude_post']);
  367.             } else {
  368.                 $exclude_post = array();
  369.             }
  370.  
  371.             $exclude_post = array_merge($this->manager->get_unique_article($attr['unique_content']), $exclude_post);
  372.             $attr['exclude_post'] = implode(',', $exclude_post);
  373.  
  374.             // we need to alter attribute here...
  375.             $this->set_attribute($attr);
  376.         }
  377.  
  378.         $result = ModuleQuery::do_query($attr);
  379.  
  380.         if(isset($attr['unique_content']) && $attr['unique_content'] !== 'disable')
  381.         {
  382.             $this->manager->add_unique_article($attr['unique_content'], $this->collect_post_id($result));
  383.         }
  384.  
  385.         if(isset($result['result']))
  386.         {
  387.             foreach($result['result'] as $post)
  388.             {
  389.                 do_action('jnews_json_archive_push', $post->ID);
  390.             }
  391.         }
  392.  
  393.         return $result;
  394.     }
  395.  
  396.     /**
  397.      * Post meta type 1
  398.      *
  399.      * @param $post
  400.      * @return string
  401.      */
  402.     public function post_meta_1($post)
  403.     {
  404.         $output = '';
  405.         $comment            = jnews_get_comments_number($post->ID);
  406.  
  407.         // author detail
  408.         $author             = $post->post_author;
  409.         $author_url         = get_author_posts_url($author);
  410.         $author_name        = get_the_author_meta('display_name', $author);
  411.  
  412.         if( jnews_is_review($post->ID) )
  413.         {
  414.             $rating = jnews_generate_rating($post->ID, 'jeg_landing_review');
  415.             $output .=
  416.                 "<div class=\"jeg_post_meta\">
  417.                    {$rating}
  418.                    <div class=\"jeg_meta_author\"><span class=\"by\">" . jnews_return_translation('by', 'jnews', 'by') . "</span> {$author_name}</div>
  419.                </div>";
  420.         } else {
  421.             $output .=
  422.                 "<div class=\"jeg_post_meta\">
  423.                    <div class=\"jeg_meta_author\"><span class=\"by\">" . jnews_return_translation('by', 'jnews', 'by') . "</span> {$author_name}</div>
  424.                    <div class=\"jeg_meta_date\"><a href=\"" . get_the_permalink($post) . "\"><i class=\"fa fa-clock-o\"></i> " . $this->format_date($post) . "</a></div>
  425.                    <div class=\"jeg_meta_comment\"><a href=\"" . jnews_get_respond_link($post->ID) . "\" ><i class=\"fa fa-comment-o\"></i> {$comment}</a></div>
  426.                </div>";
  427.         }
  428.  
  429.         return apply_filters('jnews_module_post_meta_1', $output, $post, self::getInstance());
  430.     }
  431.  
  432.     /**
  433.      * Post Meta Type 2
  434.      *
  435.      * @param $post
  436.      * @return string
  437.      */
  438.     public function post_meta_2($post)
  439.     {
  440.         $output = '';
  441.         if( jnews_is_review($post->ID) )
  442.         {
  443.             $output = jnews_generate_rating($post->ID, 'jeg_landing_review');
  444.         } else {
  445.  
  446.             $output .=
  447.                 "<div class=\"jeg_post_meta\">
  448.                    <div class=\"jeg_meta_date\"><a href=\"" . get_the_permalink($post) . "\" ><i class=\"fa fa-clock-o\"></i> " . $this->format_date($post) . "</a></div>
  449.                </div>";
  450.         }
  451.  
  452.  
  453.         return apply_filters('jnews_module_post_meta_2', $output, $post, self::getInstance());
  454.     }
  455.  
  456.     /**
  457.      * Post meta type 3
  458.      *
  459.      * @param $post
  460.      * @return string
  461.      */
  462.     public function post_meta_3($post)
  463.     {
  464.         $output = '';
  465.  
  466.         if(jnews_is_review($post->ID))
  467.         {
  468.             $rating = jnews_generate_rating($post->ID, 'jeg_landing_review');
  469.  
  470.             $output =
  471.                 "<div class=\"jeg_post_meta\">
  472.                    {$rating}
  473.                    <div class=\"jeg_meta_date\"><a href=\"" . get_the_permalink($post) . "\"><i class=\"fa fa-clock-o\"></i> " . $this->format_date($post) . "</a></div>                    
  474.                </div>";
  475.         } else {
  476.  
  477.             // author detail
  478.             $author             = $post->post_author;
  479.             $author_url         = get_author_posts_url($author);
  480.             $author_name        = get_the_author_meta('display_name', $author);
  481.  
  482.             $output .=
  483.                 "<div class=\"jeg_post_meta\">
  484.                    <div class=\"jeg_meta_author\"><span class=\"by\">" . jnews_return_translation('by', 'jnews', 'by') . "</span> {$author_name}</div>
  485.                    <div class=\"jeg_meta_date\"><a href=\"" . get_the_permalink($post) . "\"><i class=\"fa fa-clock-o\"></i> " . $this->format_date($post) . "</a></div>
  486.                </div>";
  487.  
  488.         }
  489.  
  490.         return apply_filters('jnews_module_post_meta_3', $output, $post, self::getInstance());
  491.     }
  492.  
  493.     /**
  494.      * Get attribute
  495.      *
  496.      * @param $attr
  497.      * @return array
  498.      */
  499.     public function get_attribute($attr)
  500.     {
  501.         $this->attribute = wp_parse_args( $attr , $this->options);
  502.         return $this->attribute;
  503.     }
  504.  
  505.     public function set_attribute($attr)
  506.     {
  507.         $this->attribute = $attr;
  508.     }
  509.  
  510.     /**
  511.      * Empty Content
  512.      *
  513.      * @return string
  514.      */
  515.     public function empty_content()
  516.     {
  517.         $no_content = "<div class='jeg_empty_module'>" . jnews_return_translation('No Content Available','jnews', 'no_content_available') . "</div>";
  518.         return apply_filters('jnews_module_no_content', $no_content);
  519.     }
  520.  
  521.     public function render_module_ads( $ajax_class = '' )
  522.     {
  523.         $attr     = $this->attribute;
  524.         $addclass = isset( $attr['ads_class'] ) ? 'jnews_' . $attr['ads_class'] . '_ads' : '';
  525.  
  526.         return "<div class='jeg_ad jeg_ad_module {$addclass} {$ajax_class}'>" . $this->build_module_ads( $attr ) . "</div>";
  527.     }
  528.  
  529.     public function build_module_ads( $attr, $echo = false )
  530.     {
  531.         $type       = $attr['ads_type'];
  532.         $addclass   = isset( $attr['ads_class'] ) ? $attr['ads_class'] : '';
  533.         $ads_html   = '';
  534.  
  535.         if ( $type === 'image' )
  536.         {
  537.             $ads_tab    = $attr['ads_image_new_tab'] ? '_blank' : '_self';
  538.             $ads_link   = $attr['ads_image_link'];
  539.             $ads_text   = $attr['ads_image_alt'];
  540.             $ads_image  = $attr['ads_image'];
  541.  
  542.             if ( filter_var($ads_image, FILTER_VALIDATE_URL) === FALSE )
  543.             {
  544.                 if ( isset( $ads_image['url'] ) && ! empty( $ads_image['url'] ) )
  545.                 {
  546.                     $ads_image = $ads_image['url'];
  547.                 } else {
  548.                     $ads_image  = wp_get_attachment_image_src($ads_image, 'full')[0];
  549.                 }
  550.             }
  551.  
  552.             if ( ! empty( $ads_image ) )
  553.             {
  554.                 $ads_html = "<a href='{$ads_link}' target='{$ads_tab}' class='adlink {$addclass}'><img src='{$ads_image}' alt='{$ads_text}' data-pin-no-hover=\"true\"></a>";
  555.             }
  556.         }
  557.  
  558.         if ( $type === 'shortcode' )
  559.         {
  560.             $shortcode  = $attr['shortcode'];
  561.             $ads_html   = "<div class='{$addclass}'>" . do_shortcode($shortcode) . "</div>";
  562.         }
  563.  
  564.         if ( $type === 'code' )
  565.         {
  566.             $code       = $attr['code'];
  567.             $ads_html   = "<div class='{$addclass}'>" . $code . "</div>";
  568.         }
  569.  
  570.         if ( $type === 'googleads' )
  571.         {
  572.             $publisherid = $attr['google_publisher_id'];
  573.             $slotid      = $attr['google_slot_id'];
  574.  
  575.             if ( ! empty($publisherid) && ! empty($slotid) )
  576.             {
  577.                 $column = $this->manager->get_current_width();
  578.  
  579.                 if ( $column >= 8 )
  580.                 {
  581.                     $desktopsize_ad = array('728','90');
  582.                     $tabsize_ad     = array('468','60');
  583.                     $phonesize_ad   = array('320', '50');
  584.                 } else {
  585.                     $desktopsize_ad = array('300','250');
  586.                     $tabsize_ad     = array('300','250');
  587.                     $phonesize_ad   = array('300','250');
  588.                 }
  589.  
  590.                 $desktopsize    = $attr['google_desktop'];
  591.                 $tabsize        = $attr['google_tab'];
  592.                 $phonesize      = $attr['google_phone'];
  593.  
  594.                 if ( $desktopsize !== 'auto' )
  595.                 {
  596.                     $desktopsize_ad = explode('x', $desktopsize);
  597.                 }
  598.                 if ( $tabsize !== 'auto' )
  599.                 {
  600.                     $tabsize_ad = explode('x', $tabsize);
  601.                 }
  602.                 if ( $phonesize !== 'auto' )
  603.                 {
  604.                     $phonesize_ad = explode('x', $phonesize);
  605.                 }
  606.  
  607.                 $randomstring = $this->random_string();
  608.                 $ad_style = '';
  609.  
  610.                 if ( $desktopsize !== 'hide' && is_array($desktopsize_ad) && isset($desktopsize_ad['0']) && isset($desktopsize_ad['1']) )
  611.                 {
  612.                     $ad_style .= ".adsslot_{$randomstring}{ width:{$desktopsize_ad[0]}px !important; height:{$desktopsize_ad[1]}px !important; }\n";
  613.                 }
  614.                 if ( $tabsize !== 'hide' && is_array($tabsize_ad) && isset($tabsize_ad['0']) && isset($tabsize_ad['1']) )
  615.                 {
  616.                     $ad_style .= "@media (max-width:1199px) { .adsslot_{$randomstring}{ width:{$tabsize_ad[0]}px !important; height:{$tabsize_ad[1]}px !important; } }\n";
  617.                 }
  618.                 if ( $phonesize !== 'hide' && is_array($phonesize_ad) && isset($phonesize_ad['0']) && isset($phonesize_ad['1']) )
  619.                 {
  620.                     $ad_style .= "@media (max-width:767px) { .adsslot_{$randomstring}{ width:{$phonesize_ad[0]}px !important; height:{$phonesize_ad[1]}px !important; } }\n";
  621.                 }
  622.  
  623.                 $ads_html .=
  624.                     "<div class=\"{$addclass}\">
  625.                        <style type='text/css' scoped>
  626.                            {$ad_style}
  627.                        </style>
  628.                        <ins class=\"adsbygoogle adsslot_{$randomstring}\" style=\"display:inline-block;\" data-ad-client=\"{$publisherid}\" data-ad-slot=\"{$slotid}\"></ins>
  629.                        <script async src='//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js'></script>
  630.                        <script>(adsbygoogle = window.adsbygoogle || []).push({});</script>
  631.                    </div>";
  632.             }
  633.         }
  634.  
  635.         if ( $echo )
  636.         {
  637.             echo $ads_html;
  638.         } else {
  639.             return $ads_html;
  640.         }
  641.     }
  642.  
  643.     protected function random_string( $length = 10 )
  644.     {
  645.         $characters         = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
  646.         $charactersLength   = strlen($characters);
  647.         $randomString       = '';
  648.        
  649.         for ($i = 0; $i < $length; $i++)
  650.         {
  651.             $randomString .= $characters[rand(0, $charactersLength - 1)];
  652.         }
  653.  
  654.         return $randomString;
  655.     }
  656.  
  657.     protected function random_ads_position( $count )
  658.     {
  659.         $position = -1;
  660.         $attr     = $this->attribute;
  661.  
  662.         if ( $attr['ads_type'] !== 'disable' )
  663.         {
  664.             if ( $attr['ads_random'] )
  665.             {
  666.                 $position = rand( $attr['ads_position'], ($count - 2) );
  667.             } else {
  668.                 $position = $attr['ads_position'];
  669.             }
  670.         }
  671.  
  672.         return (int) $position;
  673.     }
  674.  
  675.     public function _content_template() {}
  676.  
  677.     abstract public function render_module($attr, $column_class);
  678. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement