/**
  * Front-end display of widget.
  *
  * @see WP_Widget::widget()
  *
  * @param array $args
  *        	Widget arguments.
  * @param array $instance
  *        	Saved values from database.
  * @since 1.0.0
  * @author Panagiotis Vagenas <*****@*****.**>
  */
 public function widget($args, $instance)
 {
     global $post;
     // get instance of main plugin
     $plugin = easyRelatedPostsPRO::get_instance();
     // check if it's time to take action
     if (!is_single($post->ID)) {
         return;
     }
     if ($plugin->isInExcludedPostTypes($post) || $plugin->isInExcludedTaxonomies($post)) {
         return;
     }
     // Fill missing options
     if (empty($instance)) {
         $instance = erpPRODefaults::$comOpts + erpPRODefaults::$widOpts;
     } else {
         $instance = $instance + erpPRODefaults::$comOpts + erpPRODefaults::$widOpts;
     }
     erpPROPaths::requireOnce(erpPROPaths::$erpProRelated);
     erpPROPaths::requireOnce(erpPROPaths::$erpPROMainOpts);
     erpPROPaths::requireOnce(erpPROPaths::$erpPROWidOpts);
     $mainOpts = new erpPROMainOpts();
     $instance['tags'] = $mainOpts->getTags();
     $instance['categories'] = $mainOpts->getCategories();
     $instance['postTypes'] = $mainOpts->getPostTypes();
     $widOpts = new erpPROWidOpts($instance);
     // Get related
     $relatedObj = erpProRelated::get_instance($widOpts);
     $wpQ = $relatedObj->getRelated($post->ID);
     // If we have some posts to show
     if (!$wpQ->have_posts()) {
         $this->displayEmptyWidget($args, $instance);
     }
     erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
     VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$widgetThemesFolder), $instance['dsplLayout']);
     $theme = VPluginThemeFactory::getThemeByName($instance['dsplLayout'], 'widget');
     if (!$theme) {
         return $this->displayEmptyWidget($args, $instance);
     }
     $theme->setOptions($instance);
     $theme->formPostData($wpQ, $widOpts, $relatedObj->getRatingsFromRelDataObj());
     $content = $theme->renderW($this->number);
     // display rel content
     echo $args['before_widget'];
     echo $args['before_title'] . $instance['title'] . $args['after_title'];
     echo $content;
     echo $args['after_widget'];
 }
 /**
  * Checks if current post belongs in an excluded taxonimies
  * Returns true iff all post categories are in excluded ones
  * or all post tags are in excluded ones
  *
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 1.0.0
  */
 public function isInExcludedTaxonomies($post)
 {
     if (is_a($post, 'WP_Post')) {
         $post = $post->ID;
     }
     $exCats = $this->mainOpts->getCategories();
     if (!empty($exCats)) {
         $postCategories = get_the_category($post);
         if (is_array($postCategories) && !empty($postCategories)) {
             $catIds = array();
             foreach ($postCategories as $cat) {
                 array_push($catIds, $cat->term_id);
             }
             $intersect = array_intersect($catIds, $exCats);
             if (!empty($intersect) && count($intersect) == count($postCategories)) {
                 return TRUE;
             }
         }
     }
     $exTags = $this->mainOpts->getTags();
     if (!empty($exTags)) {
         $postTags = get_the_tags($post);
         if (is_array($postTags) && !empty($postTags)) {
             $tagsIds = array();
             foreach ($postTags as $tag) {
                 array_push($tagsIds, (string) $tag->term_id);
             }
             $intersect = array_intersect($tagsIds, $exTags);
             if (!empty($intersect) && count($intersect) == count($postTags)) {
                 return TRUE;
             }
         }
     }
     return FALSE;
 }
 /**
  * Saves admin options.
  * This is called through a hook
  *
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 1.0.0
  */
 public function saveOptions()
 {
     if (!current_user_can('manage_options')) {
         wp_die('Not allowed');
     }
     erpPROPaths::requireOnce(erpPROPaths::$erpPROMainOpts);
     // Save template options
     if (isset($_POST['dsplLayout'])) {
         erpPROPaths::requireOnce(erpPROPaths::$VPluginThemeFactory);
         VPluginThemeFactory::registerThemeInPathRecursive(erpPROPaths::getAbsPath(erpPROPaths::$mainThemesFolder), $_POST['dsplLayout']);
         $theme = VPluginThemeFactory::getThemeByName($_POST['dsplLayout']);
         if ($theme) {
             $theme->saveSettings($_POST);
             foreach ($theme->getDefOptions() as $key => $value) {
                 unset($_POST[$key]);
             }
         } else {
             // TODO Add notice class
             //                $message = new WP_Error_Notice('Theme '.$_POST ['dsplLayout'].' not found. Theme options discarded', 1, array('settings_page_erp_settings'));
             //                WP_Admin_Notices::getInstance()->addNotice($message);
         }
     }
     // Save the rest of the options
     $mainOptionsObj = new erpPROMainOpts();
     $mainOptionsObj->saveOptions($_POST);
     wp_redirect(add_query_arg(array('page' => $this->plugin_slug . '_settings', 'tab-spec' => wp_strip_all_tags($_POST['tab-spec'])), admin_url('options-general.php')));
     exit;
 }
 /**
  * Get default thumb url
  * @return string|NULL
  * @author Panagiotis Vagenas <*****@*****.**>
  * @since 1.0.0
  */
 public function getDefaultThumbnail()
 {
     if ($this instanceof erpPROMainOpts) {
         $t = $this->getValue('defaultThumbnail');
     } else {
         erpPROPaths::requireOnce(erpPROPaths::$erpPROMainOpts);
         $mOpts = new erpPROMainOpts();
         $t = $mOpts->getDefaultThumbnail();
     }
     return !empty($t) ? $t : erpPRODefaults::$comOpts['defaultThumbnail'];
 }
 private static function delMainOptions()
 {
     erpPROPaths::requireOnce(erpPROPaths::$erpPROMainOpts);
     $mOpts = new erpPROMainOpts();
     delete_option($mOpts->getOptionsArrayName());
 }