/**
  *
  */
 public static function get_instance()
 {
     if (!self::$instance) {
         self::$instance = new NelioABMenuExpAdminController();
     }
     return self::$instance;
 }
 public function duplicate($new_name)
 {
     $id = parent::duplicate($new_name);
     if (-1 == $id) {
         return $id;
     }
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/menu-experiment-controller.php';
     $controller = NelioABMenuExpAdminController::get_instance();
     require_once NELIOAB_MODELS_DIR . '/experiments-manager.php';
     /** @var NelioABMenuAlternativeExperiment $exp */
     $exp = NelioABExperimentsManager::get_experiment_by_id($id, $this->get_type());
     $alts = 0;
     $controller->begin();
     foreach ($exp->get_alternatives() as $alt) {
         /** @var NelioABAlternative $alt */
         $menu_id = $controller->duplicate_menu_and_create_alternative($alt->get_value(), $exp->get_id());
         $body = array('value' => $menu_id);
         try {
             NelioABBackend::remote_post(sprintf(NELIOAB_BACKEND_URL . '/alternative/%s/update', $alt->get_id()), $body);
             $alts++;
         } catch (Exception $e) {
         }
     }
     $controller->commit();
     if (0 == $alts) {
         $exp->set_status(NelioABExperiment::STATUS_DRAFT);
     }
     $exp->save();
     return $exp->get_id();
 }
 public function apply_alternative()
 {
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/menu-experiment-controller.php';
     $aux = NelioABMenuExpAdminController::get_instance();
     $aux->copy_nav_menu_items($_POST['alternative'], $_POST['original']);
     echo 'OK';
     die;
 }
/**
 * Deactivates our plugin.
 *
 * This function is called by the "registed_deactivation_hook". Alternatives
 * are regular pages or posts (draft status) with a special metaoption that
 * is used to hide them from the admin menu. When the plugin is deactivated,
 * no one hides the alternatives... In order to prevent them from appearing,
 * we change their post_type to a fake type.
 *
 * @return void
 *
 * @since PHPDOC
 */
function nelioab_deactivate_plugin()
{
    /** @var wpdb $wpdb */
    global $wpdb;
    require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
    require_once NELIOAB_EXP_CONTROLLERS_DIR . '/menu-experiment-controller.php';
    if (isset($_GET['action']) && 'clean-and-deactivate' == $_GET['action']) {
        // Remove all alternative widgets
        NelioABWidgetExpAdminController::clean_all_alternative_widgets();
        // Remove all alternative menus
        NelioABMenuExpAdminController::clean_all_alternative_menus();
        // Remove all alternative pages and posts
        $query = '' . 'DELETE FROM ' . $wpdb->posts . ' WHERE ' . 'id IN (' . 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE ' . 'meta_key = \'_is_nelioab_alternative\' ' . ')';
        $wpdb->query($query);
        // Clean all experiments in AE
        require_once NELIOAB_UTILS_DIR . '/backend.php';
        for ($i = 0; $i < 5; ++$i) {
            try {
                NelioABBackend::remote_get(sprintf(NELIOAB_BACKEND_URL . '/site/%s/clean', NelioABAccountSettings::get_site_id()));
                break;
            } catch (Exception $e) {
            }
        }
        // Remove all Nelio options
        $query = 'DELETE FROM ' . $wpdb->postmeta . ' WHERE meta_key LIKE \'%nelioab%\'';
        $wpdb->query($query);
        $query = 'DELETE FROM ' . $wpdb->options . ' WHERE option_name LIKE \'%nelioab%\'';
        $wpdb->query($query);
    } else {
        // Hiding alternative posts
        $query = '' . 'UPDATE ' . $wpdb->posts . ' SET post_type = %s WHERE ' . 'id IN (' . 'SELECT post_id FROM ' . $wpdb->postmeta . ' WHERE ' . 'meta_key = \'_is_nelioab_alternative\' ' . ') AND ' . 'post_type = %s';
        // recover post type names
        require_once NELIOAB_UTILS_DIR . '/wp-helper.php';
        $cpts = NelioABWpHelper::get_custom_post_types();
        $post_types = array('post', 'page');
        foreach ($cpts as $post_type) {
            array_push($post_types, $post_type->name);
        }
        // execute the query
        foreach ($post_types as $post_type) {
            $wpdb->query($wpdb->prepare($query, 'nelioab_alt_' . $post_type, $post_type));
        }
        // Hiding widget alternatives
        NelioABWidgetExpAdminController::backup_alternative_widgets();
        // Hiding menu alternatives
        NelioABMenuExpAdminController::backup_alternative_menus();
    }
}