Exemplo n.º 1
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();
    }
}