public function remove()
 {
     // 1. We remove the experiment itself
     parent::remove();
     // 2. And we now remove all the alternative widgets
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
     NelioABWidgetExpAdminController::clean_widgets_in_experiment($this->get_id());
 }
 public function apply_alternative()
 {
     if (isset($_POST['original']) && isset($_POST['alternative'])) {
         require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
         $exp = $_GET['id'];
         $ori = $_POST['original'];
         $alt = $_POST['alternative'];
         if ($ori == $alt) {
             NelioABWidgetExpAdminController::apply_alternative_and_clean($exp);
         } else {
             NelioABWidgetExpAdminController::apply_alternative_and_clean($exp, $alt);
         }
     }
 }
 /**
  * PHPDOC
  *
  * @param array $all_widgets PHPDOC
  *
  * @return array PHPDOC
  *
  * @since PHPDOC
  */
 public function filter_original_widgets($all_widgets)
 {
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
     $widgets_in_experiments = NelioABWidgetExpAdminController::get_widgets_in_experiments();
     $res = array();
     foreach ($all_widgets as $sidebar => $widgets) {
         $res[$sidebar] = array();
         if (!is_array($widgets)) {
             continue;
         }
         foreach ($widgets as $widget) {
             if ($this->is_widget_original($widget, $widgets_in_experiments)) {
                 array_push($res[$sidebar], $widget);
             }
         }
     }
     return $res;
 }
 /**
  *
  * @return
  */
 private static function clean_inconsistent_data($experiments)
 {
     if (count($experiments) == 0) {
         return;
     }
     $last_clean = get_option('nelioab_last_clean_of_inconsitent_data', false);
     if (NELIOAB_PLUGIN_VERSION == $last_clean) {
         return;
     }
     $ids = array();
     foreach ($experiments as $exp) {
         array_push($ids, $exp->get_id());
     }
     // Clean old widgets
     require_once NELIOAB_EXP_CONTROLLERS_DIR . '/widget-experiment-controller.php';
     NelioABWidgetExpAdminController::remove_alternatives_not_in($ids);
     update_option('nelioab_last_clean_of_inconsitent_data', NELIOAB_PLUGIN_VERSION);
 }
Exemplo n.º 5
0
/**
 * 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();
    }
}