Skip to:
Content

BuddyPress.org

Ticket #2673: 2673.03.diff

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

     
    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        }
    20382039
    2039                 // Hook the extension into BuddyPress
     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                }
     2049
    20402050                $this->setup_display_hooks();
    20412051                $this->setup_create_hooks();
    20422052                $this->setup_edit_hooks();
     
    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        /**
     
    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->slug, $context, $this->screens[ $context ]['enabled'] ) ;
    25302551        }
    25312552
    25322553        /**
     
    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-loader.php

     
    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)
  • 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  $slug     the slug of the group extension to check
     180 * @param  string  $context   the context for the screen requested
     181 * @uses   is_admin() to check we're in backend
     182 * @uses   bp_groups_is_extension_active() to check for current group extensions settings
     183 * @return boolean            true|false
     184 */
     185function groups_maybe_deactivate_extension( $is_active = true, $slug = '', $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 = bp_groups_is_extension_active_for_group( $slug );
     197
     198        return $is_active;
     199}
     200
     201
     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 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( 'bp_inactive_group_extensions', array() );
     1098
     1099        return ! in_array( $slug, $inactive_extensions );
     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_extension_settings = groups_get_groupmeta( $group_id, 'bp_group_extensions_status' );
     1126
     1127        if( empty( $group_extension_settings ) || !is_array( $group_extension_settings ) )
     1128                return bp_groups_is_extension_active( $slug );
     1129
     1130        // status is 1 or 0
     1131        if( isset( $group_extension_settings[$slug] ) )
     1132                return $group_extension_settings[$slug];
     1133
     1134        else
     1135                return bp_groups_is_extension_active( $slug );
     1136       
     1137}