Advertisement
sgaffney

fcking bbpress plugin 500th attempt

Nov 6th, 2011
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.39 KB | None | 0 0
  1. /**
  2.  * Functions of bbPress's Twenty Ten theme
  3.  *
  4.  * @package bbPress
  5.  * @subpackage BBP_Twenty_Ten
  6.  * @since Twenty Ten 1.1
  7.  */
  8.  
  9. // Exit if accessed directly
  10. if ( !defined( 'ABSPATH' ) ) exit;
  11.  
  12. /** Theme Setup ***************************************************************/
  13.  
  14. if ( !class_exists( 'tumbleboard' ) ) :
  15. /**
  16.  * Loads bbPress Twenty Ten Theme functionality
  17.  *
  18.  * Usually functions.php contains a few functions wrapped in function_exisits()
  19.  * checks. Since bbp-twenty-ten is intended to be used both as a child theme and
  20.  * for Theme Compatibility, we've moved everything into one convenient class
  21.  * that can be copied or extended.
  22.  *
  23.  * See @link BBP_Theme_Compat() for more.
  24.  *
  25.  * @since bbPress (r3277)
  26.  *
  27.  * @package bbPress
  28.  * @subpackage BBP_Twenty_Ten
  29.  */
  30. class tumbleboard extends BBP_Theme_Compat {
  31.  
  32.     /** Functions *************************************************************/
  33.  
  34.     /**
  35.      * The main bbPress (Twenty Ten) Loader
  36.      *
  37.      * @since bbPress (r3277)
  38.      *
  39.      * @uses BBP_Twenty_Ten::setup_globals()
  40.      * @uses BBP_Twenty_Ten::setup_actions()
  41.      */
  42.     public function __construct() {
  43.         $this->setup_globals();
  44.         $this->setup_actions();
  45.     }
  46.  
  47.     /**
  48.      * Component global variables
  49.      *
  50.      * @since bbPress (r2626)
  51.      * @access private
  52.      *
  53.      * @uses plugin_dir_path() To generate bbPress plugin path
  54.      * @uses plugin_dir_url() To generate bbPress plugin url
  55.      * @uses apply_filters() Calls various filters
  56.      */
  57.     private function setup_globals() {
  58.         global $bbp;
  59.  
  60.         // Theme name to help identify if it's been extended
  61.         $this->name = 'tumbleboard';
  62.  
  63.         // Version of theme in YYYMMDD format
  64.         $this->version = '20110921';
  65.  
  66.         // Setup the theme path
  67.         $this->dir = WP_PLUGIN_DIR . '/tumbleboard/wptumble-twentyten';
  68.  
  69.         // Setup the theme URL
  70.         $this->url = WP_PLUGIN_URL . '/tumbleboard/wptumble-twentyten';
  71.     }
  72.  
  73.     /**
  74.      * Setup the theme hooks
  75.      *
  76.      * @since bbPress (r3277)
  77.      * @access private
  78.      *
  79.      * @uses add_filter() To add various filters
  80.      * @uses add_action() To add various actions
  81.      */
  82.     private function setup_actions() {
  83.  
  84.         // Add theme support for bbPress
  85.         // add_action( 'after_setup_theme',        array( $this, 'add_theme_support'     ) );
  86.  
  87.         // Enqueue theme CSS
  88.         // add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_styles'        ) );
  89.  
  90.         // Enqueue theme JS
  91.         add_action( 'bbp_enqueue_scripts',      array( $this, 'enqueue_scripts'       ) );
  92.  
  93.         // Enqueue theme script localization
  94.         add_filter( 'bbp_enqueue_scripts',      array( $this, 'localize_topic_script' ) );
  95.  
  96.         // Output some extra JS in the <head>
  97.         add_action( 'bbp_head',                 array( $this, 'head_scripts'          ) );
  98.  
  99.         // Handles the ajax favorite/unfavorite
  100.         add_action( 'wp_ajax_dim-favorite',     array( $this, 'ajax_favorite'         ) );
  101.  
  102.         // Handles the ajax subscribe/unsubscribe
  103.         add_action( 'wp_ajax_dim-subscription', array( $this, 'ajax_subscription'     ) );
  104.     }
  105.  
  106.     /**
  107.      * Sets up theme support for bbPress
  108.      *
  109.      * Because this theme comes bundled with bbPress template files, we add it
  110.      * to the list of things this theme supports. Note that the function
  111.      * "add_theme_support()" does not /enable/ theme support, but is instead an
  112.      * API for telling WordPress what it can already do on its own.
  113.      *
  114.      * If you're looking to add bbPress support into your own custom theme, you'll
  115.      * want to make sure it includes all of the template files for bbPress, and then
  116.      * use: add_theme_support( 'bbpress' ); in your functions.php.
  117.      *
  118.      * @since bbPress (r2652)
  119.      */
  120.     public function add_theme_support() {
  121.         add_theme_support( 'bbpress' );
  122.     }
  123.  
  124.     /**
  125.      * Load the theme CSS
  126.      *
  127.      * @since bbPress (r2652)
  128.      *
  129.      * @uses wp_enqueue_style() To enqueue the styles
  130.      */
  131.     public function enqueue_styles() {
  132.  
  133.         // Right to left
  134.         if ( is_rtl() ) {
  135.  
  136.             // TwentyTen
  137.             wp_enqueue_style( 'twentyten',     get_template_directory_uri() . '/style.css', '',          $this->version, 'screen' );
  138.             wp_enqueue_style( 'twentyten-rtl', get_template_directory_uri() . '/rtl.css',   'twentyten', $this->version, 'screen' );
  139.  
  140.             // bbPress specific
  141.             wp_enqueue_style( 'bbp-twentyten-bbpress', get_stylesheet_directory_uri() . '/css/bbpress-rtl.css', 'twentyten-rtl', $this->version, 'screen' );
  142.  
  143.         // Left to right
  144.         } else {
  145.  
  146.             // TwentyTen
  147.             wp_enqueue_style( 'twentyten', get_template_directory_uri() . '/style.css', '', $this->version, 'screen' );
  148.  
  149.             // bbPress specific
  150.             wp_enqueue_style( 'bbp-twentyten-bbpress', get_stylesheet_directory_uri() . '/css/bbpress.css', 'twentyten', $this->version, 'screen' );
  151.         }
  152.     }
  153.  
  154.     /**
  155.      * Enqueue the required Javascript files
  156.      *
  157.      * @since bbPress (r2652)
  158.      *
  159.      * @uses bbp_is_single_topic() To check if it's the topic page
  160.      * @uses get_stylesheet_directory_uri() To get the stylesheet directory uri
  161.      * @uses bbp_is_single_user_edit() To check if it's the profile edit page
  162.      * @uses wp_enqueue_script() To enqueue the scripts
  163.      */
  164.     public function enqueue_scripts() {
  165.  
  166.         if ( bbp_is_single_topic() )
  167.             wp_enqueue_script( 'bbp_topic', WP_PLUGIN_URL . '/tumbleboard/wptumble-twentyten/js/topic.js', array( 'wp-lists' ), $this->version );
  168.  
  169.         if ( bbp_is_single_user_edit() )
  170.             wp_enqueue_script( 'user-profile' );
  171.     }
  172.  
  173.     /**
  174.      * Put some scripts in the header, like AJAX url for wp-lists
  175.      *
  176.      * @since bbPress (r2652)
  177.      *
  178.      * @uses bbp_is_single_topic() To check if it's the topic page
  179.      * @uses admin_url() To get the admin url
  180.      * @uses bbp_is_single_user_edit() To check if it's the profile edit page
  181.      */
  182.     public function head_scripts() {
  183.         if ( bbp_is_single_topic() ) : ?>
  184.  
  185.         <script type='text/javascript'>
  186.             /* <![CDATA[ */
  187.             var ajaxurl = '<?php echo admin_url( 'admin-ajax.php' ); ?>';
  188.             /* ]]> */
  189.         </script>
  190.  
  191.         <?php elseif ( bbp_is_single_user_edit() ) : ?>
  192.  
  193.         <script type="text/javascript" charset="utf-8">
  194.             if ( window.location.hash == '#password' ) {
  195.                 document.getElementById('pass1').focus();
  196.             }
  197.         </script>
  198.  
  199.         <?php
  200.         endif;
  201.     }
  202.  
  203.     /**
  204.      * Load localizations for topic script
  205.      *
  206.      * These localizations require information that may not be loaded even by init.
  207.      *
  208.      * @since bbPress (r2652)
  209.      *
  210.      * @uses bbp_is_single_topic() To check if it's the topic page
  211.      * @uses is_user_logged_in() To check if user is logged in
  212.      * @uses bbp_get_current_user_id() To get the current user id
  213.      * @uses bbp_get_topic_id() To get the topic id
  214.      * @uses bbp_get_favorites_permalink() To get the favorites permalink
  215.      * @uses bbp_is_user_favorite() To check if the topic is in user's favorites
  216.      * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  217.      * @uses bbp_is_user_subscribed() To check if the user is subscribed to topic
  218.      * @uses bbp_get_topic_permalink() To get the topic permalink
  219.      * @uses wp_localize_script() To localize the script
  220.      */
  221.     public function localize_topic_script() {
  222.  
  223.         // Bail if not viewing a single topic
  224.         if ( !bbp_is_single_topic() )
  225.             return;
  226.  
  227.         // Bail if user is not logged in
  228.         if ( !is_user_logged_in() )
  229.             return;
  230.  
  231.         $user_id = bbp_get_current_user_id();
  232.  
  233.         $localizations = array(
  234.             'currentUserId' => $user_id,
  235.             'topicId'       => bbp_get_topic_id(),
  236.         );
  237.  
  238.         // Favorites
  239.         if ( bbp_is_favorites_active() ) {
  240.             $localizations['favoritesActive'] = 1;
  241.             $localizations['favoritesLink']   = bbp_get_favorites_permalink( $user_id );
  242.             $localizations['isFav']           = (int) bbp_is_user_favorite( $user_id );
  243.             $localizations['favLinkYes']      = __( 'favorites',                                         'bbpress' );
  244.             $localizations['favLinkNo']       = __( '?',                                                 'bbpress' );
  245.             $localizations['favYes']          = __( 'This topic is one of your %favLinkYes% [%favDel%]', 'bbpress' );
  246.             $localizations['favNo']           = __( '%favAdd% (%favLinkNo%)',                            'bbpress' );
  247.             $localizations['favDel']          = __( '&times;',                                           'bbpress' );
  248.             $localizations['favAdd']          = __( 'Add this topic to your favorites',                  'bbpress' );
  249.         } else {
  250.             $localizations['favoritesActive'] = 0;
  251.         }
  252.  
  253.         // Subscriptions
  254.         if ( bbp_is_subscriptions_active() ) {
  255.             $localizations['subsActive']   = 1;
  256.             $localizations['isSubscribed'] = (int) bbp_is_user_subscribed( $user_id );
  257.             $localizations['subsSub']      = __( 'Subscribe',   'bbpress' );
  258.             $localizations['subsUns']      = __( 'Unsubscribe', 'bbpress' );
  259.             $localizations['subsLink']     = bbp_get_topic_permalink();
  260.         } else {
  261.             $localizations['subsActive'] = 0;
  262.         }
  263.  
  264.         wp_localize_script( 'bbp_topic', 'bbpTopicJS', $localizations );
  265.     }
  266.  
  267.     /**
  268.      * Add or remove a topic from a user's favorites
  269.      *
  270.      * @since bbPress (r2652)
  271.      *
  272.      * @uses bbp_get_current_user_id() To get the current user id
  273.      * @uses current_user_can() To check if the current user can edit the user
  274.      * @uses bbp_get_topic() To get the topic
  275.      * @uses check_ajax_referer() To verify the nonce & check the referer
  276.      * @uses bbp_is_user_favorite() To check if the topic is user's favorite
  277.      * @uses bbp_remove_user_favorite() To remove the topic from user's favorites
  278.      * @uses bbp_add_user_favorite() To add the topic from user's favorites
  279.      */
  280.     public function ajax_favorite() {
  281.         $user_id = bbp_get_current_user_id();
  282.         $id      = intval( $_POST['id'] );
  283.  
  284.         if ( !current_user_can( 'edit_user', $user_id ) )
  285.             die( '-1' );
  286.  
  287.         if ( !$topic = bbp_get_topic( $id ) )
  288.             die( '0' );
  289.  
  290.         check_ajax_referer( 'toggle-favorite_' . $topic->ID );
  291.  
  292.         if ( bbp_is_user_favorite( $user_id, $topic->ID ) ) {
  293.             if ( bbp_remove_user_favorite( $user_id, $topic->ID ) ) {
  294.                 die( '1' );
  295.             }
  296.         } else {
  297.             if ( bbp_add_user_favorite( $user_id, $topic->ID ) ) {
  298.                 die( '1' );
  299.             }
  300.         }
  301.  
  302.         die( '0' );
  303.     }
  304.  
  305.     /**
  306.      * Subscribe/Unsubscribe a user from a topic
  307.      *
  308.      * @since bbPress (r2668)
  309.      *
  310.      * @uses bbp_is_subscriptions_active() To check if the subscriptions are active
  311.      * @uses bbp_get_current_user_id() To get the current user id
  312.      * @uses current_user_can() To check if the current user can edit the user
  313.      * @uses bbp_get_topic() To get the topic
  314.      * @uses check_ajax_referer() To verify the nonce & check the referer
  315.      * @uses bbp_is_user_subscribed() To check if the topic is in user's
  316.      *                                 subscriptions
  317.      * @uses bbp_remove_user_subscriptions() To remove the topic from user's
  318.      *                                        subscriptions
  319.      * @uses bbp_add_user_subscriptions() To add the topic from user's subscriptions
  320.      */
  321.     public function ajax_subscription() {
  322.         if ( !bbp_is_subscriptions_active() )
  323.             return;
  324.  
  325.         $user_id = bbp_get_current_user_id();
  326.         $id      = intval( $_POST['id'] );
  327.  
  328.         if ( !current_user_can( 'edit_user', $user_id ) )
  329.             die( '-1' );
  330.  
  331.         if ( !$topic = bbp_get_topic( $id ) )
  332.             die( '0' );
  333.  
  334.         check_ajax_referer( 'toggle-subscription_' . $topic->ID );
  335.  
  336.         if ( bbp_is_user_subscribed( $user_id, $topic->ID ) ) {
  337.             if ( bbp_remove_user_subscription( $user_id, $topic->ID ) ) {
  338.                 die( '1' );
  339.             }
  340.         } else {
  341.             if ( bbp_add_user_subscription( $user_id, $topic->ID ) ) {
  342.                 die( '1' );
  343.             }
  344.         }
  345.  
  346.         die( '0' );
  347.     }
  348. }
  349.  
  350. /**
  351.  * Instantiate a new BBP_Twenty_Ten class inside the $bbp global. It is
  352.  * responsible for hooking itself into WordPress where apprpriate.
  353.  */
  354. if ( 'bbPress' == get_class( $bbp ) ) {
  355.     $bbp->theme_compat->theme = new tumbleboard();
  356. }
  357.  
  358. endif;
  359.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement