Skip to:
Content

BuddyPress.org

Ticket #2673: 2673.02.patch

File 2673.02.patch, 34.0 KB (added by boonebgorges, 10 years ago)
  • bp-groups/bp-groups-admin.php

    diff --git bp-groups/bp-groups-admin.php bp-groups/bp-groups-admin.php
    index faf7087..a82a978 100644
    function bp_groups_add_admin_menu() { 
    3939
    4040        // Hook into early actions to load custom CSS and our init handler.
    4141        add_action( "load-$hook", 'bp_groups_admin_load' );
     42
     43        /** Group Extensions management main page *****************/
     44        /*
     45        The goal of this page is to define the available default group extensions
     46        If a group extension is deactivated from this page it's always possible to activate it
     47        from group admin UI
     48        If a group extension is activated, it's always possible to deactivate it from the group
     49        admin UI
     50        When creating a group, the deactivated extensions won't be able to add their create step.
     51         */
     52        $extensions = add_submenu_page(
     53                'bp-groups',
     54                __( 'Extensions', 'buddypress' ),
     55                __( 'Extensions', 'buddypress' ),
     56                'bp_moderate',
     57                'bp-groups-extensions',
     58                'bp_groups_admin_extensions'
     59        );
     60
     61        add_action( "load-$extensions", 'bp_groups_admin_extensions_load' );
    4262}
    4363add_action( bp_core_admin_hook(), 'bp_groups_add_admin_menu' );
    4464
    function bp_groups_admin_load() { 
    119139                add_meta_box( 'bp_group_add_members', _x( 'Add New Members', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_add_new_members', get_current_screen()->id, 'normal', 'core' );
    120140                add_meta_box( 'bp_group_members', _x( 'Manage Members', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_members', get_current_screen()->id, 'normal', 'core' );
    121141
     142                /** Group extensions management meta box, these settings overide default ones ****/
     143                add_meta_box( 'bp_group_extensions', _x( 'Manage Group Extensions', 'group admin edit screen', 'buddypress' ), 'bp_groups_admin_edit_metabox_extensions', get_current_screen()->id, 'normal', 'low' );
     144
    122145                do_action( 'bp_groups_admin_meta_boxes' );
    123146
    124147                // Enqueue javascripts
    function bp_groups_admin_load() { 
    325348                        }
    326349                }
    327350
     351                // Process group extension settings
     352                if ( ! empty( $_POST['group-extensions-names'] ) && is_array( $_POST['group-extensions-names'] ) ) {
     353
     354                        $inactive_extensions = array();
     355
     356                        foreach ( $_POST['group-extensions-names'] as $extension ) {
     357                                if ( empty( $_POST['group-extensions'][ $extension ] ) ) {
     358                                        $inactive_extensions[ $extension ] = 1;
     359                                }
     360                        }
     361
     362                        groups_update_groupmeta( $group_id, 'inactive_extensions', $inactive_extensions );
     363                }
     364
    328365                // Call actions for plugins to do something before we redirect
    329366                do_action( 'bp_group_admin_edit_after', $group_id );
    330367
    function bp_groups_admin_edit_metabox_members( $item ) { 
    838875
    839876}
    840877
     878/** Group extensions management meta box ****/
     879
     880/**
     881 * Displays a metabox to overide group extensions default setting
     882 *
     883 * @param  BP_Groups_Group $item the group object
     884 * @uses   bp_groups_admin_extensions_build_list() to get the available group extensions
     885 * @uses   groups_extension_group_status() to check the group extension status (inactive or active for current group)
     886 * @uses   checked() to add a checked attribute when needed
     887 * @return string html output
     888 */
     889function bp_groups_admin_edit_metabox_extensions( $item ) {
     890        $group_extensions = bp_groups_admin_extensions_build_list();
     891        ?>
     892        <div class="bp-groups-extensions-manage">
     893                <p class="description"><?php _e( 'Activate the checkboxes for the extension you want to use in this group', 'buddypress' );?></p>
     894                <table class="widefat">
     895                        <thead>
     896                                <th><?php _e( 'Group Extensions', 'buddypress' );?></th>
     897                                <th><?php _e( 'Parent Plugin', 'buddypress' );?></th>
     898                                <th><?php _e( 'Status', 'buddypress' );?></th>
     899                        </thead>
     900                        <tbody>
     901                        <?php if( !is_array( $group_extensions ) && count( $group_extensions ) < 1) :?>
     902                                <tr><td colspan="3"><?php _e( 'No group extensions were found', 'buddypress' );?></td></tr>
     903                        <?php else:?>
     904
     905                                <?php foreach( $group_extensions as $extension_slug => $extension ) : ?>
     906                                        <?php $is_active_for_group = bp_groups_is_extension_active_for_group( $extension_slug ) ?>
     907                                        <tr>
     908                                                <td>
     909                                                        <input type="checkbox" value="1" name="group-extensions[<?php echo esc_attr( $extension_slug ) ?>]" <?php checked( $is_active_for_group, true );?>/> <strong><?php echo $extension['class_name'];?></strong>
     910                                                        <input type="hidden" value="<?php echo esc_attr( $extension_slug ) ?>" name="group-extensions-names[]"/>
     911                                                </td>
     912                                                <td><?php echo $extension['parent_plugin_data']['Name'];?></td>
     913                                                <td>
     914                                                        <?php if( $is_active_for_group ):?>
     915                                                                <?php /* todo this should be done with CSS */ ?>
     916                                                                <strong style="color:green"><?php _e( 'Active', 'buddypress' ) ?></strong>
     917                                                        <?php else:?>
     918                                                                <strong style="color:red"><?php _e( 'Inactive', 'buddypress' ) ?></strong>
     919                                                        <?php endif;?>
     920                                                </td>
     921                                        </tr>
     922                                <?php endforeach;?>
     923
     924                        <?php endif;?>
     925                        </tbody>
     926                </table>
     927        </div>
     928        <?php
     929}
     930
    841931/**
    842932 * Status metabox for the Groups admin edit screen
    843933 *
    class BP_Groups_List_Table extends WP_List_Table { 
    14121502                echo apply_filters_ref_array( 'bp_groups_admin_get_group_last_active', array( $last_active, $item ) );
    14131503        }
    14141504}
     1505
     1506/** Group extensions management specific functions ****/
     1507
     1508/**
     1509 * Prints some style in Group Management page header
     1510 *
     1511 * @return string css styles
     1512 */
     1513function bp_groups_extensions_admin_cssjs() {
     1514        ?>
     1515        <style>
     1516        table.widefat span.deactivate a.delete{
     1517                color:#bc0b0b;
     1518        }
     1519        .extensions tr {
     1520                background-color: #fcfcfc;
     1521        }
     1522        .extensions .inactive {
     1523                background-color: #f4f4f4;
     1524        }
     1525        </style>
     1526        <?php
     1527}
     1528
     1529/**
     1530 * Group Extensions management process
     1531 *
     1532 * Setting default options for group extensions, these settings can be overidden by each group
     1533 * from group admin ui
     1534 *
     1535 * @global  WP_List_Table $bp_groups_extensions_list_table
     1536 * @global  string $status status of the extensions (all, inactive, active)
     1537 * @global  integer $page the current number of page
     1538 * @uses    BP_Groups_Extensions_List_Table to build the html table of available extensions
     1539 * @uses    get_current_screen() to get current admin screen attributes
     1540 * @uses    add_action() to load some style in page header
     1541 * @uses    bp_get_admin_url() to build the url of this current page
     1542 * @uses    remove_query_arg() to strip some args from the requested url
     1543 * @uses    current_user_can() to check for current user's capability
     1544 * @uses    wp_die() to eventually kill the process if user has not enought capability
     1545 * @uses    check_admin_referer() for security reasons
     1546 * @uses    bp_groups_extensions_set_inactive() to set a group extension as inactive
     1547 * @uses    bp_groups_extensions_set_active() to set a group extension as active
     1548 * @uses    wp_redirect() to safely redirect user after settings are saved
     1549 * @return  string html output
     1550 */
     1551function bp_groups_admin_extensions_load() {
     1552        global $bp_groups_extensions_list_table, $status, $page;
     1553
     1554        $bp_groups_extensions_list_table = new BP_Groups_Extensions_List_Table();
     1555
     1556        // Help panel - overview text
     1557        get_current_screen()->add_help_tab( array(
     1558                'id'      => 'bp-groups-extensions-overview',
     1559                'title'   => __( 'Overview', 'buddypress' ),
     1560                'content' =>
     1561                        '<p>' . __( 'You can manage group extensions much like you can manage plugins. You can act on available default group extensions by using the on-hover action links or the Bulk Actions.', 'buddypress' ) . '</p>',
     1562        ) );
     1563
     1564        get_current_screen()->add_help_tab( array(
     1565                'id'      => 'bp-groups-overview-actions',
     1566                'title'   => __( 'Group Extensions Actions', 'buddypress' ),
     1567                'content' =>
     1568                        '<p>' . __( 'Clicking "Deactivate" will disable the group extension. Newly created group will not be able to use the extension. In the Group Admin, you can still activate the extension for the group being edited', 'buddypress' ) . '</p>' .
     1569                        '<p>' . __( 'Clicking "Activate" will bring back a group extension previously deactivated. In the Group Admin, you can still deactivate the extension for the group being edited.', 'buddypress' ) . '</p>' .
     1570                        '<p>' . __( 'If you select a number of extensions and then choose Deactivate or Activate from the Bulk Actions menu, you will be able to change the selected extensions status at once', 'buddypress' ) . '</p>',
     1571        ) );
     1572
     1573        // Help panel - sidebar links
     1574        get_current_screen()->set_help_sidebar(
     1575                '<p><strong>' . __( 'For more information:', 'buddypress' ) . '</strong></p>' .
     1576                '<p>' . __( '<a href="http://buddypress.org/support/">Support Forums</a>', 'buddypress' ) . '</p>'
     1577        );
     1578
     1579        add_action( 'admin_print_styles-' . get_current_screen()->id, 'bp_groups_extensions_admin_cssjs' );
     1580
     1581        $pagenum = $bp_groups_extensions_list_table->get_pagenum();
     1582
     1583        $action = $bp_groups_extensions_list_table->current_action();
     1584
     1585        // Build redirection URL
     1586        $redirect_to = bp_get_admin_url( 'admin.php?page=bp-groups-extensions' );
     1587
     1588        $extension = isset($_REQUEST['extension']) ? $_REQUEST['extension'] : '';
     1589        $s = isset($_REQUEST['s']) ? urlencode($_REQUEST['s']) : '';
     1590
     1591        // Clean up request URI from temporary args for screen options/paging uri's to work as expected.
     1592        $_SERVER['REQUEST_URI'] = remove_query_arg( array( 'error', 'activate', 'deactivate', 'activate-multi', 'deactivate-multi', '_error_nonce' ), $_SERVER['REQUEST_URI'] );
     1593
     1594        // redirect !
     1595        if( ! empty( $action ) ) {
     1596
     1597                switch( $action ) {
     1598
     1599                        case 'deactivate' :
     1600                                if ( ! current_user_can('bp_moderate') )
     1601                                        wp_die( __('You do not have sufficient permissions to deactivate group extensions for this site.', 'buddypress') );
     1602
     1603                                check_admin_referer( 'bp-groups-extensions-deactivate' );
     1604
     1605                                $deactivated = bp_groups_extensions_set_inactive( $extension );
     1606
     1607                                wp_redirect( "$redirect_to&deactivate=$deactivated&extension_status=$status&paged=$page&s=$s" );
     1608                                exit;
     1609
     1610                                break;
     1611
     1612                        case 'activate' :
     1613                                if ( ! current_user_can('bp_moderate') )
     1614                                        wp_die( __('You do not have sufficient permissions to activate group extensions for this site.', 'buddypress') );
     1615
     1616                                check_admin_referer( 'bp-groups-extensions-activate' );
     1617
     1618                                $activated = bp_groups_extensions_set_active( $extension );
     1619
     1620                                wp_redirect( "$redirect_to&activate=$activated&extension_status=$status&paged=$page&s=$s" );
     1621                                exit;
     1622
     1623                                break;
     1624
     1625                        case 'deactivate-selected' :
     1626                                if ( ! current_user_can('bp_moderate') )
     1627                                        wp_die( __('You do not have sufficient permissions to deactivate group extensions for this site.', 'buddypress') );
     1628
     1629                                check_admin_referer( 'bulk-extensions' );
     1630
     1631                                $extensions = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
     1632
     1633                                if ( empty( $extensions ) ) {
     1634                                        wp_redirect( "$redirect_to&extension_status=$status&paged=$page&s=$s" );
     1635                                        exit;
     1636                                }
     1637
     1638                                $deactivated = bp_groups_extensions_set_inactive( $extensions );
     1639
     1640                                wp_redirect( "$redirect_to&deactivate-multi=$deactivated&extension_status=$status&paged=$page&s=$s" );
     1641                                exit;
     1642
     1643                                break;
     1644
     1645                        case 'activate-selected' :
     1646                                if ( ! current_user_can('bp_moderate') )
     1647                                        wp_die( __('You do not have sufficient permissions to deactivate group extensions for this site.', 'buddypress') );
     1648
     1649                                check_admin_referer( 'bulk-extensions' );
     1650
     1651                                $extensions = isset( $_POST['checked'] ) ? (array) $_POST['checked'] : array();
     1652
     1653                                if ( empty( $extensions ) ) {
     1654                                        wp_redirect( "$redirect_to&extension_status=$status&paged=$page&s=$s" );
     1655                                        exit;
     1656                                }
     1657
     1658                                $activated = bp_groups_extensions_set_active( $extensions );
     1659
     1660                                wp_redirect( "$redirect_to&activate-multi=$activated&extension_status=$status&paged=$page&s=$s" );
     1661                                exit;
     1662
     1663                                break;
     1664
     1665                }
     1666
     1667        }
     1668
     1669}
     1670
     1671/**
     1672 * Merges the extension's plugin info
     1673 *
     1674 * @param  array  $extensions_dir the calculated extensions plugin directories
     1675 * @uses   get_plugins() to get the installed plugins
     1676 * @return array                 plugin's info
     1677 */
     1678function bp_groups_admin_extensions_get_parent_plugin_infos( $extensions_dir = array() ) {
     1679        $all_plugins = get_plugins();
     1680        $parent_plugin_infos = array();
     1681
     1682        $plugin_keys = array_keys( $all_plugins );
     1683
     1684        foreach( $extensions_dir as $key => $extension ) {
     1685                foreach( $plugin_keys as $plugin_key ) {
     1686                        if( strpos( $plugin_key, $extension ) !== false ) {
     1687                                $parent_plugin_infos[$key]['parent_plugin'] = $plugin_key;
     1688                                $parent_plugin_infos[$key]['parent_plugin_data'] = $all_plugins[$plugin_key];
     1689                        }
     1690
     1691                }
     1692        }
     1693
     1694        return $parent_plugin_infos;
     1695}
     1696
     1697/**
     1698 * Builds the list of available group extensions
     1699 *
     1700 * @uses   bp_groups_extension_get_plugin_basename() to get parent plugin's folder
     1701 * @uses   bp_groups_admin_extensions_get_parent_plugin_infos() to merge plugin's infos
     1702 * @return array the list of group extensions
     1703 */
     1704function bp_groups_admin_extensions_build_list() {
     1705        global $wp_filter;
     1706
     1707        $extensions = array();
     1708        $all_plugins = get_plugins();
     1709
     1710        foreach ( buddypress()->groups->extensions as $extension_slug => $extension ) {
     1711                $extensions[ $extension->slug ] = array(
     1712                        'settings' => array(
     1713                                'create' => $extension->screens['create']['enabled'],
     1714                                'edit' => $extension->screens['edit']['enabled'],
     1715                                'admin' => $extension->screens['admin']['enabled'],
     1716                        ),
     1717                        'class_name' => $extension->class_name,
     1718                );
     1719
     1720                // Find the parent plugin info
     1721                foreach ( $all_plugins as $plugin_key => $plugin ) {
     1722                        if ( 0 === strpos( $plugin_key, $extension->plugin_basename . '/' ) ) {
     1723                                $extensions[ $extension->slug ]['parent_plugin'] = $plugin_key;
     1724                                $extensions[ $extension->slug ]['parent_plugin_data'] = $plugin;
     1725                        }
     1726                }
     1727        }
     1728
     1729        return $extensions;
     1730}
     1731
     1732/**
     1733 * Main page to manage the Group Extensions
     1734 *
     1735 * @global WP_List_Table $bp_groups_extensions_list_table
     1736 * @global string $status (active, inactive or all)
     1737 * @global integer $page the current page number
     1738 * @uses   screen_icon() to output groups logo
     1739 * @return string html output
     1740 */
     1741function bp_groups_admin_extensions() {
     1742        global $bp_groups_extensions_list_table, $status, $page;
     1743
     1744        $bp_groups_extensions_list_table->prepare_items();
     1745
     1746        if ( isset($_GET['activate']) ) : ?>
     1747                <div id="message" class="updated"><p><?php _e( 'Group extension <strong>activated</strong>.', 'buddypress' ) ?></p></div>
     1748        <?php elseif (isset($_GET['activate-multi'])) : ?>
     1749                <div id="message" class="updated"><p><?php _e( 'Selected Group extensions <strong>activated</strong>.', 'buddypress' ); ?></p></div>
     1750        <?php elseif ( isset($_GET['deactivate']) ) : ?>
     1751                <div id="message" class="updated"><p><?php _e( 'Group extension <strong>deactivated</strong>.', 'buddypress' ) ?></p></div>
     1752        <?php elseif (isset($_GET['deactivate-multi'])) : ?>
     1753                <div id="message" class="updated"><p><?php _e( 'Selected Group extensions <strong>deactivated</strong>.', 'buddypress' ); ?></p></div>
     1754        <?php endif; ?>
     1755
     1756        <div class="wrap">
     1757                <?php screen_icon( 'buddypress-groups' ); ?>
     1758                <h2><?php _e( 'Group Extensions Management', 'buddypress' ) ?></h2>
     1759
     1760                <?php $bp_groups_extensions_list_table->views(); ?>
     1761
     1762                <form method="get" action="">
     1763                        <?php $bp_groups_extensions_list_table->search_box( __( 'Search Group extensions' ), 'extension' ); ?>
     1764                </form>
     1765
     1766                <form method="post" action="">
     1767
     1768                <input type="hidden" name="plugin_status" value="<?php echo esc_attr($status) ?>" />
     1769                <input type="hidden" name="paged" value="<?php echo esc_attr($page) ?>" />
     1770
     1771                <?php $bp_groups_extensions_list_table->display(); ?>
     1772                </form>
     1773
     1774        </div>
     1775        <?php
     1776}
     1777
     1778/**
     1779 * Sets a group extension as active
     1780 *
     1781 * @param  string $class the name of the group extension class
     1782 * @uses   bp_get_option() to get previously saved setting
     1783 * @uses   bp_update_option() to save the new settings
     1784 * @uses   bp_delete_option() to eventually delete the settings
     1785 * @return boolean true|false
     1786 */
     1787function bp_groups_extensions_set_active( $class = '' ) {
     1788        if( empty( $class ) )
     1789                return false;
     1790
     1791        $inactive_extensions = bp_get_option( 'bp_inactive_group_extensions', array() );
     1792
     1793        // forcing array to use same function in case of bulk action
     1794        foreach( (array) $class as $extension ) {
     1795                if( in_array( $extension, $inactive_extensions ) )
     1796                        $inactive_extensions = array_diff( $inactive_extensions, array( $extension ) );
     1797        }
     1798
     1799        if( count( $inactive_extensions ) > 0 )
     1800                bp_update_option( 'bp_inactive_group_extensions', $inactive_extensions );
     1801
     1802        else
     1803                bp_delete_option( 'bp_inactive_group_extensions' );
     1804
     1805        return true;
     1806}
     1807
     1808/**
     1809 * Sets a group extension as inactive
     1810 *
     1811 * @param  string $class the name of the group extension class
     1812 * @uses   bp_get_option() to get previously saved setting
     1813 * @uses   bp_update_option() to save the new settings
     1814 * @uses   sanitize_text_field() to sanitize class before inserting it in db
     1815 * @return boolean true|false
     1816 */
     1817function bp_groups_extensions_set_inactive( $class = '' ) {
     1818        if( empty( $class ) )
     1819                return false;
     1820
     1821        $inactive_extensions = bp_get_option( 'bp_inactive_group_extensions', array() );
     1822
     1823
     1824        // forcing array to use same function in case of bulk action
     1825        foreach( (array) $class as $extension ) {
     1826                if( !in_array( $extension, $inactive_extensions ) )
     1827                        $inactive_extensions[] = sanitize_text_field( $extension );
     1828        }
     1829
     1830        if( count( $inactive_extensions ) > 0 )
     1831                bp_update_option( 'bp_inactive_group_extensions', $inactive_extensions );
     1832
     1833        return true;
     1834}
     1835
     1836/**
     1837 * Group extensions list table
     1838 */
     1839class BP_Groups_Extensions_List_Table extends WP_List_Table {
     1840
     1841        function __construct( $args = array() ) {
     1842                global $status, $page;
     1843
     1844                parent::__construct( array(
     1845                        'ajax'     => false,
     1846                        'plural'   => 'extensions',
     1847                        'singular' => 'extension',
     1848                ) );
     1849
     1850                $status = 'all';
     1851                if ( isset( $_REQUEST['extension_status'] ) && in_array( $_REQUEST['extension_status'], array( 'active', 'inactive', 'search' ) ) )
     1852                        $status = $_REQUEST['extension_status'];
     1853
     1854                if ( isset($_REQUEST['s']) )
     1855                        $_SERVER['REQUEST_URI'] = add_query_arg('s', stripslashes_deep($_REQUEST['s']) );
     1856
     1857                $page = $this->get_pagenum();
     1858        }
     1859
     1860        function get_table_classes() {
     1861                return array( 'widefat', $this->_args['plural'] );
     1862        }
     1863
     1864        function ajax_user_can() {
     1865                return current_user_can('bp_moderate');
     1866        }
     1867
     1868        function prepare_items() {
     1869                global $status, $extensions, $totals, $page, $orderby, $order, $s;
     1870
     1871                wp_reset_vars( array( 'orderby', 'order', 's' ) );
     1872
     1873                $extensions = array(
     1874                        'all' => apply_filters( 'all_groups_extensions', bp_groups_admin_extensions_build_list() ),
     1875                        'search' => array(),
     1876                        'active' => array(),
     1877                        'inactive' => array()
     1878                );
     1879
     1880                $screen = $this->screen;
     1881
     1882                $extensions['active'] = (array) $extensions['all'];
     1883
     1884                foreach ( (array) $extensions['all'] as $extension_slug => $extension_data ) {
     1885                        if ( bp_groups_is_extension_active( $extension_slug ) ) {
     1886                                unset( $extensions['active'][$extension_slug] );
     1887                                $extensions['inactive'][$extension_slug] = $extension_data;
     1888                        }
     1889                }
     1890
     1891                if ( $s ) {
     1892                        $status = 'search';
     1893                        $extensions['search'] = array_filter( $extensions['all'], array( &$this, '_search_callback' ) );
     1894                }
     1895
     1896                $totals = array();
     1897                foreach ( $extensions as $type => $list )
     1898                        $totals[ $type ] = count( $list );
     1899
     1900                if ( empty( $plugins[ $status ] ) && !in_array( $status, array( 'all', 'active', 'inactive', 'search' ) ) )
     1901                        $status = 'all';
     1902
     1903                $this->items = array();
     1904                foreach ( $extensions[ $status ] as $extension_slug => $extension_data ) {
     1905                        // Translate, Don't Apply Markup, Sanitize HTML
     1906                        $this->items[ $extension_slug ] = array_merge( array( 'Extension Name' => $extension_data['class_name'] ), _get_plugin_data_markup_translate( $extension_data['parent_plugin'], $extension_data['parent_plugin_data'], false, true ) );
     1907                }
     1908
     1909                $total_this_page = $totals[ $status ];
     1910
     1911                if ( $orderby ) {
     1912                        $orderby = ucfirst( $orderby );
     1913                        $order = strtoupper( $order );
     1914
     1915                        uasort( $this->items, array( &$this, '_order_callback' ) );
     1916                }
     1917
     1918                $extensions_per_page = $this->get_items_per_page( str_replace( '-', '_', $screen->id . '_per_page' ), 20 );
     1919
     1920                $start = ( $page - 1 ) * $extensions_per_page;
     1921
     1922                if ( $total_this_page > $extensions_per_page )
     1923                        $this->items = array_slice( $this->items, $start, $extensions_per_page );
     1924
     1925                $this->set_pagination_args( array(
     1926                        'total_items' => $total_this_page,
     1927                        'per_page' => $extensions_per_page,
     1928                ) );
     1929        }
     1930
     1931        function _search_callback( $extensions ) {
     1932                static $term;
     1933                if ( is_null( $term ) )
     1934                        $term = stripslashes_deep( $_REQUEST['s'] );
     1935
     1936                foreach ( $extensions as $key => $value ) {
     1937
     1938                        switch( $key ){
     1939                                case 'settings' :
     1940                                case 'parent_plugin' :
     1941                                        continue;
     1942                                break;
     1943
     1944                                case 'class_name' :
     1945                                        if ( stripos( $value, $term ) !== false )
     1946                                                return true;
     1947                                break;
     1948
     1949                                case 'parent_plugin_data' :
     1950                                        if ( stripos( $value['Name'], $term ) !== false )
     1951                                                return true;
     1952                                break;
     1953                        }
     1954                }
     1955
     1956                return false;
     1957        }
     1958
     1959        function _order_callback( $extension_a, $extension_b ) {
     1960                global $orderby, $order;
     1961
     1962                $a = $extension_a[$orderby];
     1963                $b = $extension_b[$orderby];
     1964
     1965                if ( $a == $b )
     1966                        return 0;
     1967
     1968                if ( 'DESC' == $order )
     1969                        return ( $a < $b ) ? 1 : -1;
     1970                else
     1971                        return ( $a < $b ) ? -1 : 1;
     1972        }
     1973
     1974        function no_items() {
     1975                global $extensions, $status;
     1976
     1977                if ( !empty( $extensions ) ) {
     1978
     1979                        switch( $status ) {
     1980                                case 'inactive' :
     1981                                        $text = __( 'inactive', 'buddypress' );
     1982                                        break;
     1983                                case 'active' :
     1984                                        $text = __( 'active', 'buddypress' );
     1985                                        break;
     1986                                case 'all' :
     1987                                default:
     1988                                        $text = '';
     1989                                        break;
     1990                        }
     1991
     1992                        printf( __( 'No %s group extensions found.', 'buddypress' ), $text );
     1993                } else
     1994                        _e( 'You do not appear to have any group extensions available at this time.', 'buddypress' );
     1995        }
     1996
     1997        /**
     1998         * Display the search box.
     1999         *
     2000         * @since 3.1.0
     2001         * @access public
     2002         *
     2003         * @param string $text The search button text
     2004         * @param string $input_id The search input id
     2005         */
     2006        function search_box( $text, $input_id ) {
     2007                if ( empty( $_REQUEST['s'] ) && !$this->has_items() )
     2008                        return;
     2009
     2010                $input_id = $input_id . '-search-input';
     2011
     2012                if ( ! empty( $_REQUEST['orderby'] ) )
     2013                        echo '<input type="hidden" name="orderby" value="' . esc_attr( $_REQUEST['orderby'] ) . '" />';
     2014                if ( ! empty( $_REQUEST['order'] ) )
     2015                        echo '<input type="hidden" name="order" value="' . esc_attr( $_REQUEST['order'] ) . '" />';
     2016?>
     2017<p class="search-box">
     2018        <label class="screen-reader-text" for="<?php echo $input_id ?>"><?php echo $text; ?>:</label>
     2019        <input type="hidden" name="page" value="bp-groups-extensions"/>
     2020        <input type="search" id="<?php echo $input_id ?>" name="s" value="<?php _admin_search_query(); ?>" placeholder="<?php _e( 'Plugin or Group Extension', 'buddypress' );?>" />
     2021        <?php submit_button( $text, 'button', false, false, array('id' => 'search-submit') ); ?>
     2022</p>
     2023<?php
     2024        }
     2025
     2026        function get_columns() {
     2027                global $status;
     2028
     2029                return array(
     2030                        'cb'          => '<input type="checkbox" />',
     2031                        'name'        => __( 'Group Extensions', 'buddypress' ),
     2032                        'description' => __( 'Plugin Description', 'buddypress' )
     2033                );
     2034        }
     2035
     2036        function get_sortable_columns() {
     2037                return array();
     2038        }
     2039
     2040        function get_views() {
     2041                global $totals, $status;
     2042
     2043                $url_base = bp_get_admin_url( 'admin.php?page=bp-groups-extensions' );
     2044
     2045                $status_links = array();
     2046                foreach ( $totals as $type => $count ) {
     2047                        if ( !$count )
     2048                                continue;
     2049
     2050                        switch ( $type ) {
     2051                                case 'all':
     2052                                        $text = _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $count, 'group extensions' );
     2053                                        break;
     2054                                case 'active':
     2055                                        $text = _n( 'Active <span class="count">(%s)</span>', 'Active <span class="count">(%s)</span>', $count );
     2056                                        break;
     2057                                case 'inactive':
     2058                                        $text = _n( 'Inactive <span class="count">(%s)</span>', 'Inactive <span class="count">(%s)</span>', $count );
     2059                                        break;
     2060                        }
     2061
     2062                        if ( 'search' != $type ) {
     2063                                $status_links[$type] = sprintf( "<a href='%s' %s>%s</a>",
     2064                                        add_query_arg('extension_status', $type, $url_base),
     2065                                        ( $type == $status ) ? ' class="current"' : '',
     2066                                        sprintf( $text, number_format_i18n( $count ) )
     2067                                        );
     2068                        }
     2069                }
     2070
     2071                return $status_links;
     2072        }
     2073
     2074        function get_bulk_actions() {
     2075                global $status;
     2076
     2077                $actions = array();
     2078
     2079                if ( 'active' != $status )
     2080                        $actions['activate-selected'] = __( 'Activate' );
     2081
     2082                if ( 'inactive' != $status && 'recent' != $status )
     2083                        $actions['deactivate-selected'] = __( 'Deactivate' );
     2084
     2085
     2086                return $actions;
     2087        }
     2088
     2089        function bulk_actions() {
     2090                global $status;
     2091
     2092                parent::bulk_actions();
     2093        }
     2094
     2095
     2096        function current_action() {
     2097                return parent::current_action();
     2098        }
     2099
     2100        function display_rows() {
     2101                global $status;
     2102
     2103                foreach ( $this->items as $extension_slug => $extension_data ) {
     2104                        $this->single_row( $extension_slug, $extension_data );
     2105                }
     2106        }
     2107
     2108        function single_row( $extension_slug, $extension_data ) {
     2109                global $status, $page, $s, $totals;
     2110
     2111                $context = $status;
     2112                $screen = $this->screen;
     2113                $base_url = bp_get_admin_url( 'admin.php?page=bp-groups-extensions' );
     2114
     2115                // preorder
     2116                $actions = array(
     2117                        'activate' => '',
     2118                        'deactivate' => '',
     2119                );
     2120
     2121                $is_inactive = ! bp_groups_is_extension_active( $extension_slug );
     2122
     2123                if ( $is_inactive ) {
     2124                        $activate_url = wp_nonce_url( $base_url . '&amp;action=activate&amp;extension=' . $extension_slug .'&amp;extension_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bp-groups-extensions-activate' );
     2125                        $actions['activate'] = '<a href="' . $activate_url . '" title="' . esc_attr__('Activate this extension', 'buddypress') . '">' . __('Activate', 'buddypress') . '</a>';
     2126                } else {
     2127                        $deactivate_url = wp_nonce_url( $base_url . '&amp;action=deactivate&amp;extension='. $extension_slug .'&amp;extension_status=' . $context . '&amp;paged=' . $page . '&amp;s=' . $s, 'bp-groups-extensions-deactivate' );
     2128                        $actions['deactivate'] = '<a href="' . $deactivate_url . '" title="' . esc_attr__('Deactivate this extension', 'buddypress') . '" class="delete">' . __('Deactivate', 'buddypress') . '</a>';
     2129                }
     2130
     2131                $actions = apply_filters( 'bp_groups_extensions_action_links', array_filter( $actions ), $extension_data, $context );
     2132
     2133                 // end if $context
     2134                $checkbox_id =  "checkbox_" . md5($extension_data['Extension Name']);
     2135
     2136                $checkbox = "<label class='screen-reader-text' for='" . $checkbox_id . "' >" . sprintf( __( 'Select %s' ), $extension_data['Extension Name'] ) . "</label>"
     2137                                . "<input type='checkbox' name='checked[]' value='" . esc_attr( $extension_slug ) . "' id='" . $checkbox_id . "' />";
     2138
     2139                $description = '<p>' . ( $extension_data['Description'] ? $extension_data['Description'] : '&nbsp;' ) . '</p>';
     2140
     2141                $extension_name = $extension_data['Extension Name'];
     2142                $plugin_name = $extension_data['Name'];
     2143
     2144                $id = sanitize_title( $extension_name );
     2145                $class = $is_inactive ? 'inactive' : 'active' ;
     2146
     2147                echo "<tr id='$id' class='$class'>";
     2148
     2149                list( $columns, $hidden ) = $this->get_column_info();
     2150
     2151                foreach ( $columns as $column_name => $column_display_name ) {
     2152                        $style = '';
     2153                        if ( in_array( $column_name, $hidden ) )
     2154                                $style = ' style="display:none;"';
     2155
     2156                        switch ( $column_name ) {
     2157                                case 'cb':
     2158                                        echo "<th scope='row' class='check-column'>$checkbox</th>";
     2159                                        break;
     2160                                case 'name':
     2161                                        echo "<td class='plugin-title'$style><strong>$extension_name</strong>";
     2162                                        echo $this->row_actions( $actions, true );
     2163                                        echo "</td>";
     2164                                        break;
     2165                                case 'description':
     2166                                        echo "<td class='column-description desc'$style>
     2167                                                <div class='plugin-description'><strong>$plugin_name</strong>$description</div></td>";
     2168                                        break;
     2169                                default:
     2170                                        echo "<td class='$column_name column-$column_name'$style>";
     2171                                        do_action( 'manage_plugins_custom_column', $column_name, $extension_data );
     2172                                        echo "</td>";
     2173                        }
     2174                }
     2175
     2176                echo "</tr>";
     2177
     2178                do_action( 'bp_groups_extensions_after_extension_row', $extension_data, $status );
     2179        }
     2180}
  • bp-groups/bp-groups-classes.php

    diff --git bp-groups/bp-groups-classes.php bp-groups/bp-groups-classes.php
    index 1603327..a854413 100644
    class BP_Group_Extension { 
    20352035                // Mirror configuration data so it's accessible to plugins
    20362036                // that look for it in its old locations
    20372037                $this->setup_legacy_properties();
     2038        }
     2039
     2040        /**
     2041         * Hook the extension into BuddyPress for display
     2042         *
     2043         * @since BuddyPress (1.9)
     2044         */
     2045        public function setup_hooks() {
     2046                if ( ! bp_groups_is_extension_active_for_group( $this->slug ) ) {
     2047                        return;
     2048                }
    20382049
    2039                 // Hook the extension into BuddyPress
    20402050                $this->setup_display_hooks();
    20412051                $this->setup_create_hooks();
    20422052                $this->setup_edit_hooks();
    class BP_Group_Extension { 
    20602070                if ( is_null( $this->class_reflection ) ) {
    20612071                        $this->class_reflection = new ReflectionClass( $this->class_name );
    20622072                }
     2073
     2074                if ( empty( $this->class_file ) ) {
     2075                        $this->class_file = $this->class_reflection->getFileName();
     2076                }
     2077
     2078                if ( empty( $this->plugin_basename ) ) {
     2079                        // Strip everything after the first slash to get the basename
     2080                        $file_basename = plugin_basename( $this->class_file );
     2081                        $this->plugin_basename = substr( $file_basename, 0, strpos( $file_basename, '/' ) );
     2082                }
    20632083        }
    20642084
    20652085        /**
    class BP_Group_Extension { 
    25262546                        $enabled = $this->screens[ $context ]['enabled'] && is_callable( $this->screens[ $context ]['screen_callback'] );
    25272547                }
    25282548
    2529                 return (bool) $enabled;
     2549                /** adding a filter here to eventually disallow extension admin screen **/
     2550                return (bool) apply_filters( 'bp_group_extension_is_screen_enabled', $enabled, $this->class_name, $context, $this->screens[ $context ]['enabled'] ) ;
    25302551        }
    25312552
    25322553        /**
    function bp_register_group_extension( $group_extension_class = '' ) { 
    29542975                return false;
    29552976        }
    29562977
     2978        // Register the extension with BuddyPress
     2979        $extension = new $group_extension_class;
     2980        $extension->_register();
     2981        buddypress()->groups->extensions[ $extension->slug ] = $extension;
     2982
    29572983        // Register the group extension on the bp_init action so we have access
    29582984        // to all plugins.
    29592985        add_action( 'bp_init', create_function( '', '
    2960                 $extension = new ' . $group_extension_class . ';
    2961                 add_action( "bp_actions", array( &$extension, "_register" ), 8 );
    2962                 add_action( "admin_init", array( &$extension, "_register" ) );
     2986                $extension =& buddypress()->groups->extensions[ "' . $extension->slug . '" ];
     2987                add_action( "bp_actions", array( &$extension, "setup_hooks" ), 8 );
     2988                add_action( "admin_init", array( &$extension, "setup_hooks" ) );
    29632989        ' ), 11 );
    29642990}
  • bp-groups/bp-groups-filters.php

    diff --git bp-groups/bp-groups-filters.php bp-groups/bp-groups-filters.php
    index 08b9040..3143999 100644
    function groups_filter_forums_root_page_sql( $sql ) { 
    169169        return apply_filters( 'groups_filter_bbpress_root_page_sql', 't.topic_id' );
    170170}
    171171add_filter( 'get_latest_topics_fields', 'groups_filter_forums_root_page_sql' );
     172
     173/*** Group extensions management filters ****************************************/
     174
     175/**
     176 * Checks the group extensions is available for the group or is default
     177 *
     178 * @param  boolean $is_active
     179 * @param  string  $class     the name of the group extension class to check
     180 * @param  string  $context   the context for the screen requested
     181 * @uses   is_admin() to check we're in backend
     182 * @uses   groups_extension_group_status() to check for current group extensions settings
     183 * @return boolean            true|false
     184 */
     185function groups_maybe_deactivate_extension( $is_active = true, $class = '', $context = '' ) {
     186       
     187        // we need to still be able to list the group extensions in extensions management page
     188        if( is_admin() && ( empty( $context ) || $context != 'admin' ) )
     189                return $is_active;
     190
     191        if( !empty( $context ) ) {
     192                if( $context == 'admin' && empty( $is_active ) )
     193                        return $is_active;
     194        }
     195
     196        $is_active = groups_extension_group_status( $class );
     197
     198        return $is_active;
     199}
     200
     201add_filter( 'bp_register_group_extension', 'groups_maybe_deactivate_extension', 10, 2 );
     202add_filter( 'bp_group_extension_is_screen_enabled', 'groups_maybe_deactivate_extension', 10, 3 );
  • bp-groups/bp-groups-functions.php

    diff --git bp-groups/bp-groups-functions.php bp-groups/bp-groups-functions.php
    index 3f1d4eb..480d47c 100644
    function groups_remove_data_for_user( $user_id ) { 
    10781078add_action( 'wpmu_delete_user',  'groups_remove_data_for_user' );
    10791079add_action( 'delete_user',       'groups_remove_data_for_user' );
    10801080add_action( 'bp_make_spam_user', 'groups_remove_data_for_user' );
     1081
     1082
     1083/*** Group extensions management functions ****************************************/
     1084
     1085/**
     1086 * Checks if a group extension is active by default
     1087 *
     1088 * @param  string $class the name of the group extension class
     1089 * @uses   bp_get_option() to get the default settings
     1090 * @return boolean true|false
     1091 */
     1092function bp_groups_is_extension_active( $slug = '' ) {
     1093        if ( empty( $slug ) ) {
     1094                return false;
     1095        }
     1096
     1097        $inactive_extensions = bp_get_option( 'global_inactive_extensions', array() );
     1098
     1099        return ! isset( $inactive_extensions[ $slug ] );
     1100}
     1101
     1102/**
     1103 * Checks if the group extension is activated for current group
     1104 *
     1105 * @param  string $class the name of the group extension class
     1106 * @uses   is_admin() to check for backend
     1107 * @uses   bp_get_current_group_id() to get current group id
     1108 * @uses   groups_extension_is_inactive() to check a group extension is inactive by default
     1109 * @uses   groups_get_groupmeta() to get current group settings
     1110 * @return boolean true|false
     1111 */
     1112function bp_groups_is_extension_active_for_group( $slug = '', $group_id = 0 ) {
     1113        if ( empty( $slug ) ) {
     1114                return true;
     1115        }
     1116
     1117        if ( empty( $group_id ) ) {
     1118                $group_id = ( is_admin() && isset( $_REQUEST['gid'] ) ) ? intval( $_REQUEST['gid'] ) : bp_get_current_group_id();
     1119        }
     1120
     1121        if ( empty( $group_id ) ) {
     1122                return bp_groups_is_extension_active( $slug );
     1123        }
     1124
     1125        $group_inactive_extensions = groups_get_groupmeta( $group_id, 'inactive_extensions' );
     1126
     1127        if ( empty( $group_inactive_extensions ) || ! is_array( $group_inactive_extensions ) ) {
     1128                return bp_groups_is_extension_active( $slug );
     1129        }
     1130
     1131        return ! isset( $group_inactive_extensions[ $slug ] );
     1132}
  • bp-groups/bp-groups-loader.php

    diff --git bp-groups/bp-groups-loader.php bp-groups/bp-groups-loader.php
    index 8f7bcce..2267051 100644
    class BP_Groups_Component extends BP_Component { 
    4949        public $default_extension;
    5050
    5151        /**
     52         * Extensions registered with BuddyPress
     53         *
     54         * @since BuddyPress (1.9)
     55         * @var array
     56         */
     57        public $extensions = array();
     58
     59        /**
    5260         * Illegal group names/slugs
    5361         *
    5462         * @since BuddyPress (1.5)