Skip to:
Content

BuddyPress.org

Ticket #2673: 2673.diff

File 2673.diff, 33.5 KB (added by imath, 10 years ago)
  • bp-groups/bp-groups-admin.php

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

     
    25262526                        $enabled = $this->screens[ $context ]['enabled'] && is_callable( $this->screens[ $context ]['screen_callback'] );
    25272527                }
    25282528
    2529                 return (bool) $enabled;
     2529                /** adding a filter here to eventually disallow extension admin screen **/
     2530                return (bool) apply_filters( 'bp_group_extension_is_screen_enabled', $enabled, $this->class_name, $context, $this->screens[ $context ]['enabled'] ) ;
    25302531        }
    25312532
    25322533        /**
     
    29542955                return false;
    29552956        }
    29562957
     2958        /* this filter allow us to restrict/make available group extensions */
     2959        $is_active = apply_filters( 'bp_register_group_extension', true, $group_extension_class );
     2960
     2961        if( empty( $is_active ) )
     2962                return false;
     2963
    29572964        // Register the group extension on the bp_init action so we have access
    29582965        // to all plugins.
    29592966        add_action( 'bp_init', create_function( '', '
  • bp-groups/bp-groups-filters.php

     
    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

     
    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 inactive 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 groups_extension_is_inactive( $class = '' ) {
     1093        if( empty( $class ) )
     1094                return false;
     1095
     1096        $inactive_extensions = bp_get_option( 'bp_inactive_group_extensions', array() );
     1097
     1098        if( in_array( $class, $inactive_extensions ) )
     1099                return true;
     1100
     1101        return false;
     1102}
     1103
     1104/**
     1105 * Checks if the group extension is activated for current group
     1106 *
     1107 * @param  string $class the name of the group extension class
     1108 * @uses   is_admin() to check for backend
     1109 * @uses   bp_get_current_group_id() to get current group id
     1110 * @uses   groups_extension_is_inactive() to check a group extension is inactive by default
     1111 * @uses   groups_get_groupmeta() to get current group settings
     1112 * @return boolean true|false
     1113 */
     1114function groups_extension_group_status( $class = '' ) {
     1115        // default behavior is to use the extension..
     1116        if( empty( $class ) )
     1117                return true;
     1118
     1119        $group_id = ( is_admin() && isset( $_REQUEST['gid'] ) ) ? intval( $_REQUEST['gid'] ) : bp_get_current_group_id();
     1120
     1121        if( empty( $group_id ) )
     1122                return !groups_extension_is_inactive( $class );
     1123
     1124        $group_extension_settings = groups_get_groupmeta( $group_id, 'bp_group_extensions_status' );
     1125
     1126        if( empty( $group_extension_settings ) || !is_array( $group_extension_settings ) )
     1127                return !groups_extension_is_inactive( $class );
     1128
     1129        if( isset( $group_extension_settings[$class] ) )
     1130                return $group_extension_settings[$class];
     1131
     1132        else
     1133                return !groups_extension_is_inactive( $class );
     1134}