Example #1
0
/**
 * Init the helper.
 */
function init_comments_helper()
{
    // Require the class file
    require_once dirname(__FILE__) . '/classes/comments.php';
    // Get the current plugin version
    $plugin_version = get_file_data(__FILE__, ['Version'], 'plugin');
    // Instantiate the class
    $comments = new Comments($plugin_version);
    // Add into the helpers array
    dustpress()->add_helper('comments', $comments);
    // Add templates into DustPress
    add_filter('dustpress/partials', __NAMESPACE__ . '\\add_comments_templates');
    /**
     * Hooks for comment posting and paginating
     */
    $ajaxing = filter_input(INPUT_POST, 'dustpress_comments_ajax', FILTER_SANITIZE_NUMBER_INT);
    if ($ajaxing) {
        /**
         * Notify others
         */
        if (!defined('DUSTPRESS_AJAX')) {
            define('DUSTPRESS_AJAX', true);
        }
        /**
         * Add a hook for handling comment posting
         */
        add_action('comment_post', [$comments, 'comment_posted'], 2);
        /**
         * Handle WP comment errors
         */
        add_filter('wp_die_handler', [$comments, 'get_error_handler']);
        add_filter('wp_die_ajax_handler', [$comments, 'get_error_handler']);
        /**
         * Run pagination
         */
        $paginate = filter_input(INPUT_POST, 'dustpress_comments_paginate', FILTER_SANITIZE_NUMBER_INT);
        if ($paginate) {
            add_action('wp_ajax_dustpress_comments_paginate', [$comments, 'paginate']);
            add_action('wp_ajax_nopriv_dustpress_comments_paginate', [$comments, 'paginate']);
        }
    }
    /**
     * Handle pagination
     */
    $paginating = filter_input(INPUT_POST, 'dustpress_comments_paginate', FILTER_SANITIZE_NUMBER_INT);
    if ($paginating) {
        $page = filter_input(INPUT_POST, 'page', FILTER_SANITIZE_NUMBER_INT);
        $offset = filter_input(INPUT_POST, 'offset', FILTER_SANITIZE_NUMBER_INT);
        $comments->paginate();
    }
}
Example #2
0
 /**
  * Gathers debug data from other sources than DustPress core.
  */
 public static function set_debugger_data($key, $data)
 {
     if (empty($key)) {
         die('You did not set a key for your debugging data collection.');
     } else {
         $debug_data_block_name = dustpress()->get_setting("debug_data_block_name");
         if (!isset(self::$data[$debug_data_block_name])) {
             self::$data[$debug_data_block_name] = [];
         }
         if (!isset(self::$data[$debug_data_block_name][$key])) {
             self::$data[$debug_data_block_name][$key] = [];
         }
         self::$data[$debug_data_block_name][$key][] = $data;
     }
 }
 /**
  * Outputs the contents of the comments.
  *
  * @return string
  */
 public function output()
 {
     // If not ajaxing, get params
     if (!defined('DUSTPRESS_AJAX')) {
         $this->handle_params();
     }
     // Trying to paginate the wp way
     if ($this->wp_pagination()) {
         return;
     }
     // Closing args
     $this->close_old = isset($this->comments_args['close_comments_for_old_posts']) ? $this->comments_args['close_comments_for_old_posts'] : get_option('close_comments_for_old_posts', false);
     // Maybe close comments
     if ($this->close_old) {
         $this->closed_after = $this->comments_args['close_comments_days_old'] ? $this->comments_args['close_comments_days_old'] : get_option('close_comments_days_old', 14);
         $closing_time = strtotime(get_the_date('c', $this->comment_post_id)) + $this->closed_after * 86400;
         if ($closing_time < time()) {
             $this->echo_form = false;
         }
     }
     // Form loading and modification arguments
     if ($this->echo_form) {
         $this->replacements = isset($this->form_args['replace_input']) ? $this->form_args['replace_input'] : null;
         $this->remove = isset($this->form_args['remove_input']) ? $this->form_args['remove_input'] : null;
         $this->status_div = isset($this->form_args['status_div']) ? $this->form_args['status_div'] : null;
         $this->input_class = isset($this->form_args['input_class']) ? $this->form_args['input_class'] : null;
         $this->input_attrs = isset($this->form_args['input_attrs']) ? $this->form_args['input_attrs'] : null;
     }
     // Default args
     $reply_defaults = ['depth' => 1, 'max_depth' => get_option('thread_comments_depth')];
     $this->reply_args = array_merge($reply_defaults, (array) $this->reply_args);
     $comments_defaults = ['status' => current_user_can('moderate_comments') ? 'all' : 'approve'];
     $this->comments_args = array_merge($comments_defaults, (array) $this->comments_args);
     if ($this->echo_form) {
         $this->get_form();
     }
     // Get comments
     if ($this->load_comments) {
         $this->get_comments();
     }
     // Add additional data for comments
     if (is_array($this->comments)) {
         $this->extend_comments();
     }
     // Map data
     $rendering_data = (object) [];
     $rendering_data->ID = apply_filters('dustpress/comments/comment_post_id', $this->comment_post_id);
     $rendering_data->title = apply_filters('dustpress/comments/section_title', $this->section_title);
     $rendering_data->message = apply_filters('dustpress/comments/message', $this->params->message);
     $rendering_data->form = apply_filters('dustpress/comments/form', $this->form);
     $rendering_data->comments = apply_filters('dustpress/comments/comments', $this->comments);
     $rendering_data->after_comments = apply_filters('dustpress/comments/after_comments', $this->after_comments);
     $rendering_data->model = isset($this->params->model) ? $this->params->model : '';
     $rendering_data->filter_slug = isset($this->params->filter_slug) ? $this->params->filter_slug : '';
     // Set the partial name and possibly override it with a filter
     $partial = apply_filters('dustpress/comments/partial', 'comments-container');
     $rendering_data = apply_filters('dustpress/comments/data', $rendering_data);
     // Render output
     return dustpress()->render(['partial' => $partial, 'data' => $rendering_data, 'type' => 'html', 'echo' => false]);
 }
Example #4
0
 /**
  * maybe_cache
  *
  * This function stores data sets to transient cache if it is enabled
  * and indexes cache keys for model-function-pairs.
  *
  * @type   function
  * @date   29/01/2016
  * @since  0.3.1
  *
  * @param  $m (string), $data (any), $subs (array)
  * @return N/A
  */
 private function maybe_cache($m, $data, $subs)
 {
     // Check whether cache is enabled and model has ttl-settings.
     if (dustpress()->get_setting('cache') && $this->is_cacheable_function($m)) {
         // Extend data with submodels
         $to_cache = (object) ['data' => $data, 'subs' => $subs];
         set_transient($this->hash, $to_cache, $this->ttl[$m]);
         // If no hash key exists, bail
         if (!isset($this->hash)) {
             return;
         }
         // Index key for cache clearing
         $index = $this->generate_cache_key($this->class_name, $m);
         $hash_index = get_transient($index);
         if (!is_array($hash_index)) {
             $hash_index = [];
         }
         // Set the data hash key to the index array of this model function
         if (!in_array($this->hash, $hash_index)) {
             $hash_index[] = $this->hash;
         }
         // Store transient for 30 days
         set_transient($index, $hash_index, 30 * DAY_IN_SECONDS);
     }
     return false;
 }
Example #5
0
 /**
  * Renders and outputs the menu HTML.
  *
  * @return $output (string)
  */
 public function output()
 {
     if (!isset($this->params->menu_name) && !isset($this->params->menu_id)) {
         return $this->chunk->write('DustPress menu helper error: No menu specified.');
     } else {
         if (isset($this->params->menu_id)) {
             $menu_id = $this->params->menu_id;
             $id_given = true;
         } else {
             $menu_name = $this->params->menu_name;
         }
     }
     if (isset($this->params->parent)) {
         $parent = $this->params->parent;
     } else {
         $parent = 0;
     }
     if (isset($this->params->depth)) {
         $depth = $this->params->depth;
     } else {
         $depth = PHP_INT_MAX;
     }
     if (isset($this->params->override)) {
         $override = $this->params->override;
     } else {
         $override = null;
     }
     if (isset($this->params->ul_classes)) {
         $ul_classes = $this->params->ul_classes;
     } else {
         $ul_classes = '';
     }
     if (isset($this->params->ul_id)) {
         $ul_id = $this->params->ul_id;
     } else {
         $ul_id = '';
     }
     if (isset($this->params->show_submenu)) {
         $show_submenu = $this->params->show_submenu;
     } else {
         $show_submenu = true;
     }
     if (isset($this->params->menu_partial)) {
         $menu_partial = $this->params->menu_partial;
     } else {
         $menu_partial = "menu";
     }
     if (isset($this->params->menuitem_partial)) {
         $menuitem_partial = $this->params->menuitem_partial;
     } else {
         $menuitem_partial = "menuitem";
     }
     $menu = new \stdClass();
     if (isset($menu_name)) {
         $menu->items = self::get_menu_as_items($menu_name, $parent, $override);
     } else {
         $menu->items = self::get_menu_as_items($menu_id, $parent, $override, true);
     }
     $menu->ul_classes = $ul_classes;
     $menu->ul_id = $ul_id;
     $menu->show_submenu = $show_submenu;
     $menu->menuitem_partial = $menuitem_partial;
     $menu->depth = $depth;
     $menu = apply_filters("dustpress/menu/data", $menu);
     $output = dustpress()->render(['partial' => $menu_partial, 'data' => $menu, 'type' => 'html', 'echo' => false]);
     return apply_filters("dustpress/menu/output", $output);
 }
Example #6
0
 public function output()
 {
     $params = $this->params;
     $data = (object) [];
     $pages = array();
     $visible = 7;
     $neighbours = 3;
     $hellip_start = true;
     $hellip_end = true;
     $strings = isset($params->strings) ? $params->strings : [];
     $cur_page = (int) $params->page;
     $prev_page = $cur_page - 1;
     $next_page = $cur_page + 1;
     $per_page = (int) $params->per_page;
     $items = (int) $params->items;
     $hash = $params->hash ? '#' . $params->hash : '';
     $this->page_var = $params->page_var ? $params->page_var : 'paged';
     // Setup strings
     $defaults = ['previous' => __('Previous', 'dustpress'), 'next' => __('Next', 'dustpress'), 'start' => __('Start', 'dustpress'), 'end' => __('End', 'dustpress')];
     $strings = wp_parse_args($strings, $defaults);
     $page_count = ceil($items / $per_page);
     $first_page = 1;
     $last_page = $page_count;
     $on_first_page = false;
     $on_last_page = false;
     // More items than the set per_page
     if ($items - $per_page > 0) {
         // on the first page
         if ($cur_page == $first_page) {
             $hellip_start = '';
             $on_first_page = true;
             for ($i = 0; $i < 7; $i++) {
                 if ($i + 1 > $page_count) {
                     $hellip_end = '';
                     break;
                 }
                 $pages[$i] = (object) [];
                 $pages[$i]->page = $i + 1;
                 if ($cur_page == $pages[$i]->page) {
                     $pages[$i]->active = true;
                 }
             }
         } elseif ($cur_page == $last_page) {
             $hellip_end = '';
             $on_last_page = true;
             if ($page_count <= $visible) {
                 $hellip_start = '';
                 for ($i = 0; $i < $page_count; $i++) {
                     $pages[$i] = (object) [];
                     $pages[$i]->page = $i + 1;
                     if ($cur_page == $pages[$i]->page) {
                         $pages[$i]->active = true;
                     }
                 }
             } else {
                 $start = $page_count - $visible + 1;
                 for ($i = $start; $i <= $page_count; $i++) {
                     $pages[$i] = (object) [];
                     $pages[$i]->page = $i;
                     if ($cur_page == $pages[$i]->page) {
                         $pages[$i]->active = true;
                     }
                 }
             }
         } else {
             $start = $cur_page - $neighbours;
             if ($start <= 1) {
                 $start = 1;
                 $hellip_start = '';
             }
             $end = $cur_page + $neighbours;
             if ($end >= $page_count) {
                 $end = $page_count;
                 $start = $start - ($cur_page + $neighbours - $page_count);
                 if ($start <= 1) {
                     $start = 1;
                     $hellip_start = '';
                 }
                 $hellip_end = '';
             }
             // display max number of pages
             $max_pages = $start + ($visible - 1);
             if ($max_pages <= $page_count) {
                 for ($i = $start; $i <= $max_pages; $i++) {
                     $pages[$i] = (object) [];
                     $pages[$i]->page = $i;
                     if ($cur_page == $pages[$i]->page) {
                         $pages[$i]->active = true;
                     }
                 }
             } else {
                 for ($i = $start; $i <= $end; $i++) {
                     $pages[$i] = (object) [];
                     $pages[$i]->page = $i;
                     if ($cur_page == $pages[$i]->page) {
                         $pages[$i]->active = true;
                     }
                 }
             }
         }
         if ($prev_page == 0) {
             $prev_page = '';
         }
         if ($next_page > $page_count) {
             $next_page = '';
         }
     }
     $page_link = $this->build_page_link();
     // map data
     $data->on_first_page = $on_first_page;
     $data->on_last_page = $on_last_page;
     $data->first_page = $first_page;
     $data->last_page = $last_page;
     $data->pages = $pages;
     $data->hellip_start = $hellip_start;
     $data->hellip_end = $hellip_end;
     $data->next_page = $next_page;
     $data->prev_page = $prev_page;
     $data->hash = $hash;
     $data->page_var = $this->page_var;
     $data->page_link = apply_filters('dustpress/pagination/page_link', $page_link);
     $data->S = (object) [];
     $data->S->prev = $strings['previous'];
     $data->S->next = $strings['next'];
     $data->S->start = $strings['start'];
     $data->S->end = $strings['end'];
     $this->data = $data;
     return dustpress()->render(["partial" => "pagination", "data" => $this->data, "type" => "html", "echo" => false]);
 }