';
}
/**
* create a new group
*
*/
public function create_group(){
// check nonce
if ( ! isset( $_POST['advads-group-add-nonce'] )
|| ! wp_verify_nonce( $_POST['advads-group-add-nonce'], 'add-advads-groups' ) ){
return new WP_Error( 'invalid_ad_group', __( 'Invalid Ad Group', 'advanced-ads' ) );
}
// check user rights
if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) {
return new WP_Error( 'invalid_ad_group_rights', __( 'You don’t have permission to change the ad groups', 'advanced-ads' ) );
}
if ( isset($_POST['advads-group-name']) && '' !== $_POST['advads-group-name'] ){
$title = sanitize_text_field( $_POST['advads-group-name'] );
$new_group = wp_create_term( $title, Advanced_Ads::AD_GROUP_TAXONOMY );
if( is_wp_error($new_group ) ){
return $new_group;
}
// save default values
if( is_array( $new_group ) ){
$group = new Advanced_Ads_Group( $new_group['term_id'] );
// allow other add-ons to save their own group attributes
$atts = apply_filters( 'advanced-ads-group-save-atts', array(
'type' => 'default',
'ad_count' => 1,
'options' => array(),
), $group);
$group->save( $atts );
}
// reload groups
$this->load_groups();
} else {
return new WP_Error( 'no_ad_group_created', __( 'No ad group created', 'advanced-ads' ) );
}
return true;
}
private function get_groups_by_ad_id($ad_id){
$ids = array();
$terms = wp_get_object_terms($ad_id, $this->taxonomy);
foreach ($terms as $term){
$ids[] = $term->term_id;
}
return $ids;
}
/**
* bulk update groups
*
*/
public function update_groups(){
// check nonce
if ( ! isset( $_POST['advads-group-update-nonce'] )
|| ! wp_verify_nonce( $_POST['advads-group-update-nonce'], 'update-advads-groups' ) ){
return new WP_Error( 'invalid_ad_group', __( 'Invalid Ad Group', 'advanced-ads' ) );
}
// check user rights
if( ! current_user_can( Advanced_Ads_Plugin::user_cap( 'advanced_ads_edit_ads') ) ) {
return new WP_Error( 'invalid_ad_group_rights', __( 'You don’t have permission to change the ad groups', 'advanced-ads' ) );
}
/** empty group settings
* edit: emptying disabled, because when only a few groups are saved (e.g. when filtered by search), options are reset
* todo: needs a solution that also removes options when the group is removed
*/
// empty weights
// update_option( 'advads-ad-weights', array() );
$all_weights = get_option( 'advads-ad-weights', array() );
// ad_id => group_ids
$ad_groups_assoc = array();
if ( isset( $_POST['advads-groups-removed-ads'] ) && is_array( $_POST['advads-groups-removed-ads'] ) && isset( $_POST['advads-groups-removed-ads-gid'])) {
$len = count($_POST['advads-groups-removed-ads']);
for ($i=0; $i<$len; $i++){
$ad_id = $_POST['advads-groups-removed-ads'][$i];
$group_id = $_POST['advads-groups-removed-ads-gid'][$i];
$ad_groups_assoc[ $ad_id ] = array();
// remove it from the weights
if ( isset($all_weights[$group_id]) && isset($all_weights[$group_id][$ad_id])){
unset($all_weights[$group_id][$ad_id]);
}
// we need to load all the group ids, that are allocated to this ad and then remove the right one only.
$group_ids = $this->get_groups_by_ad_id($ad_id);
foreach ($group_ids as $gid){
if ($gid != $group_id){
$ad_groups_assoc[$ad_id][] = $gid;
}
}
}
}
// iterate through groups
if ( isset($_POST['advads-groups']) && count( $_POST['advads-groups'] ) ){
foreach ( $_POST['advads-groups'] as $_group_id => $_group ){
// save basic wp term
wp_update_term( $_group_id, Advanced_Ads::AD_GROUP_TAXONOMY, $_group );
$group = new Advanced_Ads_Group( $_group['id'] );
if ( isset( $_group['ads'] ) && is_array( $_group['ads'] ) ) {
foreach ( $_group['ads'] as $_ad_id => $_ad_weight ) {
/**
* check if this ad is representing the current group and remove it in this case
* could cause an infinite loop otherwise
* see also /classes/ad_type_group.php::remove_from_ad_group()
*/
$ad = new Advanced_Ads_Ad( $_ad_id );
// we will have to load all the groups allocated to this ad.
if (! isset($ad_groups_assoc[$_ad_id])){
$ad_groups_assoc[$_ad_id] = $this->get_groups_by_ad_id($_ad_id);
}
if( isset( $ad->type )
&& 'group' === $ad->type
&& isset( $ad->output['group_id'] )
&& absint( $ad->output['group_id'] ) == $_group_id
){
unset( $_group['ads'][ $_ad_id ] );
} else {
$ad_groups_assoc[ $_ad_id ][] = (int) $_group_id;
}
}
// save ad weights
$all_weights[$group->id] = $group->sanitize_ad_weights( $_group['ads'] );
//$group->save_ad_weights( $_group['ads'] );
}
// save other attributes
$type = isset($_group['type']) ? $_group['type'] : 'default';
$ad_count = isset($_group['ad_count']) ? $_group['ad_count'] : 1;
$options = isset($_group['options']) ? $_group['options'] : array();
// allow other add-ons to save their own group attributes
$atts = apply_filters( 'advanced-ads-group-save-atts', array(
'type' => $type,
'ad_count' => $ad_count,
'options' => $options,
), $_group);
$group->save( $atts );
}
foreach ( $ad_groups_assoc as $_ad_id => $group_ids ) {
wp_set_object_terms( $_ad_id, $group_ids, $this->taxonomy);
}
}
update_option( 'advads-ad-weights', $all_weights );
// reload groups
$this->load_groups();
return true;
}
/**
* returns a link to the ad group list page
*
* @since 1.0.0
* @param arr $args additional arguments, e.g. action or group_id
* @return string admin url
*/
public static function group_page_url($args = array()) {
$plugin = Advanced_Ads::get_instance();
$defaultargs = array(
// 'post_type' => constant("Advanced_Ads::POST_TYPE_SLUG"),
'page' => 'advanced-ads-groups',
);
$args = $args + $defaultargs;
return add_query_arg( $args, admin_url( 'admin.php' ) );
}
}