* @author Sam Bauers * @copyright 2007 Aditya Naik * @copyright 2007 Sam Bauers * @license http://www.gnu.org/licenses/gpl.txt GNU General Public License v2 * @version 2.3.5 **/ /** * Wrapper class for the Support forum plugin * * @author Sam Bauers * @version 2.3.4 **/ class Support_Forum { /** * An array of forum ids that are support forums * * @var array **/ var $enabled; /** * The default support status of new topics * * @var string **/ var $status; /** * The list of available resolutions to choose from * * @var array **/ var $resolutions; /** * The settings for views * * @var array **/ var $views; /** * Just helps pass a variable between a couple of functions * * @var string **/ var $statusForGetView; /** * The settings for icons * * @var array **/ var $icons; /** * The path past the base url where the icons reside * * @var string **/ var $iconPath; /** * The uri where the icons reside * * @var string **/ var $iconURI; /** * Whether or not the topic poster can set the status * * @var boolean **/ var $posterSetable; /** * Whether or not the topic poster can change the status * * @var boolean **/ var $posterChangeable; /** * Retrieves settings for the plugin * * @return void * @author Sam Bauers **/ function Support_Forum() { $this->enabled = bb_get_option('support_forum_enabled'); $this->correctEnabled(); $this->status = bb_get_option('support_forum_default_status'); $this->resolutions = array( 'yes' => __('resolved'), 'no' => __('not resolved'), 'mu' => __('not a support question') ); $this->getViewStatus(); $this->getIconStatus(); $this->iconPath = str_replace(rtrim(BBPLUGINDIR, '/'), '', dirname(__FILE__)) . '/'; if (defined('SUPPORT_FORUM_ICON_URI')) { $this->iconURI = SUPPORT_FORUM_ICON_URI; } else { $this->iconURI = bb_get_option('uri') . $this->iconPath; } $this->posterSetable = bb_get_option('support_forum_poster_setable'); } /** * Returns the current active state of the plugin * * @return boolean * @author Sam Bauers **/ function isActive() { $this->enabled = bb_get_option('support_forum_enabled'); $this->correctEnabled(); if (!$this->enabled) { $this->enabled = array(); } if (isset($this->enabled) && is_array($this->enabled)) { if (count($this->enabled) > 0) { return true; } else { return false; } } else { return false; } } /** * Correct pre 2.1 support_forum_enabled option * * @return void * @author Sam Bauers **/ function correctEnabled() { if (!is_array($this->enabled)) { bb_delete_option('support_forum_enabled'); $this->enabled = false; } } /** * Returns the current default status * * @return string * @author Sam Bauers **/ function getStatus() { $this->status = bb_get_option('support_forum_default_status'); if (isset($this->status)) { return $this->status; } else { return 'mu'; } } /** * Update the current view settings * * @return array * @author Sam Bauers **/ function getViewStatus() { $this->views = bb_get_option('support_forum_views'); if (!isset($this->views)) { $this->views = array('no' => true); } } /** * Update the current icon settings * * @return void * @author Sam Bauers **/ function getIconStatus() { $this->icons = array( 'status' => bb_get_option('support_forum_icons_status'), 'closed' => bb_get_option('support_forum_icons_closed'), 'sticky' => bb_get_option('support_forum_icons_sticky') ); } /** * Determines if the current user can change the status of a topic * * @return void * @author Sam Bauers **/ function getChangeableStatus($topic_id = 0) { if (isset($this->posterChangeable)) { return $this->posterChangeable; } if (is_topic()) { if (bb_current_user_can('edit_others_topics', $topic->topic_id)) { $this->posterChangeable = TRUE; return TRUE; } if (bb_get_option('support_forum_poster_changeable')) { if (!$topic_id) { global $topic; } else { $topic = get_topic($topic_id); } if ($topic->topic_poster == bb_get_current_user_info('id')) { $this->posterChangeable = TRUE; return TRUE; } } } $this->posterChangeable = FALSE; return FALSE; } /** * Adds a view to bbPress * * @return array * @author Sam Bauers **/ function addViews($views) { foreach ($this->views as $status => $enabled) { if ($enabled) { $views['support-forum-' . $status] = sprintf(__('Support topics that are %s'), $this->resolutions[$status]); } } return $views; } /** * Adds a filter to show the unresolved topics view * * @return void * @author Sam Bauers **/ function processViews($view, $page) { global $topics; global $view_count; global $bbdb; foreach ($this->views as $status => $enabled) { if ($enabled) { if ($view == 'support-forum-' . $status) { $this->statusForGetView = $status; add_filter('get_latest_topics_where', array(&$this, 'getView')); $topics = get_latest_topics(0, $page); $view_count = bb_count_last_query(); } } } } /** * Returns the "where" part of the SQL query for the unresolved topics view * * @return string * @author Sam Bauers **/ function getView($where) { global $bbdb; $query = 'SELECT ' . $bbdb->topics . '.topic_id'; $query .= ' FROM ' . $bbdb->topics; $query .= ' LEFT JOIN ' . $bbdb->topicmeta; $query .= ' ON ' . $bbdb->topics . '.topic_id = ' . $bbdb->topicmeta . '.topic_id'; $query .= " AND meta_key = 'topic_resolved'"; if ($this->getStatus() == $this->statusForGetView) { $query .= " WHERE (meta_value = '" . $this->statusForGetView . "'"; $query .= ' OR meta_value IS NULL)'; } else { $query .= " WHERE meta_value = '" . $this->statusForGetView . "'"; } $query .= ' AND ' . $bbdb->topics . '.forum_id IN (' . join(',', $this->enabled) . ')'; $topic_ids = $bbdb->get_col($query); if ($topic_ids) { $topics_in = join(',', $topic_ids); $where = 'WHERE topic_status = 0 AND topic_open = 1 AND topic_id IN (' . $topics_in . ')'; } else { $where = 'WHERE 0'; } return $where; } /** * Registers a view to bbPress * * @return void * @author Sam Bauers **/ function registerViews() { if (is_callable('bb_register_view')) { // Build 876+ foreach ($this->views as $status => $enabled) { if ($enabled) { if ($this->getStatus() == $status) { $query = array( 'sticky' => 'all', 'meta_key' => 'topic_resolved', 'meta_value'=> $status . ',NULL', 'forum_id' => join(',', $this->enabled) ); } else { $query = array( 'sticky' => 'all', 'meta_key' => 'topic_resolved', 'meta_value'=> $status, 'forum_id' => join(',', $this->enabled) ); } bb_register_view('support-forum-' . $status, sprintf(__('Support topics that are %s'), $this->resolutions[$status]), $query); } } } else { // Build 214-875 add_filter('bb_views', array(&$this, 'addViews')); add_action('bb_custom_view', array(&$this, 'processViews'),10,2); } } /** * Runs either the status chooser or the status statement function depending on permissions * * @return string * @author Sam Bauers **/ function getStatusDisplay() { global $topic; if ($this->getChangeableStatus()) { $this->statusChooser(); } else { $this->statusStatement(); } } /** * Prints a string of HTML which is a topic status chooser * * @return string * @author Sam Bauers **/ function statusChooser() { global $topic; $topicStatus = $this->getTopicStatus(); if ($this->icons['status']) { echo ' '; } $r = '
' . "\n"; $r .= '' . "\n"; $r .= '' . "\n"; $r .= '' . "\n"; $r .= '' . "\n"; $r .= '
' . "\n"; echo $r; bb_nonce_field('support-forum-resolve-topic_' . $topic->topic_id); echo "\n" . '
'; } /** * Prints a string of HTML which is a statement of the current status * * @return string * @author Sam Bauers **/ function statusStatement() { $topicStatus = $this->getTopicStatus(); if ($this->icons['status']) { echo ' '; } echo $this->resolutions[$topicStatus]; } /** * Prints a dropdown status selector for the topic meta area of each topic * * @return string * @author Sam Bauers **/ function topicMeta() { global $topic; if ($this->icons['sticky']) { if ($topic->topic_sticky > 0) { echo '
  • ' . __('This topic is') . ' ' . __('sticky') . '
  • ' . "\n"; } } if ($this->icons['closed']) { if ('0' === $topic->topic_open) { echo '
  • ' . __('This topic is') . ' ' . __('closed') . '
  • ' . "\n"; } } if (in_array($topic->forum_id, $this->enabled)) { echo '
  • ' . __('This topic is') . ' '; $this->getStatusDisplay(); echo '
  • ' . "\n"; } } /** * Returns the current support status of a given topic * * @return void * @author Sam Bauers **/ function getTopicStatus() { global $topic; if (in_array($topic->forum_id, $this->enabled)) { if ($topic->topic_resolved) { return $topic->topic_resolved; } else { return $this->getStatus(); } } else { return false; } } /** * Sets the status of the given topic * * @return boolean * @author Sam Bauers **/ function setTopicStatus($topic_id, $resolution = 'yes') { global $bbdb; global $bb_cache; $topic_id = (integer) $topic_id; apply_filters('topic_resolution', $status, $topic_id); if (!in_array($resolution, array_keys($this->resolutions))) { return false; } $bb_cache->flush_one('topic', $topic_id); bb_update_topicmeta($topic_id, 'topic_resolved', $resolution); return true; } /** * Prints a string that is a javascript call to allow for AJAX editing of the topic support status * * @return string * @author Sam Bauers **/ function AJAX_setTopicStatus() { if (is_topic()) { $r = '' . "\n"; echo $r; } } /** * undocumented function * * @return void * @author Sam Bauers **/ function AJAX_setTopicStatusProcess() { global $topic; $topic_id = (integer) @$_POST['topic_id']; $resolved = @$_POST['resolved']; if ($this->getChangeableStatus($topic_id)) { die('-1'); } $topic = get_topic($topic_id); if (!$topic) { die('0'); } if ($this->setTopicStatus($topic_id, $resolved)) { $topic->topic_resolved = $resolved; ob_start(); echo '
  • ' . __('This topic is') . ' '; $this->statusChooser(); echo '
  • '; $data = ob_get_contents(); ob_end_clean(); $x = new WP_Ajax_Response( array( 'what' => 'resolution', 'id' => 'flipper', 'data' => $data ) ); $x->send(); } } /** * Adds a class to resolved topics * * @return array * @author Sam Bauers **/ function addTopicClass($class) { global $topic; if ('yes' == $topic->topic_resolved && in_array($topic->forum_id, $this->enabled)) { $class[] = 'resolved'; } return $class; } /** * Appends data to the topic title that indicates the topic support status * * @return string * @author Sam Bauers **/ function modifyTopicLabelStatus($label) { if (is_forum() || is_front() || is_view()) { $topicStatus = $this->getTopicStatus(); if ($topicStatus) { if ($this->icons['status']) { $status_image = '[' . $this->resolutions[$topicStatus] . ']'; } elseif ($topicStatus != 'mu') { $status_image = '[' . $this->resolutions[$topicStatus] . '] '; } $label = $status_image . $label; } } return $label; } /** * Appends data to the topic title that indicates the topic closed status * * @return string * @author Sam Bauers **/ function modifyTopicLabelClosed($label) { if (is_forum() || is_front() || is_view()) { global $topic; if ('0' === $topic->topic_open) { return sprintf(__('[' . __('closed') . ']%s'), $label); } } return $label; } /** * Appends data to the topic title that indicates the topic sticky status * * @return string * @author Sam Bauers **/ function modifyTopicLabelSticky($label) { if (is_forum() || is_front() || is_view()) { global $topic; if ($topic->topic_sticky > 0) { return sprintf(__('[' . __('sticky') . ']%s'), $label); } } return $label; } /** * Adds javascript that determines if the status dropdown in the topic post form is active * * @return void * @author Sam Bauers **/ function addStatusSelectModifier() { if ($this->posterSetable && ((is_front() && $_GET['new']) || is_tag())) { $j = '' . "\n"; echo $j; } } /** * Adds a dropdown to the topic post form so that a status can be set on topic creation * * @return void * @author Sam Bauers **/ function addStatusSelectToPostForm() { if ($this->posterSetable && !is_topic()) { global $forum; if (!$forum || in_array($forum->forum_id, $this->enabled)) { $r = '

    ' . "\n"; $r .= '' . "\n"; $r .= '

    ' . "\n"; echo $r; } } } /** * Sets the status of the given topic on creation * * @return boolean * @author Sam Bauers **/ function setTopicStatusOnCreation($topic_id) { if ($this->posterSetable && isset($_POST['topic_support_status'])) { $this->setTopicStatus($topic_id, $_POST['topic_support_status']); } } } // END class Support_Forum // Initialise the class $support_forum = new Support_Forum(); if ($support_forum->isActive()) { $support_forum->registerViews(); add_action('topicmeta', array(&$support_forum, 'topicMeta')); add_action('bb_head', array(&$support_forum, 'AJAX_setTopicStatus')); add_action('bb_ajax_update-resolution', array(&$support_forum, 'AJAX_setTopicStatusProcess')); add_filter('topic_class', array(&$support_forum, 'addTopicClass')); add_action('bb_head', array(&$support_forum, 'addStatusSelectModifier')); add_action('post_form_pre_post', array(&$support_forum, 'addStatusSelectToPostForm')); add_action('bb_new_topic', array(&$support_forum, 'setTopicStatusOnCreation')); if ($support_forum->icons['closed']) { if (function_exists('bb_topic_labels')) { remove_filter('bb_topic_labels', 'bb_closed_label', 10); // Build 968+ add_filter('bb_topic_labels', array(&$support_forum, 'modifyTopicLabelClosed'), 20); } else { remove_filter('topic_title', 'closed_title', 30); // Build 371-791 remove_filter('topic_title', 'bb_closed_title', 30); // Build 792+ add_filter('topic_title', array(&$support_forum, 'modifyTopicLabelClosed'), 30); } } if ($support_forum->icons['sticky']) { if (function_exists('bb_topic_labels')) { remove_filter('bb_topic_labels', 'bb_sticky_label', 20); // Build 968+ add_filter('bb_topic_labels', array(&$support_forum, 'modifyTopicLabelSticky'), 30); } } if (function_exists('bb_topic_labels')) { add_filter('bb_topic_labels', array(&$support_forum, 'modifyTopicLabelStatus'), 10); } else { add_filter('topic_title', array(&$support_forum, 'modifyTopicLabelStatus'), 40); } } /** * The admin pages below are handled outside of the class due to constraints * in the architecture of the admin menu generation routine in bbPress */ // Add filters for the admin area add_action('bb_admin_menu_generator', 'support_forum_admin_page_add'); add_action('bb_admin_head', 'support_forum_admin_page_head'); add_action('bb_admin-header.php', 'support_forum_admin_page_process'); /** * Adds in an item to the $bb_admin_submenu array * * @return void * @author Sam Bauers **/ function support_forum_admin_page_add() { if (function_exists('bb_admin_add_submenu')) { // Build 794+ bb_admin_add_submenu(__('Support forum'), 'use_keys', 'support_forum_admin_page'); } else { global $bb_submenu; $submenu = array(__('Support forum'), 'use_keys', 'support_forum_admin_page'); if (isset($bb_submenu['plugins.php'])) { // Build 740-793 $bb_submenu['plugins.php'][] = $submenu; } else { // Build 277-739 $bb_submenu['site.php'][] = $submenu; } } } /** * Adds some cute javascript to the admin head that controls the enabling and disabling of one of the checkboxes * * @return string * @author Sam Bauers **/ function support_forum_admin_page_head() { global $bb_current_submenu; if ($bb_current_submenu[2] == 'support_forum_admin_page') { $j = '' . "\n"; echo $j; } } /** * Upgrades the topicmeta table data to be compatible with version 1.1+ of the plugin * * @return void * @author Sam Bauers **/ function support_forum_upgrade_1_1() { global $bbdb; $rows = $bbdb->get_results("SELECT * FROM $bbdb->topicmeta WHERE meta_key = 'support_forum_resolved'"); if ($rows) { foreach($rows as $row) : bb_update_topicmeta($row->topic_id, 'topic_resolved', $row->meta_value); bb_delete_topicmeta($row->topic_id, 'support_forum_resolved'); endforeach; bb_admin_notice(__('Update performed')); } else { bb_admin_notice(__('No update required')); } } /** * Writes an admin page for the plugin * * @return string * @author Sam Bauers **/ function support_forum_admin_page() { global $support_forum; // Called here to get the enabled forums after an update $support_forum->isActive(); $support_forum_default_status = array( 'yes' => null, 'no' => null, 'mu' => null ); $support_forum_default_status[$support_forum->getStatus()] = ' selected="selected"'; if (bb_get_option('support_forum_poster_setable')) { $support_forum_poster_setable_checked = "checked=\"checked\" "; $support_forum_poster_changeable_disabled = NULL; } else { $support_forum_poster_setable_checked = NULL; $support_forum_poster_changeable_disabled = "disabled=\"disabled\" "; } $support_forum_poster_changeable_checked = (bb_get_option('support_forum_poster_changeable')) ? "checked=\"checked\" " : ""; $support_forum->getViewStatus(); $support_forum_views_checked = array( 'yes' => ($support_forum->views['yes']) ? "checked=\"checked\" " : "", 'no' => ($support_forum->views['no']) ? "checked=\"checked\" " : "", 'mu' => ($support_forum->views['mu']) ? "checked=\"checked\" " : "" ); $support_forum->getIconStatus(); $support_forum_icons_status_checked = ($support_forum->icons['status']) ? "checked=\"checked\" " : ""; $support_forum_icons_closed_checked = ($support_forum->icons['closed']) ? "checked=\"checked\" " : ""; $support_forum_icons_sticky_checked = ($support_forum->icons['sticky']) ? "checked=\"checked\" " : ""; ?>



    /> />


    >
    >
    >


    /> resolutions as $resolution => $display) { ?> -

    /> -

    /> -


    0) { bb_update_option('support_forum_enabled', $_POST['support_forum_enabled']); } else { bb_update_option('support_forum_enabled', 0); } if ($_POST['support_forum_default_status']) { bb_update_option('support_forum_default_status', $_POST['support_forum_default_status']); } if ($_POST['support_forum_poster_setable']) { bb_update_option('support_forum_poster_setable', 1); } else { bb_delete_option('support_forum_poster_setable'); } if ($_POST['support_forum_poster_changeable']) { bb_update_option('support_forum_poster_changeable', 1); } else { bb_delete_option('support_forum_poster_changeable'); } if ($_POST['support_forum_views']) { bb_update_option('support_forum_views', $_POST['support_forum_views']); } if ($_POST['support_forum_icons_status']) { bb_update_option('support_forum_icons_status', 1); } else { bb_delete_option('support_forum_icons_status'); } if ($_POST['support_forum_icons_closed']) { bb_update_option('support_forum_icons_closed', 1); } else { bb_delete_option('support_forum_icons_closed'); } if ($_POST['support_forum_icons_sticky']) { bb_update_option('support_forum_icons_sticky', 1); } else { bb_delete_option('support_forum_icons_sticky'); } bb_admin_notice(__('Settings saved')); } } elseif (isset($_POST['submit_upgrade'])) { if ($_POST['action'] == 'support_forum_post_upgrade') { support_forum_upgrade_1_1(); } } } ?>