/includes/bp-gifts-core.php

  1. <?php 
  2.  
  3.  
  4.  
  5. /** Define a constant that can be checked to see if the component is installed or not. */ 
  6.  
  7. define ( 'BP_GIFTS_IS_INSTALLED', 1 ); 
  8.  
  9.  
  10.  
  11. /** Define a constant that will hold the current version number of the component */ 
  12.  
  13. define ( 'BP_GIFTS_VERSION', '1.0' ); 
  14.  
  15.  
  16.  
  17. /** Define a constant that will hold the database version number that can be used for upgrading the DB 
  18. */ 
  19.  
  20. define ( 'BP_GIFTS_DB_VERSION', '1' ); 
  21.  
  22.  
  23.  
  24. /** Define a slug constant that will be used to view this components pages (http://example.org/SLUG) */ 
  25.  
  26. if ( !defined( 'BP_GIFTS_SLUG' ) ) 
  27.  
  28. define ( 'BP_GIFTS_SLUG', 'gifts' ); 
  29.  
  30.  
  31.  
  32. /** 
  33.   
  34. * If you want the users of your component to be able to change the values of your other custom constants,  
  35.   
  36. * you can use this code to allow them to add new definitions to the wp-config.php file and set the value there. 
  37.   
  38. */ 
  39.  
  40.  
  41.  
  42.  
  43.  
  44.  
  45. load_textdomain( 'bp-gifts', dirname( __FILE__ ) . '/languages/bp-gifts-' . get_locale() . '.mo' ); 
  46.  
  47. /** 
  48.   
  49. * The next step is to include all the files you need for your component. 
  50.   
  51. * You should remove or comment out any files that you don't need. 
  52.   
  53. */ 
  54.  
  55.  
  56.  
  57. /** The classes file should hold all database access classes and functions */ 
  58.  
  59. //require ( dirname( __FILE__ ) . '/bp-gifts-classes.php' ); 
  60.  
  61.  
  62.  
  63. /** The ajax file should hold all functions used in AJAX queries */ 
  64.  
  65. //require ( dirname( __FILE__ ) . '/bp-gifts-ajax.php' ); 
  66.  
  67.  
  68.  
  69. /** The cssjs file should set up and enqueue all CSS and JS files used by the component */ 
  70.  
  71. //require ( dirname( __FILE__ ) . '/bp-gifts-cssjs.php' ); 
  72.  
  73.  
  74. /** The notifications file should contain functions to send email notifications on specific user actions */ 
  75.  
  76. //require ( dirname( __FILE__ ) . '/bp-gifts-notifications.php' ); 
  77.  
  78.  
  79.  
  80. /** The filters file should create and apply filters to component output functions. */ 
  81.  
  82. //require ( dirname( __FILE__ ) . '/bp-gifts-filters.php' ); 
  83.  
  84.  
  85.  
  86. /** 
  87.   
  88. * bp_gifts_setup_globals() 
  89.   
  90. * 
  91.   
  92. * Sets up global variables for your component. 
  93.   
  94. */ 
  95.  
  96. function bp_gifts_setup_globals() { 
  97.  
  98. global $bp, $wpdb; 
  99.  
  100. $bp->gifts->id = 'gifts'; 
  101.  
  102. $bp->gifts->table_name = $wpdb->base_prefix . 'bp_gifts'; 
  103.  
  104. $bp->gifts->table_name_data = $wpdb->base_prefix . 'bp_gifts_data'; 
  105.  
  106. $bp->gifts->format_notification_function = 'bp_gifts_format_notifications'; 
  107.  
  108. $bp->gifts->slug = BP_GIFTS_SLUG; 
  109.  
  110. $bp->active_components[$bp->gifts->slug] = $bp->gifts->id; 
  111.  
  112. }  
  113.  
  114. /*** 
  115.   
  116. * In versions of BuddyPress 1.2.2 and newer you will be able to use: 
  117.   
  118. * add_action( 'bp_setup_globals', 'bp_gifts_setup_globals' ); 
  119.   
  120. */ 
  121. add_action( 'bp_setup_globals', 'bp_gifts_setup_globals' ); 
  122. add_action( 'wp', 'bp_gifts_setup_globals', 2 ); 
  123.  
  124. add_action( 'admin_menu', 'bp_gifts_setup_globals', 2 ); 
  125. add_action( 'network_admin_menu', 'bp_gifts_setup_globals', 2 ); 
  126.  
  127.  
  128.  
  129. /** 
  130.   
  131. * bp_gifts_add_admin_menu() 
  132.   
  133. * 
  134.   
  135. * This function will add a WordPress wp-admin admin menu for your component under the 
  136.   
  137. * "BuddyPress" menu. 
  138.   
  139. */ 
  140.  
  141. function bp_gifts_add_admin_menu() { 
  142.  
  143. global $bp; 
  144.  
  145. if( is_multisite() ) { 
  146. return; 
  147.  
  148. else 
  149. if ( !is_super_admin() ) { 
  150. return false; 
  151.  
  152. if ($bp->loggedin_user->is_site_admin) 
  153. //require ( dirname( __FILE__ ) . '/bp-gifts-admin.php' ); 
  154.  
  155. add_submenu_page( 'bp-general-settings', __( 'Buddypress Gifts', 'bp-gifts' ), __( 'Gifts Setup', 'bp-gifts' ), 'manage_options', 'bp-gifts-settings', 'bp_gifts_admin' ); 
  156.  
  157. add_action( 'admin_menu', 'bp_gifts_add_admin_menu' ); 
  158.  
  159.  
  160. function bp_gifts_add_admin_menu_network() { 
  161.  
  162. global $bp; 
  163.  
  164. if ( is_super_admin() ) { 
  165.  
  166. //require ( dirname( __FILE__ ) . '/bp-gifts-admin-network.php' ); 
  167.  
  168. add_submenu_page( 'bp-general-settings', __( 'Buddypress Gifts', 'bp-gifts' ), __( 'Gifts Setup', 'bp-gifts' ), 'manage_options', 'bp-gifts-settings', 'bp_gifts_admin' ); 
  169. else { 
  170.  
  171. return false; 
  172.  
  173.  
  174.  
  175. add_action( 'network_admin_menu', 'bp_gifts_add_admin_menu_network' ); 
  176.  
  177.  
  178.  
  179. /** 
  180.   
  181. * bp_gifts_setup_nav() 
  182.   
  183. * 
  184.   
  185. * Sets up the user profile navigation items for the component. This adds the top level nav 
  186.   
  187. * item and all the sub level nav items to the navigation array. This is then 
  188.   
  189. * rendered in the template. 
  190.   
  191. */ 
  192.  
  193. function bp_gifts_setup_nav() { 
  194.  
  195. global $bp; 
  196.  
  197.  
  198.  
  199. /** Add 'Gifts' to the main user profile navigation */ 
  200.  
  201. bp_core_new_nav_item( array( 
  202.  
  203. 'name' => __( 'Gifts', 'bp-gifts' ),  
  204.  
  205. 'slug' => $bp->gifts->slug,  
  206.  
  207. 'position' => 80,  
  208.  
  209. 'screen_function' => 'bp_gifts_screen',  
  210.  
  211. 'default_subnav_slug' => 'screen-one' 
  212.  
  213. ) ); 
  214.  
  215.  
  216.  
  217. $gifts_link = $bp->loggedin_user->domain . $bp->gifts->slug . '/'; 
  218.  
  219.  
  220.  
  221. /** Create two sub nav items for this component */ 
  222.  
  223. bp_core_new_subnav_item( array( 
  224.  
  225. 'name' => __( '', 'bp-gifts' ),  
  226.  
  227. 'slug' => 'screen-one',  
  228.  
  229. 'parent_slug' => $bp->gifts->slug,  
  230.  
  231. 'parent_url' => $gifts_link,  
  232.  
  233. 'screen_function' => 'bp_gifts_screen',  
  234.  
  235. 'position' => 10 
  236.  
  237. ) ); 
  238.  
  239.  
  240.  
  241.  
  242. /*** 
  243.   
  244. * In versions of BuddyPress 1.2.2 and newer you will be able to use: 
  245.   
  246. * add_action( 'bp_setup_nav', 'bp_gifts_setup_nav' ); 
  247.   
  248. */ 
  249.  
  250. add_action( 'bp_setup_nav', 'bp_gifts_setup_nav' ); 
  251. add_action( 'wp', 'bp_gifts_setup_nav', 2 ); 
  252.  
  253. add_action( 'admin_menu', 'bp_gifts_setup_nav', 2 ); 
  254. add_action( 'network_admin_menu', 'bp_gifts_setup_nav', 2 ); 
  255.  
  256.  
  257.  
  258. /** 
  259.   
  260. * bp_gifts_load_template_filter() 
  261.   
  262. * 
  263.   
  264. * You can define a custom load template filter for your component. This will allow 
  265.   
  266. * you to store and load template files from your plugin directory. 
  267.   
  268. */ 
  269.  
  270. function bp_gifts_load_template_filter( $found_template, $templates ) { 
  271.  
  272. global $bp; 
  273.  
  274.  
  275.  
  276. /** 
  277.   
  278. * Only filter the template location when we're on the gifts component pages. 
  279.   
  280. */ 
  281.  
  282. if ( $bp->current_component != $bp->gifts->slug ) 
  283.  
  284. return $found_template; 
  285.  
  286.  
  287.  
  288. foreach ( (array) $templates as $template ) { 
  289.  
  290. if ( file_exists( STYLESHEETPATH . '/' . $template ) ) 
  291.  
  292. $filtered_templates[] = STYLESHEETPATH . '/' . $template; 
  293.  
  294. else 
  295.  
  296. $filtered_templates[] = dirname( __FILE__ ) . '/templates/' . $template; 
  297.  
  298.  
  299.  
  300.  
  301. $found_template = $filtered_templates[0]; 
  302.  
  303.  
  304.  
  305. return apply_filters( 'bp_gifts_load_template_filter', $found_template ); 
  306.  
  307.  
  308. add_filter( 'bp_located_template', 'bp_gifts_load_template_filter', 10, 2 ); 
  309.  
  310.  
  311.  
  312.  
  313.  
  314. /******************************************************************************** 
  315.   
  316. * Screen Functions 
  317.   
  318. * 
  319.   
  320. * Screen functions are the controllers of BuddyPress. They will execute when their 
  321.   
  322. * specific URL is caught. They will first save or manipulate data using business 
  323.   
  324. * functions, then pass on the user to a template file. 
  325.   
  326. */ 
  327.  
  328.  
  329. function bp_gifts_screen() { 
  330.  
  331. global $bp; 
  332.  
  333.  
  334. /** remove all gift component notification */ 
  335.  
  336. if ($bp->loggedin_user->id == $bp->displayed_user->id) { 
  337.  
  338. bp_notifications_delete_notifications_by_type($bp->displayed_user->id, 'gifts', 'new_gifts'); 
  339.  
  340.  
  341.  
  342. /** Add a do action here, so your component can be extended by others. */ 
  343.  
  344. do_action( 'bp_gifts_screen_one' ); 
  345.  
  346.  
  347.  
  348. /**** 
  349.   
  350. * Displaying Content 
  351.   
  352. */ 
  353.  
  354.  
  355.  
  356. bp_core_load_template( apply_filters( 'bp_gifts_template_screen', 'gifts/screen' ) ); 
  357.  
  358.  
  359.  
  360.  
  361. function bp_gifts_screen_settings_menu() { 
  362.  
  363. global $bp, $current_user, $bp_settings_updated, $pass_error; 
  364.  
  365.  
  366.  
  367. if ( isset( $_POST['submit'] ) ) { 
  368.  
  369. /** Check the nonce */ 
  370.  
  371. check_admin_referer('bp-gifts-admin'); 
  372.  
  373.  
  374.  
  375. $bp_settings_updated = true; 
  376.  
  377.  
  378.  
  379. /** 
  380.   
  381. * This is when the user has hit the save button on their settings. 
  382.   
  383. * The best place to store these settings is in wp_usermeta. 
  384.   
  385. */ 
  386.  
  387. update_usermeta( $bp->loggedin_user->id, 'bp-gifts-option-one', attribute_escape( $_POST['bp-gifts-option-one'] ) ); 
  388.  
  389.  
  390.  
  391.  
  392. add_action( 'bp_template_content_header', 'bp_gifts_screen_settings_menu_header' ); 
  393.  
  394. add_action( 'bp_template_title', 'bp_gifts_screen_settings_menu_title' ); 
  395.  
  396. add_action( 'bp_template_content', 'bp_gifts_screen_settings_menu_content' ); 
  397.  
  398.  
  399.  
  400. bp_core_load_template( apply_filters( 'bp_core_template_plugin', 'plugin-template' ) ); 
  401.  
  402.  
  403.  
  404.  
  405. function bp_gifts_screen_settings_menu_header() { 
  406.  
  407. _e( 'Gifts Settings Header', 'bp-gifts' ); 
  408.  
  409.  
  410.  
  411.  
  412. function bp_gifts_screen_settings_menu_title() { 
  413.  
  414. _e( 'Gifts Settings', 'bp-gifts' ); 
  415.  
  416.  
  417.  
  418.  
  419. function bp_gifts_screen_settings_menu_content() { 
  420.  
  421. global $bp, $bp_settings_updated; ?> 
  422.  
  423.  
  424.  
  425. <?php if ( $bp_settings_updated ) { ?> 
  426.  
  427. <div id="message" class="updated fade"> 
  428.  
  429. <p><?php _e( 'Changes Saved.', 'bp-gifts' ) ?></p> 
  430.  
  431. </div> 
  432.  
  433. <?php } ?> 
  434.  
  435.  
  436.  
  437. <form action="<?php echo $bp->loggedin_user->domain . 'settings/gifts-admin'; ?>" name="bp-gifts-admin-form" id="account-delete-form" class="bp-gifts-admin-form" method="post"> 
  438.  
  439.  
  440.  
  441. <input type="checkbox" name="bp-gifts-option-one" id="bp-gifts-option-one" value="1"<?php if ( '1' == get_usermeta( $bp->loggedin_user->id, 'bp-gifts-option-one' ) ) : ?> checked="checked"<?php endif; ?> /> <?php _e( 'Do you love clicking checkboxes?', 'bp-gifts' ); ?> 
  442.  
  443. <p class="submit"> 
  444.  
  445. <input type="submit" value="<?php _e( 'Save Settings', 'bp-gifts' ) ?> »" id="submit" name="submit" /> 
  446.  
  447. </p> 
  448.  
  449.  
  450.  
  451. <?php 
  452.  
  453. /** This is very important, don't leave it out. */ 
  454.  
  455. wp_nonce_field( 'bp-gifts-admin' ); 
  456.  
  457. ?> 
  458.  
  459.  
  460.  
  461. </form> 
  462.  
  463. <?php 
  464.  
  465.  
  466.  
  467.  
  468.  
  469.  
  470. /******************************************************************************** 
  471.   
  472. * Activity & Notification Functions 
  473.   
  474. * 
  475.   
  476. * These functions handle the recording, deleting and formatting of activity and 
  477.   
  478. * notifications for the user and for this specific component. 
  479.   
  480. */ 
  481.  
  482.  
  483.  
  484.  
  485.  
  486. /** 
  487.   
  488. * bp_example_screen_notification_settings() 
  489.   
  490. * 
  491.   
  492. * Adds notification settings for the component, so that a user can turn off email 
  493.   
  494. * notifications set on specific component actions. 
  495.   
  496. */ 
  497.  
  498. function bp_gifts_screen_notification_settings() { 
  499.  
  500. global $current_user; 
  501.  
  502.  
  503. ?> 
  504.  
  505. <table class="notification-settings" id="bp-gifts-notification-settings"> 
  506. <thead> 
  507. <tr> 
  508.  
  509. <th class="icon"></th> 
  510.  
  511. <th class="title"><?php _e( 'Gifts', 'bp-gifts' ) ?></th> 
  512.  
  513. <th class="yes"><?php _e( 'Yes', 'bp-gifts' ) ?></th> 
  514.  
  515. <th class="no"><?php _e( 'No', 'bp-gifts' )?></th> 
  516.  
  517. </tr> 
  518. </thead> 
  519. <tbody> 
  520. <tr> 
  521.  
  522. <td></td> 
  523.  
  524. <td><?php _e( 'You received new gifts', 'bp-gifts' ) ?></td> 
  525.  
  526. <td class="yes"><input type="radio" name="notifications[notification_gifts_received]" value="yes" <?php if ( !get_usermeta( $bp->loggedin_user->id, 'notification_gifts_received') || 'yes' == get_usermeta( $bp->loggedin_user->id, 'notification_gifts_received') ) { ?>checked="checked" <?php } ?>/></td> 
  527.  
  528. <td class="no"><input type="radio" name="notifications[notification_gifts_received]" value="no" <?php if ( get_usermeta( $bp->loggedin_user->id, 'notification_gifts_received') == 'no' ) { ?>checked="checked" <?php } ?>/></td> 
  529.  
  530. </tr> 
  531. </tbody> 
  532.  
  533.  
  534. <?php do_action( 'bp_gifts_notification_settings' ); ?> 
  535.  
  536. </table> 
  537.  
  538. <?php 
  539.  
  540.  
  541. add_action( 'bp_notification_settings', 'bp_gifts_screen_notification_settings' ); 
  542.  
  543.  
  544. function bp_gifts_record_activity( $args = '' ) { 
  545.  
  546. global $bp; 
  547.  
  548.  
  549.  
  550. if ( !function_exists( 'bp_activity_add' ) ) 
  551.  
  552. return false; 
  553.  
  554.  
  555.  
  556. $defaults = array( 
  557.  
  558. 'id' => false,  
  559.  
  560. 'user_id' => $bp->loggedin_user->id,  
  561.  
  562. 'action' => '',  
  563.  
  564. 'content' => '',  
  565.  
  566. 'primary_link' => '',  
  567.  
  568. 'component' => 'gifts',  
  569.  
  570. 'type' => false,  
  571.  
  572. 'item_id' => false,  
  573.  
  574. 'secondary_item_id' => false,  
  575.  
  576. 'recorded_time' => gmdate( "Y-m-d H:i:s" ),  
  577.  
  578. 'hide_sitewide' => false 
  579.  
  580. ); 
  581.  
  582.  
  583.  
  584. $r = wp_parse_args( $args, $defaults ); 
  585.  
  586. extract( $r ); 
  587.  
  588.  
  589. return bp_activity_add( array( 'id' => $id, 'user_id' => $user_id, 'action' => $action, 'content' => $content, 'primary_link' => $primary_link, 'component' => $component, 'type' => $type, 'item_id' => $item_id, 'secondary_item_id' => $secondary_item_id, 'recorded_time' => $recorded_time, 'hide_sitewide' => $hide_sitewide ) ); 
  590.  
  591.  
  592. function bp_gifts_record_points() { 
  593.  
  594.  
  595. if(is_user_logged_in()) { 
  596. if( function_exists('cp_alterPoints')) { 
  597. cp_alterPoints(cp_currentUser(), -($_POST['point'])); 
  598. cp_log('Points Deducted for Gift', cp_currentUser(), -($_POST['point']), Gifts); 
  599. } else { 
  600. global $current_user; 
  601. get_currentuserinfo(); 
  602. //$user_id = bp_displayed_user_id(); 
  603. $user_id = $current_user->ID; 
  604. $mycred = mycred(); 
  605. $mycred->add_creds( 
  606. $user_id, // reference id 
  607. $user_id, // the user id 
  608. 0-($_POST['point']), // amount, negative value means deduction 
  609. 'Points Deducted for Gift' // Log entry 
  610. ); 
  611.  
  612.  
  613.  
  614. //add_action( 'bp_activity_add', 'bp_gifts_record_points'); 
  615.  
  616.  
  617. function bp_gifts_format_notifications( $action, $item_id, $secondary_item_id, $total_items ) { 
  618.  
  619. global $bp; 
  620.  
  621. switch ( $action ) { 
  622.  
  623. case 'new_gifts': 
  624.  
  625.  
  626.  
  627. if ( (int)$total_items > 1 ) { 
  628.  
  629. return apply_filters( 'bp_gifts_multiple_newgifts_notifications', '<a href="' . $bp->loggedin_user->domain . $bp->gifts->slug . '/" title="' . __( 'Multiple gifts', 'bp-gifts' ) . '">' . sprintf( __( '%d new gifts, multi-gifts!', 'bp-gifts' ), (int)$total_items ) . '</a>', $total_items ); 
  630.  
  631. } else { 
  632.  
  633. $user_fullname = bp_core_get_user_displayname( $item_id, false ); 
  634.  
  635. $user_url = bp_core_get_userlink( $item_id, true ); 
  636.  
  637. return apply_filters( 'bp_gifts_single_newgifts_notifications', '<a href="' . 'gifts' . '?new" title="' . $user_fullname .'\'s profile">' . sprintf( __( '%s sent you a high-five!', 'bp-gifts' ), $user_fullname ) . '</a>', $user_fullname ); 
  638.  
  639.  
  640.  
  641.  
  642. break; 
  643.  
  644.  
  645.  
  646.  
  647. do_action( 'bp_gifts_format_notifications', $action, $item_id, $secondary_item_id, $total_items ); 
  648.  
  649.  
  650.  
  651. return false; 
  652.  
  653.  
  654.  
  655.  
  656.  
  657.  
  658. /*** 
  659.   
  660. * From now on you will want to add your own functions that are specific to the component you are developing. 
  661.   
  662. * For example, in this section in the friends component, there would be functions like: 
  663.   
  664. */ 
  665.  
  666.  
  667.  
  668. function bp_gifts_send_giftsnotify( $to_user_id, $from_user_id ) { 
  669.  
  670. global $bp; 
  671.  
  672.  
  673.  
  674.  
  675.  
  676. bp_core_add_notification( $from_user_id, $to_user_id, 'gifts', 'new_gifts' ); 
  677.  
  678.  
  679.  
  680. do_action( 'bp_gifts_send_gifts', $to_user_id, $from_user_id ); 
  681.  
  682.  
  683.  
  684. return true; 
  685.  
  686.  
  687.  
  688. function bp_gifts_remove_data( $user_id ) { 
  689.  
  690. /** You'll want to run a function here that will delete all information from any component tables 
  691.   
  692. for this $user_id */ 
  693.  
  694.  
  695.  
  696. /** Remember to remove usermeta for this component for the user being deleted */ 
  697.  
  698. delete_usermeta( $user_id, 'notification_gifts_received' ); 
  699.  
  700.  
  701.  
  702. do_action( 'bp_gifts_remove_data', $user_id ); 
  703.  
  704.  
  705. add_action( 'wpmu_delete_user', 'bp_gifts_remove_data', 1 ); 
  706.  
  707. add_action( 'delete_user', 'bp_gifts_remove_data', 1 ); 
  708.  
  709.  
  710. function bp_gifts_root_slug() { 
  711. echo bp_get_gifts_root_slug(); 
  712. /** 
  713. * Return the gifts component root slug 
  714. * 
  715. */ 
  716. function bp_get_gifts_root_slug() { 
  717. global $bp; 
  718. return apply_filters( 'bp_get_gifts_root_slug', $bp->gifts->slug ); 
  719.  
  720.  
  721. ?> 
.