Example #1
0
/**
 * Ajax callback for Create New Stack button
 *
 * @since    1.0.0
 * @link     http://codex.wordpress.org/Function_Reference/add_editor_style
 * @see      get_bloginfo()
 * @param    array $wp See link for description.
 * @return   void
 */
function mp_stacks_add_new_stack_ajax()
{
    //Check nonce
    if (!check_ajax_referer('mp-stacks-nonce-action-name', 'mp_stacks_nonce', false)) {
        echo __('Ajax Security Check', 'mp_stacks');
        die;
    }
    $new_stack_name = $_POST['mp_stacks_new_stack_name'];
    $mp_stacks_new_stack_source_type = $_POST['mp_stacks_new_stack_source_type'];
    $new_stack_duplicate_id = $_POST['mp_stacks_new_stack_duplicate_id'];
    $new_stack_template_slug = $_POST['mp_stacks_new_stack_template_slug'];
    if (!empty($new_stack_name) && current_user_can('edit_theme_options')) {
        //If the user wants to duplicate an existing stack
        if ($mp_stacks_new_stack_source_type == 'duplicate-stack-option') {
            //Duplicate the stack id passed to ajax
            $new_stack_id = mp_stacks_duplicate_stack($new_stack_duplicate_id, $new_stack_name);
        } else {
            if ($mp_stacks_new_stack_source_type == 'template-stack-option') {
                $new_stack_id = mp_stacks_create_stack_from_template($new_stack_template_slug(), $new_stack_name);
            } else {
                //Make new stack
                $new_stack_array = wp_insert_term($new_stack_name, 'mp_stacks');
                //If there was an error making the stack, echo that error
                if (is_wp_error($new_stack_array)) {
                    $error_string = $new_stack_array->get_error_message();
                    echo '<div id="message" class="error"><p>' . $error_string . '</p></div>';
                    die;
                }
                $new_stack_id = $new_stack_array['term_id'];
            }
        }
        //Get the updated Stacks Table
        $wp_list_table = _get_list_table('WP_Terms_List_Table', array('screen' => 'edit-mp_stacks'));
        ob_start();
        $wp_list_table->display_rows_or_placeholder();
        $updated_stacks_table = ob_get_clean();
        echo json_encode(array('new_stack_id' => $new_stack_id, 'updated_stacks_table' => $updated_stacks_table));
    }
    die;
    // this is required to return a proper result
}
Example #2
0
/**
 * Theme Bundle Installation Function: This function will check if we've created this Theme Bundle's Default Stacks and corresponding Pages/Posts
 * If they haven't been created - or just don't exist (they've been deleted), re-create them. 
 * Additionally we can apply Stacks to certain roles. If a page is supposed to be the 'home' page, set that as well.
 * If a stack is supposed to be the 'footer' stack, set that as well.
 * @since 1.0
 * @param $theme_bundle_slug The slug of the theme bundle using underscores.
 * @return void
 */
function mp_stacks_theme_bundle_create_default_pages($theme_bundle_slug)
{
    /*The $default_stacks_to_create filtered array is formatted like so:
    	//array(
    		'post_type (we'll create a post for each item in this array and put the corresponding Stack onto it)' => array(
    			'stack's template_slug' => array( 'is_home' => true ),
    			'stack's template_slug' => array( 'is_footer' => true ),
    			'stack's template_slug' => array(),
    		),
    		'post' => array(
    			'stack's template_slug' => array(),
    			'stack's template_slug' => array(),
    			'stack's template_slug' => array(),
    		),
    		'page' => array(
    			'stack's template_slug' => array(),
    			'stack's template_slug' => array(),
    			'stack's template_slug' => array(),
    		),
    		'download' => array(
    			'stack's template_slug' => array(),
    			'stack's template_slug' => array(),
    			'stack's template_slug' => array(),
    		),	
    	);
    	*/
    //Set up a default empty array for us to begin filtering
    $default_stacks_to_create = array('post' => array(), 'page' => array());
    $default_stacks_to_create = apply_filters($theme_bundle_slug . '_default_stacks', $default_stacks_to_create);
    //Get the option where we save all default-created stacks
    $previously_created_default_stacks = get_option('mp_stacks_default_stacks_created');
    //Loop through each post type in the $default_stacks_to_create
    foreach ($default_stacks_to_create as $post_type => $stacks_to_create) {
        //Loop through each stack to create for this post type
        foreach ($stacks_to_create as $stack_template_slug => $other_info) {
            //If a default stack doesn't exist for this template
            if (!isset($previously_created_default_stacks[$stack_template_slug]['stack_id']) || !get_term_by('id', $previously_created_default_stacks[$stack_template_slug]['stack_id'], 'mp_stacks')) {
                $stack_template_function_name = 'mp_stacks_' . $stack_template_slug . '_array';
                //Create a new stack using this stack template.
                $new_stack_id = mp_stacks_create_stack_from_template($stack_template_function_name(), isset($other_info['title']) ? $other_info['title'] : ucwords(str_replace('_', ' ', $stack_template_slug)));
                //Add this stack to the list of default stacks we've created for this stack template
                $previously_created_default_stacks[$stack_template_slug]['stack_id'] = $new_stack_id;
            }
            //If there should be a corresponding post for this stack
            if ($post_type != 'none') {
                //If a default corresponding post doesn't exist for this template (it has never been created before, OR it has been deleted)
                if (!isset($previously_created_default_stacks[$stack_template_slug]['post_id']) || isset($previously_created_default_stacks[$stack_template_slug]['post_id']) && !mp_core_post_exists($previously_created_default_stacks[$stack_template_slug]['post_id'])) {
                    //Set up the new post to use
                    $default_post = array('post_title' => isset($other_info['title']) ? $other_info['title'] : ucwords(str_replace('_', ' ', $stack_template_slug)), 'post_content' => '[mp_stack stack="' . $previously_created_default_stacks[$stack_template_slug]['stack_id'] . '"]', 'post_type' => $post_type, 'post_status' => 'publish', 'post_author' => 1, 'comment_status' => 'closed');
                    //Creat the new default post and assign the Stack to be on the page
                    $new_post_id = wp_insert_post($default_post);
                    //Add this post to the list of default posts we've created for this stack template
                    $previously_created_default_stacks[$stack_template_slug]['post_id'] = $new_post_id;
                } else {
                    $default_post = array('ID' => $previously_created_default_stacks[$stack_template_slug]['post_id'], 'post_content' => '[mp_stack stack="' . $previously_created_default_stacks[$stack_template_slug]['stack_id'] . '"]');
                    // Update the post into the database
                    wp_update_post($default_post);
                }
            }
            //If this stack template/corresponding post is supposed to be the homepage
            if (isset($other_info['is_home']) && $other_info['is_home']) {
                //Set the home page to be this Stack Template/Corresponding Post
                update_option('page_on_front', $previously_created_default_stacks[$stack_template_slug]['post_id']);
                update_option('show_on_front', 'page');
            }
            //If this stack template/corresponding post is supposed to be the footer
            if (isset($other_info['is_footer']) && $other_info['is_footer']) {
                //Set the footer to be this Stack Template/Corresponding Post
                set_theme_mod('mp_stacks_footer_stack', $previously_created_default_stacks[$stack_template_slug]['stack_id']);
            }
            //If this stack template/corresponding post requires a page template
            if (isset($other_info['page_template'])) {
                //Set the page template of this post
                update_post_meta($previously_created_default_stacks[$stack_template_slug]['post_id'], '_wp_page_template', $other_info['page_template']);
            }
            //If this stack template/corresponding post requires a post format
            if (isset($other_info['post_format'])) {
                //Set the post format of this post
                set_post_format($previously_created_default_stacks[$stack_template_slug]['post_id'], $other_info['post_format']);
            }
            //If a corresponding post exists for this stack
            if (isset($previously_created_default_stacks[$stack_template_slug]['post_id'])) {
                //Disable Comments on this corresponding post
                wp_update_post(array('ID' => $previously_created_default_stacks[$stack_template_slug]['post_id'], 'comment_status' => 'closed'));
            }
        }
    }
    //Update the option which tells us which default stacks have been created and their corresponding ids
    update_option('mp_stacks_default_stacks_created', $previously_created_default_stacks);
    return true;
}
Example #3
0
/**
 * Function which duplicates a stack and all bricks within it
 * Parameter: Stack ID
 * Parameter: $args
 * Returns New Stack's ID
 */
function mp_stacks_duplicate_stack($original_stack_id, $new_stack_name)
{
    //Get the template array for the stack we want to duplicate
    $mp_stack_template_array = mp_stack_template_array($original_stack_id);
    //Create the new stack using the stack template and name
    $new_stack_id = mp_stacks_create_stack_from_template($mp_stack_template_array, $new_stack_name);
    //Return the new stack id
    return $new_stack_id;
}