Пример #1
0
 /**
  * Creates a new instance of the renderer
  *
  * @param UTCW_Plugin $plugin Main plugin instance
  *
  * @since 2.0
  */
 public function __construct(UTCW_Plugin $plugin)
 {
     $this->data = $plugin->get('data');
     $this->config = $plugin->get('renderConfig');
     $this->plugin = $plugin;
     $this->id = base_convert(crc32(serialize($this->config)), 10, 27);
     $this->buildCSS();
 }
Пример #2
0
 /**
  * Creates a new instance of the QueryBuilder
  *
  * @param UTCW_Plugin $plugin Main plugin instance
  *
  * @since 2.2
  */
 public function __construct(UTCW_Plugin $plugin)
 {
     $this->db = $plugin->get('wpdb');
     $this->plugin = $plugin;
     $this->query = $this->getBaseQuery();
     $this->parameters = array();
 }
Пример #3
0
 /**
  * Will add the style to the internal array if the option doesn't have is default value
  *
  * @param string $option
  * @param string $template
  * @param string $value
  *
  * @since 2.6
  */
 protected function addStyle($option, $template, $value = '')
 {
     if (!$value) {
         $value = $this->plugin->get('renderConfig')->{$option};
     }
     if (!$this->hasDefaultValue($option)) {
         $this->styles[] = sprintf($template, $value);
     }
 }
Пример #4
0
 /**
  * Returns the SQL query to be used when fetching terms
  *
  * @return string
  * @since 2.6
  */
 protected function getQuery()
 {
     $config = $this->plugin->get('dataConfig');
     $db = $this->plugin->get('wpdb');
     $builder = new UTCW_QueryBuilder($this->plugin);
     $builder->addAuthorConstraint($config->authors);
     $builder->addPostTypeConstraint($config->post_type);
     $builder->addPostStatusConstraint($this->plugin->isAuthenticatedUser(), $config->post_type);
     $builder->addDaysOldConstraint($config->days_old);
     $builder->addTaxonomyConstraint($config->taxonomy);
     $builder->addTagsListConstraint($config->tags_list_type, $config->tags_list, $config->taxonomy);
     $builder->addPostTermConstraint($config->post_term);
     $builder->addGrouping();
     $builder->addMinimum($config->minimum);
     // Add statements from the strategy
     $this->buildQuery($builder);
     $builder->addMaxConstraint($config->max);
     $builder->addSort($config->order, $config->reverse, $config->case_sensitive);
     $query = $builder->getQuery();
     $parameters = $builder->getParameters();
     return $db->prepare($query, $parameters);
 }
Пример #5
0
 /**
  * Loads terms based on current configuration
  *
  * @return UTCW_Term[]
  * @since 2.0
  */
 public function getTerms()
 {
     $this->terms = array();
     $this->result = $this->config->strategy->getData();
     // Calculate sizes
     $min_count = PHP_INT_MAX;
     $max_count = 0;
     // Get translation handler if a translation plugin is active
     $this->translationHandler = $this->plugin->get('translationHandler');
     foreach ($this->result as $item) {
         if ($item->count < $min_count) {
             $min_count = $item->count;
         }
         if ($item->count > $max_count) {
             $max_count = $item->count;
         }
         if ($this->translationHandler) {
             // Let the translation handler determine if the term should be included or not
             $term = $this->translationHandler->createTerm($item, $this->plugin);
             if ($term) {
                 $this->terms[] = $term;
             }
         } else {
             $this->terms[] = new UTCW_Term($item, $this->plugin);
         }
     }
     if ($this->config->post_term_query_var && $this->config->post_term) {
         $this->addTermFilterQueryVars();
     }
     $size_from = floatval($this->config->size_from);
     $size_to = floatval($this->config->size_to);
     $unit = preg_replace('/' . UTCW_DECIMAL_REGEX . '/', '', $this->config->size_from);
     $font_step = $this->calcStep($min_count, $max_count, $size_from, $size_to);
     foreach ($this->terms as $term) {
         $term->size = $this->calcSize($size_from, $term->count, $min_count, $font_step) . $unit;
     }
     // Set colors
     switch ($this->config->color) {
         case 'random':
             foreach ($this->terms as $term) {
                 $term->color = sprintf(UTCW_HEX_COLOR_FORMAT, rand() % 256, rand() % 256, rand() % 256);
             }
             break;
         case 'set':
             if ($this->config->color_set) {
                 foreach ($this->terms as $term) {
                     $term->color = $this->config->color_set[array_rand($this->config->color_set)];
                 }
             }
             break;
         case 'span':
             if ($this->config->color_span_from && $this->config->color_span_to) {
                 preg_match_all('/[0-9a-f]{2}/i', $this->config->color_span_from, $cf_rgb_matches);
                 list($red_from, $green_from, $blue_from) = array_map('hexdec', $cf_rgb_matches[0]);
                 preg_match_all('/[0-9a-f]{2}/i', $this->config->color_span_to, $ct_rgb_matches);
                 list($red_to, $green_to, $blue_to) = array_map('hexdec', $ct_rgb_matches[0]);
                 $colors = new stdClass();
                 $colors->red_from = $red_from;
                 $colors->red_to = $red_to;
                 $colors->green_from = $green_from;
                 $colors->green_to = $green_to;
                 $colors->blue_from = $blue_from;
                 $colors->blue_to = $blue_to;
                 foreach ($this->terms as $term) {
                     $term->color = $this->calcColor($min_count, $max_count, $colors, $term->count);
                 }
             }
     }
     // Last order by color if selected, this is the only order which can't be done in the DB
     if ($this->config->order == 'color') {
         // Change the argument order to change the sort order
         $sort_fn_arguments = $this->config->reverse ? '$b,$a' : '$a,$b';
         // There's no difference in sortin case sensitive or case in-sensitive since
         // the colors are always lower case and internally generated
         $sort_fn = create_function($sort_fn_arguments, 'return strcmp( $a->color, $b->color );');
         usort($this->terms, $sort_fn);
     }
     return $this->terms;
 }