/** * Handles updating an idea * * @package WP Idea Stream * @subpackage ideas/functions * * @since 2.0.0 * * @uses check_admin_referer() to check the request has been done from current site * @uses wp_idea_stream_get_redirect_url() to get default redirect url * @uses get_query_var() to get the value of a specific query var * @uses wp_idea_stream_get_post_type() to get the ideas post type identifier * @uses get_queried_object() to try to get the idea object WordPress built * @uses wp_idea_stream_ideas_get_idea_by_name() to get an idea object out of its post name * @uses wp_idea_stream_user_can() to check user's capability * @uses wp_idea_stream_add_message() to add a feddback message to user * @uses wp_safe_redirect() to safely redirect the user and avoid duplicates * @uses wp_idea_stream_ideas_save_idea() to save the idea * @uses wp_idea_stream_get_form_url() to get the add new form url * @uses wp_idea_stream_ideas_get_idea_permalink() to get the idea link */ function wp_idea_stream_ideas_update_idea() { global $wp_query; // Bail if not a post request if ('POST' != strtoupper($_SERVER['REQUEST_METHOD'])) { return; } // Bail if not a post idea request if (empty($_POST['wp_idea_stream']) || !is_array($_POST['wp_idea_stream'])) { return; } // Bail if it's not an update if (empty($_POST['wp_idea_stream']['_the_id'])) { return; } // Check nonce check_admin_referer('wp_idea_stream_save'); $redirect = wp_idea_stream_get_redirect_url(); // Get idea name $idea_name = get_query_var(wp_idea_stream_get_post_type()); // Get Idea Object $idea = get_queried_object(); // If queried object doesn't match or wasn't helpfull, try to get the idea using core function if (empty($idea->post_name) || empty($idea_name) || $idea_name != $idea->post_name) { $idea = wp_idea_stream_ideas_get_idea_by_name($idea_name); } // Found no idea, redirect and inform the user if (empty($idea->ID)) { wp_idea_stream_add_message(array('type' => 'error', 'content' => __('The idea you are trying to edit does not seem to exist.', 'wp-idea-stream'))); // Redirect to main archive page wp_safe_redirect($redirect); exit; } // Checks if the user can edit the idea if (!wp_idea_stream_ideas_can_edit($idea)) { // Add feedback to the user wp_idea_stream_add_message(array('type' => 'error', 'content' => __('You are not allowed to edit this idea.', 'wp-idea-stream'))); // Redirect to main archive page wp_safe_redirect($redirect); exit; } $updated = array_diff_key($_POST['wp_idea_stream'], array('save' => 'submit')); // Title & content are required if (empty($updated['_the_title']) || empty($updated['_the_content'])) { // Add feedback to the user wp_idea_stream_add_message(array('type' => 'error', 'content' => __('Title and description are required fields.', 'wp-idea-stream'))); // Simply stop, so that the user keeps the posted values. return; } // Reset '_the_id' param to the ID of the idea found $updated['_the_id'] = $idea->ID; $feedback_message = array(); $featured_error = __('There was a problem saving the featured image, sorry.', 'wp-idea-stream'); $featured_type = 'info'; // Take care of the featured image $thumbnail_id = (int) get_post_thumbnail_id($idea); if (!empty($updated['_the_thumbnail'])) { $thumbnail_src = key($updated['_the_thumbnail']); $thumbnail = reset($updated['_the_thumbnail']); // Update the Featured image if (!is_numeric($thumbnail) || $thumbnail_id !== (int) $thumbnail) { if (is_numeric($thumbnail)) { // validate the attachment if (!get_post($thumbnail)) { $feedback_message[] = $featured_error; // Set the new Featured image } else { set_post_thumbnail($idea->ID, $thumbnail); } } else { $sideload = WP_Idea_Stream_Ideas_Thumbnail::start($thumbnail_src, $idea->ID); if (is_wp_error($sideload->result)) { $feedback_message[] = $featured_error; } } } // Delete the featured image } elseif (!empty($thumbnail_id)) { delete_post_thumbnail($idea); } // Update the idea $id = wp_idea_stream_ideas_save_idea($updated); if (empty($id)) { // Set the feedback for the user $featured_type = 'error'; $feedback_message = __('Something went wrong while trying to update your idea.', 'wp-idea-stream'); // Redirect to the form $redirect = wp_idea_stream_get_form_url(wp_idea_stream_edit_slug(), $idea_name); // Redirect to the idea } else { $redirect = wp_idea_stream_ideas_get_idea_permalink($id); } if (!empty($feedback_message)) { // Add feedback to the user wp_idea_stream_add_message(array('type' => $featured_type, 'content' => join(' ', $feedback_message))); } wp_safe_redirect($redirect); exit; }
/** * Displays the list of inserted images to let the user * choose the one he wishes to use as the Idea Featured image * * @since 2.3.0 * * @return string HTML Output */ function wp_idea_stream_ideas_the_images_list() { if (!wp_idea_stream_featured_images_allowed() || !current_theme_supports('post-thumbnails')) { return; } $selected = false; $content = ''; $srcs = array(); $wp_idea_stream = wp_idea_stream(); $class = ' class="hidden"'; // There was an error eg: missing title if (!empty($_POST['wp_idea_stream']['_the_content'])) { $content = wp_unslash($_POST['wp_idea_stream']['_the_content']); // Did the user selected a featured image ? if (!empty($_POST['wp_idea_stream']['_the_thumbnail'])) { $selected = (array) $_POST['wp_idea_stream']['_the_thumbnail']; $selected = reset($selected); } // Are we editing an idea ? } else { if (!empty($wp_idea_stream->query_loop->idea->post_content)) { $idea = $wp_idea_stream->query_loop->idea; $content = $idea->post_content; // Try to get the current featured image $selected = (int) get_post_thumbnail_id($idea); if (!empty($selected)) { $original_url = get_post_meta($selected, '_ideastream_original_src', true); if (empty($original_url)) { $original_url = wp_get_attachment_url($selected); } $srcs = array($original_url => $selected); } /** * Get all idea attachments (those who have an _ideastream_original_url meta) * * We need to do this in case the featured image was edited and for some reason the * user deleted one or more images from the content */ $srcs = array_replace($srcs, WP_Idea_Stream_Ideas_Thumbnail::get_idea_attachments($idea->ID)); } } // Find image into the content if (!empty($content)) { $class = ''; if (false !== stripos($content, 'src=')) { preg_match_all('#src=(["\'])([^"\']+)\\1#i', $content, $img_srcs); if (!empty($img_srcs[2])) { // Avoid duplicates $content_srcs = array_unique($img_srcs[2]); // Create a non numeric keys array $content_srcs = array_combine($content_srcs, $content_srcs); /** * Make sure to use attachment ids if some were found earlier */ $srcs = array_replace($content_srcs, $srcs); } } } // Can be an attachment ID if (!empty($selected)) { if (is_numeric($selected)) { $selected = (int) $selected; // Or an url } else { $selected = esc_url($selected); } } ?> <div id="idea-images-list"<?php echo $class; ?> > <label><?php esc_html_e('Select the featured image for your idea.', 'wp-idea-stream'); ?> </label> <?php if (!empty($srcs)) { ?> <ul> <?php foreach ($srcs as $ksrc => $src) { ?> <li> <img src="<?php echo esc_url($ksrc); ?> "/> <?php if (is_numeric($src)) { $thumbnail = (int) $src; } else { $thumbnail = esc_url($src); } ?> <div class="cb-container"> <input type="checkbox" name="wp_idea_stream[_the_thumbnail][<?php echo esc_url_raw($ksrc); ?> ]" value="<?php echo $thumbnail; ?> " <?php checked($selected, $thumbnail); ?> /> </div> </li> <?php } ?> </ul> <?php } ?> </div> <?php }
/** * Starting point. * * @param string $src the link to the image to side upload * @param int $post_id the ID of the post set the featured image for */ public static function start($src = '', $post_id = 0) { if (empty($src) || empty($post_id)) { return new WP_Error('missing_argument'); } // If the single instance hasn't been set, set it now. if (null == self::$instance) { self::$instance = new self($src, $post_id); } return self::$instance; }