PUM_Admin_Upgrade_Routine_4::run()

Upgrade popup targeting conditions.


Description Description

  • Convert Conditions
  • Default popups with no conditions to draft

Source Source

File: includes/admin/upgrades/class-pum-admin-upgrade-routine-4.php

	public static function run() {
		if ( ! current_user_can( PUM_Admin_Upgrades::instance()->required_cap ) ) {
			wp_die( __( 'You do not have permission to do upgrades', 'popup-maker' ), __( 'Error', 'popup-maker' ), array( 'response' => 403 ) );
		}

		ignore_user_abort( true );

		if ( ! pum_is_func_disabled( 'set_time_limit' ) ) {
			@set_time_limit( 0 );
		}

		$upgrades  = PUM_Admin_Upgrades::instance();
		$completed = $upgrades->get_arg( 'completed' );
		$total     = $upgrades->get_arg( 'total' );

		// Set the correct total.
		if ( $total <= 1 ) {
			$popups = wp_count_posts( 'popup' );
			$total  = 0;
			foreach ( $popups as $status ) {
				$total += $status;
			}
			$upgrades->set_arg( 'total', $total );
		}

		$popups = pum_get_popups( array(
			'number' => $upgrades->get_arg( 'number' ),
			'page'   => $upgrades->get_arg( 'step' ),
			'status' => array( 'any', 'trash', 'auto-draft' ),
			'order'  => 'ASC',
		) );

		if ( $popups ) {

			foreach ( $popups as $popup ) {

				$_conditions = $conditions = array();

				// Convert Conditions
				$targeting_conditions = popmake_get_popup_meta_group( 'targeting_condition', $popup->ID );

				if ( empty( $targeting_conditions ) ) {
					if ( $popup->post_status == 'publish' ) {
						// Default popups with no conditions to draft
						PUM_Admin_Upgrade_Routine_4::change_post_status( $popup->ID, 'draft' );
					}
					update_post_meta( $popup->ID, 'popup_conditions', $conditions );
					$completed ++;
					continue;
				}


				$sitewide = false;

				if ( array_key_exists( 'on_entire_site', $targeting_conditions ) ) {
					$sitewide             = true;
					$targeting_conditions = PUM_Admin_Upgrade_Routine_4::filter_excludes( $targeting_conditions );
				} else {
					$targeting_conditions = PUM_Admin_Upgrade_Routine_4::filter_includes( $targeting_conditions );
				}

				$targeting_conditions = PUM_Admin_Upgrade_Routine_4::parse_conditions( $targeting_conditions );

				$_group = array();

				foreach ( $targeting_conditions as $condition ) {

					// If sitewide is enabled then all conditions use the not_operand.
					$condition['not_operand'] = $sitewide ? 1 : 0;

					// Add a new AND condition group.
					if ( $sitewide ) {
						$_conditions[] = array( $condition );
					} // Add a new OR condition to the group.
					else {
						$_group[] = $condition;
					}

				}

				if ( ! $sitewide && ! empty( $_group ) ) {
					$_conditions[] = $_group;
				}

				foreach ( $_conditions as $group_key => $group ) {
					foreach ( $group as $condition_key => $condition ) {
						$validated = PUM_Conditions::instance()->validate_condition( $condition );
						if ( ! is_wp_error( $validated ) ) {
							$conditions[ $group_key ][ $condition_key ] = $validated;
						}
					}
				}

				update_post_meta( $popup->ID, 'popup_conditions', $conditions );

				$completed ++;
			}

			if ( $completed < $total ) {
				$upgrades->set_arg( 'completed', $completed );
				PUM_Admin_Upgrade_Routine_4::next_step();
			}

		}

		PUM_Admin_Upgrade_Routine_4::done();
	}


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.