Exemplo n.º 1
0
/**
 * Save custom settings related to Theme Options
 *
 * When the post and page is updated, everything that is tied to it is saved also.
 * This includes theme settings.
 *
 * @see wp_update_post()
 *
 * @param int $post_id Post ID.
 */
function iron_save_post($post_id)
{
    global $wpdb;
    if ($post = $wpdb->get_row($wpdb->prepare("SELECT p.*, pm.meta_value AS page_template FROM {$wpdb->posts} AS p INNER JOIN {$wpdb->postmeta} AS pm ON p.ID = pm.post_id WHERE p.ID = %d AND pm.meta_key = '_wp_page_template'", $post_id))) {
        if ('page' == $post->post_type) {
            switch ($post->page_template) {
                case 'front-page.php':
                case 'page-front.php':
                case 'page-home.php':
                    update_option('show_on_front', 'page');
                    update_option('page_on_front', $post_id);
                    break;
                case 'home.php':
                case 'index.php':
                case 'page-blog.php':
                case 'archive-post.php':
                    update_option('show_on_front', 'page');
                    update_option('page_for_posts', $post_id);
                    break;
                case 'archive-album.php':
                    set_iron_option('page_for_albums', $post_id);
                    break;
                case 'archive-event.php':
                    set_iron_option('page_for_events', $post_id);
                    break;
                case 'archive-video.php':
                    set_iron_option('page_for_videos', $post_id);
                    break;
                case 'archive-photo.php':
                    set_iron_option('page_for_photos', $post_id);
                    break;
                default:
                    if ($post->post_name == 'home') {
                        update_option('show_on_front', 'page');
                        update_option('page_on_front', $post_id);
                    } else {
                        if (get_iron_option('page_for_albums') == $post_id) {
                            reset_iron_option('page_for_albums');
                        }
                        if (get_iron_option('page_for_events') == $post_id) {
                            reset_iron_option('page_for_events');
                        }
                        if (get_iron_option('page_for_videos') == $post_id) {
                            reset_iron_option('page_for_videos');
                        }
                        if (get_iron_option('page_for_photos') == $post_id) {
                            reset_iron_option('page_for_photos');
                        }
                        if (get_option('page_on_front') === 0 && get_option('page_for_posts') === 0) {
                            update_option('show_on_front', 'posts');
                        }
                    }
                    break;
            }
        }
    }
}
Exemplo n.º 2
0
function iron_import_default_data()
{
    global $wpdb;
    require_once IRON_PARENT_DIR . '/includes/classes/autoimporter.class.php';
    $importPath = IRON_PARENT_DIR . '/import/';
    $placeholders = true;
    /*
    	if($_SERVER['HTTP_HOST'] == 'irontemplates.com' || $_SERVER['HTTP_HOST'] == 'staging.irontemplates.com' || strpos($_SERVER['HTTP_HOST'], '.dev') !== false) {
    		$placeholders = false;
    	}
    */
    $file = $importPath . 'default-data.xml';
    $file_tmp = $importPath . 'default-data-tmp.xml';
    $theme = !empty($_POST["theme"]) && $_POST["theme"] != 'default' ? '-' . $_POST["theme"] : '';
    $redux = !empty($_POST["redux"]) ? $_POST["redux"] : '';
    $revslider = !empty($_POST["revslider"]) ? $_POST["revslider"] : '';
    $file_content = file_get_contents('http://irontemplates.com/import/' . IRON_TEXT_DOMAIN . '/default-data' . $theme . '.xml');
    if ($placeholders) {
        //$file_content = preg_replace("/http:\/\/irontemplates\.com\/wp-demos\/(.*?)\/wp-content\/uploads\/(.*?).(jpg|jpeg|png|gif)\</", "http://placehold.it/400x400/text/color/placeholder.jpg<", $file_content);
    }
    if (!is_writable($file) || !file_put_contents($file, $file_content)) {
        $message = "Oops! An issue has been found. Don't worry, you have 2 different ways to fix it.<br><br>";
        $message .= "<strong>Option 1)</strong> Make sure this file is writable: " . $file . "<br>To do this, you need to set this folder permission and this file permission to 777. Check this video to know how to set folder permission using FileZilla: <a href='http://www.youtube.com/watch?v=MKgfquaVAgM'>http://www.youtube.com/watch?v=MKgfquaVAgM</a><br><br>";
        $message .= "<strong>Option 2)</strong> Import the default data using Wordpress importer. Read this faq for more info: <a href='http://it.ticksy.com/faq/180'>http://it.ticksy.com/faq/1800</a>";
        $data['error'] = true;
        $data['msg'] = '<p style="color: red;">' . $message . '</p>';
        die(json_encode($data));
    }
    if (@file_exists($file)) {
        if (@copy($file, $file_tmp)) {
            /* Import Reduc Settings
               ========================================================================== */
            if (!empty($redux)) {
                set_transient('redux-opts-saved', '1', 1000);
                $import = wp_remote_retrieve_body(wp_remote_get($redux));
                $imported_options = unserialize(trim($import, '###'));
                if (is_array($imported_options) && isset($imported_options['redux-opts-backup']) && $imported_options['redux-opts-backup'] == '1') {
                    $imported_options['imported'] = 1;
                    global $Redux_Options;
                    $Redux_Options->options = $imported_options;
                    update_option(IRON_TEXT_DOMAIN, $imported_options);
                }
            }
            if (!empty($revslider) && function_exists('is_plugin_active') && is_plugin_active('revslider/revslider.php')) {
                $revslider_data = file_get_contents($revslider);
                $revslider_data = json_decode($revslider_data, true);
                $wpdb->query("TRUNCATE TABLE " . $wpdb->prefix . "revslider_css");
                $wpdb->query("TRUNCATE TABLE " . $wpdb->prefix . "revslider_layer_animations");
                $wpdb->query("TRUNCATE TABLE " . $wpdb->prefix . "revslider_sliders");
                $wpdb->query("TRUNCATE TABLE " . $wpdb->prefix . "revslider_slides");
                if (!empty($revslider_data["sliders"])) {
                    $styles = $revslider_data["styles"];
                    $animations = $revslider_data["animations"];
                    $sliders = $revslider_data["sliders"];
                    $slides = $revslider_data["slides"];
                    foreach ($styles as $style) {
                        $wpdb->insert($wpdb->prefix . 'revslider_css', $style);
                    }
                    foreach ($animations as $animation) {
                        $wpdb->insert($wpdb->prefix . 'revslider_layer_animations', $animation);
                    }
                    foreach ($sliders as $slider) {
                        $wpdb->insert($wpdb->prefix . 'revslider_sliders', $slider);
                    }
                    foreach ($slides as $slide) {
                        $wpdb->insert($wpdb->prefix . 'revslider_slides', $slide);
                    }
                }
            }
            /* Import XML
               ========================================================================== */
            $args = array('file' => $file_tmp, 'map_user_id' => 1);
            $removed = array();
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->posts}")) {
                $removed[] = __('Posts removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->postmeta}")) {
                $removed[] = __('Postmeta removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->comments}")) {
                $removed[] = __('Comments removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->commentmeta}")) {
                $removed[] = __('Commentmeta removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->links}")) {
                $removed[] = __('Links removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->terms}")) {
                $removed[] = __('Terms removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->term_relationships}")) {
                $removed[] = __('Term relationships removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("TRUNCATE TABLE {$wpdb->term_taxonomy}")) {
                $removed[] = __('Term Taxonomy removed', IRON_TEXT_DOMAIN);
            }
            if ($wpdb->query("DELETE FROM {$wpdb->options} WHERE `option_name` LIKE ('%_transient_%')")) {
                $removed[] = __('Transients removed', IRON_TEXT_DOMAIN);
            }
            $wpdb->query("OPTIMIZE TABLE {$wpdb->options}");
            foreach ($removed as $item) {
                $output[] = '' . $item . '<br>';
            }
            $output[] = '<hr>';
            ob_start();
            auto_import($args);
            $raw = ob_get_contents();
            ob_end_clean();
            $output[] = $raw;
            /* Ugly hack to avoid duplicated Menu Items
               ========================================================================== */
            $keep_safe = array();
            $results = $wpdb->get_results("select MIN(m2.meta_value) as parent from {$wpdb->postmeta} m1\r\n\t\t\tINNER JOIN {$wpdb->postmeta} m2 ON m1.post_id = m2.post_id AND m2.meta_key = '_menu_item_menu_item_parent'\r\n\t\t\tWHERE m1.meta_key = '_menu_item_object_id' AND m2.meta_value != 0 group by m1.meta_value having count(*) > 1");
            foreach ($results as $res) {
                $keep_safe[] = $res->parent;
            }
            $results = $wpdb->get_results("select MAX(m2.meta_value) as parent, m1.post_id, m1.meta_value, MAX(m1.meta_id) from {$wpdb->postmeta} m1\r\n\t\t\tINNER JOIN {$wpdb->postmeta} m2 ON m1.post_id = m2.post_id AND m2.meta_key = '_menu_item_menu_item_parent'\r\n\t\t\tWHERE m1.meta_key = '_menu_item_object_id' AND m2.meta_value != 0 group by m1.meta_value having count(*) > 1");
            foreach ($results as $res) {
                $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE `post_id` = {$res->post_id}");
                wp_delete_post($res->post_id);
                $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE `post_id` = {$res->parent}");
                wp_delete_post($res->parent);
            }
            $results = $wpdb->get_results("select m1.post_id, m1.meta_value, MAX(m1.meta_id) from {$wpdb->postmeta} m1\r\n\t\t\tINNER JOIN {$wpdb->postmeta} m2 ON m1.post_id = m2.post_id AND m2.meta_key = '_menu_item_menu_item_parent'\r\n\t\t\tWHERE m1.meta_key = '_menu_item_object_id' AND m2.meta_value = 0 group by m1.meta_value having count(*) > 1");
            foreach ($results as $res) {
                if (!in_array($res->post_id, $keep_safe)) {
                    $wpdb->query("DELETE FROM {$wpdb->postmeta} WHERE `post_id` = {$res->post_id}");
                    wp_delete_post($res->post_id);
                }
            }
            /* Set Menu Location
               ========================================================================== */
            $menu_slug = 'main-menu';
            $menu_id = (int) $wpdb->get_var("SELECT term_id FROM {$wpdb->terms} WHERE slug = '" . $menu_slug . "'");
            if (is_numeric($menu_id)) {
                $locations = get_theme_mod('nav_menu_locations');
                $locations[$menu_slug] = $menu_id;
                set_theme_mod('nav_menu_locations', $locations);
            }
            /* Setup Widgets
               ========================================================================== */
            /**
             * Default sidebars also set in /admin/options.php:widget_areas
             */
            if (class_exists('WP_Widget')) {
                // Disable default WordPress sidebars from fresh install
                //update_option( 'widget_search', array() );
                update_option('widget_recent-posts', array());
                update_option('widget_recent-comments', array());
                update_option('widget_archives', array());
                update_option('widget_categories', array());
                update_option('widget_meta', array());
                update_option('sidebars_widgets', array('wp_inactive_widgets' => array(), IRON_SIDEBAR_PREFIX . '0' => array(), IRON_SIDEBAR_PREFIX . '1' => array(), IRON_SIDEBAR_PREFIX . '2' => array(), IRON_SIDEBAR_PREFIX . '3' => array(), IRON_SIDEBAR_PREFIX . '4' => array()));
                global $wp_registered_sidebars;
                ### Setup Widget Instances
                $sidebar_widgets = wp_get_sidebars_widgets();
                $widget_areas = get_iron_option('widget_areas', null, array());
                ### Sidebar "Default Footer"
                if (array_key_exists(IRON_SIDEBAR_PREFIX . '2', $sidebar_widgets)) {
                    $sidebar_widgets[IRON_SIDEBAR_PREFIX . '2'] = array('iron-newsletter-2');
                    update_option('widget_iron-newsletter', array(2 => array('title' => __('Subscribe to our newsletter', IRON_TEXT_DOMAIN)), '_multiwidget' => 1));
                    set_iron_option('footer-area_id', IRON_SIDEBAR_PREFIX . '2');
                }
                ### Sidebar "Default Blog Sidebar"
                if (array_key_exists(IRON_SIDEBAR_PREFIX . '0', $sidebar_widgets)) {
                    $sidebar_widgets[IRON_SIDEBAR_PREFIX . '0'] = array('iron-terms-2');
                    update_option('widget_iron-terms', array(2 => array('title' => __('Categories'), 'taxonomy' => 'category', 'count' => 1, 'hierarchical' => 0, 'dropdown' => 0), '_multiwidget' => 1));
                    $query = new WP_Query(array('post_type' => 'page', 'posts_per_page' => -1, 'no_found_rows' => true, 'meta_query' => array(array('key' => '_wp_page_template', 'value' => 'index', 'compare' => 'LIKE'))));
                    if ($query->have_posts()) {
                        foreach ($query->posts as $post) {
                            update_post_meta($post->ID, 'sidebar-position', 'right', 'disabled');
                            update_post_meta($post->ID, 'sidebar-area_id', IRON_SIDEBAR_PREFIX . '0', '');
                        }
                    }
                }
                ### Sidebar "Default Video Sidebar"
                if (array_key_exists(IRON_SIDEBAR_PREFIX . '1', $sidebar_widgets)) {
                    $sidebar_widgets[IRON_SIDEBAR_PREFIX . '1'] = array('iron-terms-3');
                    $widget_instances = get_option('widget_iron-terms');
                    unset($widget_instances['_multiwidget']);
                    $widget_instances[] = array('title' => __('Categories'), 'taxonomy' => 'video-category', 'count' => 1, 'hierarchical' => 0, 'dropdown' => 0);
                    $widget_instances['_multiwidget'] = 1;
                    update_option('widget_iron-terms', $widget_instances);
                    $query = new WP_Query(array('post_type' => 'page', 'posts_per_page' => -1, 'no_found_rows' => true, 'meta_query' => array(array('key' => '_wp_page_template', 'value' => 'archive-video', 'compare' => 'LIKE'))));
                    if ($query->have_posts()) {
                        foreach ($query->posts as $post) {
                            update_post_meta($post->ID, 'sidebar-position', 'right', 'disabled');
                            update_post_meta($post->ID, 'sidebar-area_id', IRON_SIDEBAR_PREFIX . '1', '');
                        }
                    }
                }
                wp_set_sidebars_widgets($sidebar_widgets);
                $output[] = '<hr>';
                $output[] = '<p>' . __('Widgets assigned to sidebars.', IRON_TEXT_DOMAIN) . '</p>';
            }
            /* ========================================================================== */
            flush_rewrite_rules();
            $data['error'] = false;
            $data['msg'] = implode('', $output) . '<p style="color: green;"><strong>' . __('Import Succeded!', IRON_TEXT_DOMAIN) . '</strong></p>';
        } else {
            $data['error'] = true;
            $data['msg'] = '<p style="color: red;"><strong>' . __('Unable to generate temporary copy of import file. Permission denied.', IRON_TEXT_DOMAIN) . '</strong></p>';
        }
    } else {
        $data['error'] = true;
        $data['msg'] = '<p style="color: red;"><strong>' . __('Import file is missing:', IRON_TEXT_DOMAIN) . ' ' . $file . '</strong></p>';
    }
    die(json_encode($data));
}