' . esc_html__( 'Tags', 'mainwp' ) . '',
'read',
'ManageGroups',
array(
static::get_class_name(),
'render_all_groups',
)
);
/**
* This hook allows you to add extra sub pages to the Tags page via the 'mainwp-getsubpages-tags' filter.
*
* @link http://codex.mainwp.com/#mainwp-getsubpages-tags
*/
static::$subPages = apply_filters( 'mainwp_getsubpages_tags', static::$subPages );
static::init_left_menu( static::$subPages );
}
/**
* Initiates Tags menu.
*
* @param array $subPages Sub pages array.
*
* @uses \MainWP\Dashboard\MainWP_Menu::add_left_menu()
* @uses \MainWP\Dashboard\MainWP_Menu::init_subpages_left_menu()
* @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
*/
public static function init_left_menu( $subPages = array() ) {
MainWP_Menu::add_left_menu(
array(
'title' => esc_html__( 'Tags', 'mainwp' ),
'parent_key' => 'managesites',
'slug' => 'ManageGroups',
'href' => 'admin.php?page=ManageGroups',
'icon' => '',
'desc' => 'Manage tags on your MainWP Dashboard',
'leftsub_order' => 3,
),
1
);
$init_sub_subleftmenu = array(
array(
'title' => esc_html__( 'Manage Tags', 'mainwp' ),
'parent_key' => 'ManageGroups',
'href' => 'admin.php?page=ManageGroups',
'slug' => 'ManageGroups',
'right' => 'manage_groups',
),
);
MainWP_Menu::init_subpages_left_menu( $subPages, $init_sub_subleftmenu, 'ManageGroups', 'ManageGroups' );
foreach ( $init_sub_subleftmenu as $item ) {
if ( MainWP_Menu::is_disable_menu_item( 3, $item['slug'] ) ) {
continue;
}
MainWP_Menu::add_left_menu( $item, 2 );
}
}
/**
* Method render_header()
*
* Render Tags page header.
*
* @param string $shownPage The page slug shown at this moment.
*
* @uses \MainWP\Dashboard\MainWP_Menu::is_disable_menu_item()
* @uses \MainWP\Dashboard\MainWP_UI::render_top_header()
* @uses \MainWP\Dashboard\MainWP_UI::render_page_navigation()
*/
public static function render_header( $shownPage = '' ) {
$params = array(
'title' => esc_html__( 'Tags', 'mainwp' ),
);
MainWP_UI::render_top_header( $params );
$renderItems = array();
if ( \mainwp_current_user_can( 'dashboard', 'manage_groups' ) ) {
$renderItems[] = array(
'title' => esc_html__( 'Manage Tags', 'mainwp' ),
'href' => 'admin.php?page=ManageGroups',
'active' => ( 'ManageGroups' === $shownPage ) ? true : false,
);
}
if ( isset( static::$subPages ) && is_array( static::$subPages ) ) {
foreach ( static::$subPages as $subPage ) {
if ( MainWP_Menu::is_disable_menu_item( 3, 'ManageGroups' . $subPage['slug'] ) ) {
continue;
}
$item = array();
$item['title'] = $subPage['title'];
$item['href'] = 'admin.php?page=ManageGroups' . $subPage['slug'];
$item['active'] = ( $subPage['slug'] === $shownPage ) ? true : false;
$renderItems[] = $item;
}
}
MainWP_UI::render_page_navigation( $renderItems );
}
/**
* Method render_footer()
*
* Render Tags page footer. Closes the page container.
*/
public static function render_footer() {
echo '';
}
/**
* Method get_group_list_content()
*
* Get group list contents.
*
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_groups_and_count()
*/
public static function get_group_list_content() {
$groups = MainWP_DB_Common::instance()->get_groups_and_count();
foreach ( $groups as $group ) {
static::create_group_item( $group );
}
}
/**
* Metod create_group_item()
*
* Group Data Table Row.
*
* @param array $group Array of group data.
*/
private static function create_group_item( $group ) {
?>
nrsites ) : 0; ?>
name ) ); ?>
query( MainWP_DB::instance()->get_sql_websites_for_current_user() );
while ( $websites && ( $website = MainWP_DB::fetch_object( $websites ) ) ) {
$note = html_entity_decode( $website->note );
$esc_note = MainWP_Utility::esc_content( $note );
$strip_note = wp_strip_all_tags( $esc_note );
?>
|
|
name ) ); ?> |
|
url ); ?> |
client_name ); ?> |
|
note ) ) : ?>
|
', ' ' ); // NOSONAR - noopener - open safe. ?>
get_group_by_id( intval( $_POST['groupId'] ) );
if ( ! empty( $group ) ) {
$old_name = $group->name;
$name = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
if ( empty( $name ) ) {
$name = $group->name;
}
$name = static::check_group_name( $name, $group->id );
if ( isset( $_POST['newColor'] ) && ! empty( $_POST['newColor'] ) ) {
$color = sanitize_hex_color( wp_unslash( $_POST['newColor'] ) );
if ( empty( $color ) ) {
$color = $group->color;
}
} else {
$color = '';
}
// update group.
MainWP_DB_Common::instance()->update_group( $group->id, $name, $color );
// Reload group.
$group = MainWP_DB_Common::instance()->get_group_by_id( $group->id );
$data = array(
'old_name' => $old_name,
);
/**
* Fires after a new sites tag has been created.
*
* @param object $group tag created.
* @param string tag action.
* @param array other data array.
*/
do_action( 'mainwp_site_tag_action', $group, 'updated', $data );
die( wp_json_encode( array( 'result' => $group->name ) ) );
}
}
//phpcs:enable
}
/**
* Method delete_group()
*
* Delete the selected group.
*
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
* @uses \MainWP\Dashboard\MainWP_DB_Common::remove_group()
*/
public static function delete_group() {
//phpcs:disable WordPress.Security.NonceVerification.Missing
$groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false;
//phpcs:enable
if ( $groupid ) {
$success = MainWP_DB_Common::instance()->remove_group( $groupid );
if ( $success ) {
die( 'OK' );
}
}
die( 'ERROR' );
}
/**
* Method check_group_name()
*
* Check if group name already exists
* if it does add a number to the end of it.
*
* @param mixed $groupName Given Group Name.
* @param null $groupId Group ID.
*
* @return string $groupName Group name + count # if group has bn found.
*
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_name()
*/
public static function check_group_name( $groupName, $groupId = null ) {
if ( empty( $groupName ) ) {
$groupName = esc_html__( 'New tag', 'mainwp' );
}
$groupName = esc_html( $groupName );
$cnt = null;
if ( preg_match( '/(.*) \(\d\)/', $groupName, $matches ) ) {
$groupName = $matches[1];
}
$group = MainWP_DB_Common::instance()->get_group_by_name( $groupName );
while ( $group && ( ( null === $groupId ) || ( (int) $group->id !== (int) $groupId ) ) ) {
if ( null === $cnt ) {
$cnt = 1;
} else {
++$cnt;
}
$group = MainWP_DB_Common::instance()->get_group_by_name( $groupName . ' (' . $cnt . ')' );
}
return $groupName . ( null === $cnt ? '' : ' (' . $cnt . ')' );
}
/**
* Method add_group()
*
* Add Group.
*
* @uses \MainWP\Dashboard\MainWP_DB_Common::add_group()
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
*/
public static function add_group() {
/**
* Current user global.
*
* @global string
*/
global $current_user;
//phpcs:disable WordPress.Security.NonceVerification.Missing
$newName = isset( $_POST['newName'] ) ? sanitize_text_field( wp_unslash( $_POST['newName'] ) ) : '';
$newColor = isset( $_POST['newColor'] ) ? sanitize_hex_color( wp_unslash( $_POST['newColor'] ) ) : '';
//phpcs:enable
if ( ! empty( $newName ) ) {
$groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, static::check_group_name( $newName ), $newColor );
/**
* New Group Added
*
* Fires after a new sites group has been created.
*
* @param int $groupId Group ID.
*/
do_action( 'mainwp_added_new_group', $groupId );
$group = MainWP_DB_Common::instance()->get_group_by_id( $groupId );
static::create_group_item( $group );
die();
}
die( wp_json_encode( array( 'error' => 1 ) ) );
}
/**
* Method add_group_sites()
*
* Add Group sites.
*
* @param string $gname Group Name.
* @param array $site_ids Sites IDs.
* @param string $gcolor Tag color.
*/
public static function add_group_sites( $gname, $site_ids, $gcolor = '' ) {
/**
* Current user global.
*
* @global string
*/
global $current_user;
$groupId = MainWP_DB_Common::instance()->add_group( $current_user->ID, static::check_group_name( $gname ), $gcolor );
if ( $groupId ) {
$group = MainWP_DB_Common::instance()->get_group_by_id( $groupId );
if ( ! empty( $group ) && ! empty( $site_ids ) ) {
foreach ( $site_ids as $websiteId ) {
$website = MainWP_DB::instance()->get_website_by_id( $websiteId );
if ( MainWP_System_Utility::can_edit_website( $website ) ) {
MainWP_DB_Common::instance()->update_group_site( $group->id, $website->id );
}
}
}
/**
* New Group Added
*
* Fires after a new sites group has been created.
*
* @param int $groupId Group ID.
*/
do_action( 'mainwp_added_new_group', $groupId );
return true;
}
return false;
}
/**
* Method update_group()
*
* Update groups Sites.
*
* @uses \MainWP\Dashboard\MainWP_DB_Common::get_group_by_id()
* @uses \MainWP\Dashboard\MainWP_DB_Common::clear_group()
* @uses \MainWP\Dashboard\MainWP_DB_Common::update_group_site()
* @uses \MainWP\Dashboard\MainWP_DB::get_website_by_id()
* @uses \MainWP\Dashboard\MainWP_System_Utility::can_edit_website()
*/
public static function update_group() { //phpcs:ignore -- NOSONAR - complex.
// phpcs:disable WordPress.Security.NonceVerification,WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
$groupid = isset( $_POST['groupId'] ) && ! empty( $_POST['groupId'] ) ? intval( $_POST['groupId'] ) : false;
if ( $groupid ) {
$group = MainWP_DB_Common::instance()->get_group_by_id( $groupid );
if ( ! empty( $group ) ) {
MainWP_DB_Common::instance()->clear_group( $group->id );
if ( isset( $_POST['websiteIds'] ) ) {
foreach ( array_map( 'sanitize_text_field', wp_unslash( $_POST['websiteIds'] ) ) as $websiteId ) {
$website = MainWP_DB::instance()->get_website_by_id( $websiteId );
if ( MainWP_System_Utility::can_edit_website( $website ) ) {
MainWP_DB_Common::instance()->update_group_site( $group->id, $website->id );
}
}
}
die( wp_json_encode( array( 'result' => true ) ) );
}
}
// phpcs:enable
die( wp_json_encode( array( 'result' => false ) ) );
}
/**
* Method mainwp_help_content()
*
* Creates the MainWP Help Documentation List for the help component in the sidebar.
*/
public static function mainwp_help_content() {
if ( isset( $_GET['page'] ) && 'ManageGroups' === $_GET['page'] ) { // phpcs:ignore WordPress.Security.NonceVerification
?>
*
* @since 4.1
*/
do_action( 'mainwp_tags_help_item' );
?>