/**
  * Save the new field data for the nav menu.
  *
  * @param $menu_id
  * @param $menu_item_db_id
  * @param $args
  */
 public function update_nav_fields($menu_id, $menu_item_db_id, $args)
 {
     // Hide on mobile.
     if (isset($_POST['hide-menu-on-mobile'][$menu_item_db_id])) {
         update_post_meta($menu_item_db_id, 'hide_menu_on_mobile', empty($_POST['hide-menu-on-mobile'][$menu_item_db_id]) ? false : 'on');
     } else {
         delete_post_meta($menu_item_db_id, 'hide_menu_on_mobile');
     }
     // Image.
     if (isset($_POST['menu-item-image']) && is_array($_POST['menu-item-image'])) {
         if (!isset($_POST['menu-item-image'][$menu_item_db_id]) || !$_POST['menu-item-image'][$menu_item_db_id]) {
             delete_post_thumbnail($menu_item_db_id);
         }
         if (isset($_POST['menu-item-image'][$menu_item_db_id])) {
             set_post_thumbnail($menu_item_db_id, absint($_POST['menu-item-image'][$menu_item_db_id]));
         }
     }
     if (isset($_POST['menu-item-icon']) && is_array($_POST['menu-item-icon'])) {
         if (isset($_POST['menu-item-icon'][$menu_item_db_id])) {
             update_post_meta($menu_item_db_id, '_menu_item_icon', sanitize_text_field($_POST['menu-item-icon'][$menu_item_db_id]));
         }
     }
     if (isset($_POST['menu-item-widget-area']) && isset($_POST['menu-item-widget-area'][$menu_item_db_id]) && is_array($_POST['menu-item-widget-area'])) {
         update_post_meta($menu_item_db_id, '_menu_item_widget_area', sanitize_text_field($_POST['menu-item-widget-area'][$menu_item_db_id]));
     }
 }
/**
 * Delete attachments linked to a specified post
 * @param int $parent_id Parent id of post to delete attachments for
 */
function wp_delete_attachments($parent_id, $unlink = true, $type = 'images')
{
    if ($type == 'images' and has_post_thumbnail($parent_id)) {
        delete_post_thumbnail($parent_id);
    }
    $ids = array();
    $attachments = get_posts(array('post_parent' => $parent_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null));
    foreach ($attachments as $attach) {
        if ($type == 'files' and !wp_attachment_is_image($attach->ID)) {
            if ($unlink) {
                wp_delete_attachment($attach->ID, true);
            } else {
                $ids[] = $attach->ID;
            }
        } elseif ($type == 'images' and wp_attachment_is_image($attach->ID)) {
            if ($unlink) {
                wp_delete_attachment($attach->ID, true);
            } else {
                $ids[] = $attach->ID;
            }
        }
    }
    global $wpdb;
    if (!empty($ids)) {
        $ids_string = implode(',', $ids);
        // unattach
        $result = $wpdb->query("UPDATE {$wpdb->posts} SET post_parent = 0 WHERE post_type = 'attachment' AND ID IN ( {$ids_string} )");
        foreach ($ids as $att_id) {
            clean_attachment_cache($att_id);
        }
    }
    return $ids;
}
Exemplo n.º 3
0
 /**
  *  Delete the post thumbnail when deleted from front end
  *
  * @since 0.1
  *
  * @param array $data Sanitized data to use for saving.
  *
  * @return bool Always returns true.
  */
 public function delete($data)
 {
     $postid = isset($data['postid']) ? $data['postid'] : false;
     delete_post_thumbnail($postid);
     do_action('lasso_featured_image_deleted', $postid, get_current_user_ID());
     return true;
 }
function mc_event_post($action, $data, $event_id)
{
    // if the event save was successful.
    if ($action == 'add' || $action == 'copy') {
        $post_id = mc_create_event_post($data, $event_id);
    } else {
        if ($action == 'edit') {
            if (isset($_POST['event_post']) && ($_POST['event_post'] == 0 || $_POST['event_post'] == '')) {
                $post_id = mc_create_event_post($data, $event_id);
            } else {
                $post_id = $_POST['event_post'];
            }
            // If, after all that, the post doesn't exist, create it.
            if (!get_post_status($post_id)) {
                mc_create_event_post($data, $event_id);
            }
            $term = mc_get_category_detail($data['event_category'], 'category_term');
            if (!$term) {
                $term = wp_insert_term('General', 'mc-event-category');
                $term = !is_wp_error($term) ? $term['term_id'] : false;
                if ($term) {
                    $update = mc_update_category('category_term', $term, $data['event_category']);
                }
            }
            $privacy = mc_get_category_detail($data['event_category'], 'category_private') == 1 ? 'private' : 'publish';
            $title = $data['event_title'];
            $template = apply_filters('mc_post_template', 'details', $term);
            $data['shortcode'] = "[my_calendar_event event='{$event_id}' template='{$template}' list='']";
            $description = $data['event_desc'];
            $excerpt = $data['event_short'];
            $post_status = $privacy;
            $auth = $data['event_author'];
            $type = 'mc-events';
            $my_post = array('ID' => $post_id, 'post_title' => $title, 'post_content' => $description, 'post_status' => $post_status, 'post_author' => $auth, 'post_name' => sanitize_title($title), 'post_date' => date('Y-m-d H:i:s', current_time('timestamp')), 'post_type' => $type, 'post_excerpt' => $excerpt);
            if (mc_switch_sites() && defined(BLOG_ID_CURRENT_SITE)) {
                switch_to_blog(BLOG_ID_CURRENT_SITE);
            }
            $post_id = wp_update_post($my_post);
            wp_set_object_terms($post_id, (int) $term, 'mc-event-category');
            if ($data['event_image'] == '') {
                delete_post_thumbnail($post_id);
            } else {
                $attachment_id = isset($_POST['event_image_id']) && is_numeric($_POST['event_image_id']) ? $_POST['event_image_id'] : false;
                if ($attachment_id) {
                    set_post_thumbnail($post_id, $attachment_id);
                }
            }
            $access = isset($_POST['events_access']) ? $_POST['events_access'] : array();
            $access_terms = implode(',', array_values($access));
            mc_update_event('event_access', $access_terms, $event_id, '%s');
            do_action('mc_update_event_post', $post_id, $_POST, $data, $event_id);
            if (mc_switch_sites()) {
                restore_current_blog();
            }
        }
    }
    return $post_id;
}
Exemplo n.º 5
0
 /**
  * When menu item removed remove menu image metadata.
  */
 public function delete_menu_item_image_action()
 {
     $menu_item_id = (int) $_REQUEST['menu-item'];
     check_admin_referer('delete-menu_item_image_' . $menu_item_id);
     if (is_nav_menu_item($menu_item_id) && has_post_thumbnail($menu_item_id)) {
         delete_post_thumbnail($menu_item_id);
         delete_post_meta($menu_item_id, 'menu_item_icon');
         delete_post_meta($menu_item_id, 'menu-item-bg-img');
     }
 }
Exemplo n.º 6
0
/**
 *
 */
function erm_update_menu_item()
{
    //echo '<pre>'; print_r( $_POST ); echo '</pre>'; exit();
    if (isset($_POST['post_id'])) {
        $post_id = absint($_POST['post_id']);
        wp_update_post(array('ID' => $post_id, 'post_title' => $_POST['title'], 'post_name' => $_POST['title'], 'post_content' => $_POST['content']));
        update_post_meta($post_id, '_erm_visible', $_POST['visible'] == 'true' ? true : false);
        update_post_meta($post_id, '_erm_prices', $_POST['prices']);
        $image_id = absint($_POST['image_id']);
        if ($image_id != 0) {
            set_post_thumbnail($post_id, $image_id);
        } else {
            delete_post_thumbnail($post_id);
        }
        wp_send_json_success();
    }
    exit;
}
/**
 * Delete attachments linked to a specified post
 * @param int $parent_id Parent id of post to delete attachments for
 */
function wp_delete_attachments($parent_id, $unlink = true, $type = '')
{
    if ($type == 'images' and has_post_thumbnail($parent_id)) {
        delete_post_thumbnail($parent_id);
    }
    foreach (get_posts(array('post_parent' => $parent_id, 'post_type' => 'attachment', 'numberposts' => -1, 'post_status' => null)) as $attach) {
        if ($type == 'files' and !wp_attachment_is_image($attach->ID)) {
            wp_delete_attachment($attach->ID, true);
        } elseif ($type == 'images' and wp_attachment_is_image($attach->ID) or "" == $type) {
            if ($unlink or !wp_attachment_is_image($attach->ID)) {
                wp_delete_attachment($attach->ID, true);
            } else {
                global $wpdb;
                $sql = "delete a,b,c\n\t\t\t\t\tFROM " . $wpdb->posts . " a\n\t\t\t\t\tLEFT JOIN " . $wpdb->term_relationships . " b ON ( a.ID = b.object_id )\n\t\t\t\t\tLEFT JOIN " . $wpdb->postmeta . " c ON ( a.ID = c.post_id )\n\t\t\t\t\tLEFT JOIN " . $wpdb->posts . " d ON ( a.ID = d.post_parent )\n\t\t\t\t\tWHERE a.ID = " . $attach->ID . ";";
                $wpdb->query($wpdb->prepare($sql, ''));
            }
        }
    }
}
 public final function set_banner_image()
 {
     $post_ID = intval($_POST['post_id']);
     if (!current_user_can('edit_post', $post_ID)) {
         wp_die(-1);
     }
     $banner_id = intval($_POST['banner_id']);
     check_ajax_referer("update-post_{$post_ID}");
     if ($banner_id == '-1') {
         if (delete_post_thumbnail($post_ID)) {
             wp_send_json_success('');
         } else {
             wp_die(0);
         }
     }
     $course = \Ekko\Core\CoursePostType::singleton()->get_course($post_ID);
     if (set_post_thumbnail($course->ID, $banner_id)) {
         wp_send_json_success($this->banner_thumbnail_html($course));
     }
     wp_die(0);
 }
Exemplo n.º 9
0
 private function update_post_thumbnail($post_id, $reserved_post_thumb_path)
 {
     include_once ABSPATH . 'wp-admin/includes/image.php';
     $upload_dir = wp_upload_dir();
     $image_data = file_get_contents($reserved_post_thumb_path);
     $file_name = basename($reserved_post_thumb_path);
     if (wp_mkdir_p($upload_dir['path'])) {
         $file = $upload_dir['path'] . '/' . $file_name;
     } else {
         $file = $upload_dir['basedir'] . '/' . $file_name;
     }
     file_put_contents($file, $image_data);
     $wp_file_type = wp_check_filetype($file_name, null);
     $attachment = array('post_mime_type' => $wp_file_type['type'], 'post_title' => sanitize_file_name($file_name), 'post_content' => '', 'post_status' => 'inherit');
     delete_post_thumbnail($post_id);
     $attachment_id = wp_insert_attachment($attachment, $file, $post_id);
     $attach_data = wp_generate_attachment_metadata($attachment_id, $file);
     if (!empty($attach_data) && !is_wp_error($attach_data)) {
         $res = wp_update_attachment_metadata($attachment_id, $attach_data);
         set_post_thumbnail($post_id, $attachment_id);
         return $res;
     }
 }
Exemplo n.º 10
0
 public static function deletePostPicture($postId)
 {
     delete_post_thumbnail($postId);
 }
Exemplo n.º 11
0
 /**
  * Update item thumbnail via ajax action.
  *
  * @since 2.0
  */
 public function wp_ajax_set_menu_item_thumbnail()
 {
     $json = !empty($_REQUEST['json']);
     $post_ID = intval($_POST['post_id']);
     if (!current_user_can('edit_post', $post_ID)) {
         wp_die(-1);
     }
     $thumbnail_id = intval($_POST['thumbnail_id']);
     $is_hovered = (bool) $_POST['is_hover'];
     check_ajax_referer("update-menu-item");
     if ($thumbnail_id == '-1') {
         if ($is_hovered) {
             $success = delete_post_meta($post_ID, '_thumbnail_hover_id');
         } else {
             $success = delete_post_thumbnail($post_ID);
         }
     } else {
         if ($is_hovered) {
             $success = update_post_meta($post_ID, '_thumbnail_hover_id', $thumbnail_id);
         } else {
             $success = set_post_thumbnail($post_ID, $thumbnail_id);
         }
     }
     if ($success) {
         $return = $this->wp_post_thumbnail_only_html($post_ID);
         $json ? wp_send_json_success($return) : wp_die($return);
     }
     wp_die(0);
 }
Exemplo n.º 12
0
 public static function update_post_meta($meta_vars, $post_id)
 {
     // prepare the meta vars
     list($meta_key_value, $multi_repeated, $files) = self::prepare_meta_fields($meta_vars);
     // set featured image if there's any
     // if not in admin or if in admin (but doing an ajax call)
     if (is_admin() && defined('DOING_AJAX') && DOING_AJAX || !is_admin()) {
         if (isset($_POST['feat-image-id']) && $_POST['feat-image-id'] != 0) {
             $attachment_id = $_POST['feat-image-id'];
             fes_associate_attachment($attachment_id, $post_id);
             set_post_thumbnail($post_id, $attachment_id);
         }
         if (!isset($_POST['feat-image-id']) || $_POST['feat-image-id'] == 0) {
             delete_post_thumbnail($post_id);
         }
     }
     // save all custom fields
     foreach ($meta_key_value as $meta_key => $meta_value) {
         update_post_meta($post_id, $meta_key, $meta_value);
     }
     // save any multicolumn repeatable fields
     foreach ($multi_repeated as $repeat_key => $repeat_value) {
         // first, delete any previous repeatable fields
         delete_post_meta($post_id, $repeat_key);
         // now add them
         foreach ($repeat_value as $repeat_field) {
             update_post_meta($post_id, $repeat_key, $repeat_field);
         }
     }
     // save any files attached
     foreach ($files as $file_input) {
         if (!isset($_POST[$file_input['name']])) {
             continue;
         }
         $ids = array();
         // We need to detach all previously attached files for this field. See #559
         $old_files = get_post_meta($post_id, $file_input['name'], true);
         if (!empty($old_files) && is_array($old_files)) {
             foreach ($old_files as $file_id) {
                 global $wpdb;
                 $wpdb->update($wpdb->posts, array('post_parent' => 0), array('ID' => $file_id), array('%d'), array('%d'));
             }
         }
         foreach ($_POST[$file_input['name']] as $file => $url) {
             if (empty($url)) {
                 continue;
             }
             $author_id = 0;
             if (!current_user_can('manage_shop_settings')) {
                 $author_id = get_post_field('post_author', $post_id);
             }
             $attachment_id = fes_get_attachment_id_from_url($url, $author_id);
             fes_associate_attachment($attachment_id, $post_id);
             $ids[] = $attachment_id;
         }
         update_post_meta($post_id, $file_input['name'], $ids);
     }
 }
Exemplo n.º 13
0
/**
 * 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;
}
 /**
  * Process an MLA_List_Table custom bulk action
  *
  * Creates new items from the "Bulk Translate" list.
  *
  * @since 2.13
  *
  * @param	array	$item_content	NULL, to indicate no handler.
  * @param	string	$bulk_action	the requested action.
  * @param	integer	$post_id		the affected attachment.
  *
  * @return	object	updated $item_content. NULL if no handler, otherwise
  *					( 'message' => error or status message(s), 'body' => '' )
  */
 public static function mla_list_table_custom_bulk_action($item_content, $bulk_action, $post_id)
 {
     if (self::MLA_GFI_ACTION != $bulk_action) {
         return $item_content;
     }
     /* translators: 1: post ID */
     $item_prefix = sprintf(__('Item %1$d', 'media-library-assistant'), $post_id) . ', ';
     /*
      * If there is a real thumbnail image, no generation is required or allowed
      */
     $thumbnail = wp_get_attachment_image($post_id);
     if (!empty($thumbnail)) {
         return array('message' => $item_prefix . __('has native thumbnail.', 'media-library-assistant'));
     }
     /*
      * Look for the "Featured Image" as an alternate thumbnail for PDFs, etc.
      */
     $thumbnail = get_post_thumbnail_id($post_id);
     if (!empty($thumbnail)) {
         switch (self::$bulk_action_options['existing_thumbnails']) {
             case 'ignore':
                 break;
             case 'trash':
                 delete_post_thumbnail($post_id);
                 wp_delete_post(absint($thumbnail), false);
                 break;
             case 'delete':
                 delete_post_thumbnail($post_id);
                 wp_delete_post(absint($thumbnail), true);
                 break;
             case 'keep':
             default:
                 return array('message' => $item_prefix . __('Featured Image retained.', 'media-library-assistant'));
         }
     }
     /*
      * Validate the file existance and type
      */
     $file = get_attached_file($post_id);
     if (empty($file)) {
         /* translators: 1: ERROR tag 2: Item post ID */
         return array('message' => sprintf(__('%1$s: %2$sno attached file.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $item_prefix));
     }
     if (!in_array(strtolower(pathinfo($file, PATHINFO_EXTENSION)), array('ai', 'eps', 'pdf', 'ps'))) {
         return array('message' => $item_prefix . __('unsupported file type.', 'media-library-assistant'));
     }
     /*
      * Generate a thumbnail
      */
     require_once MLA_PLUGIN_PATH . 'includes/class-mla-image-processor.php';
     $results = MLAImageProcessor::mla_handle_thumbnail_sideload($file, self::$bulk_action_options);
     if (!empty($results['error'])) {
         /* translators: 1: ERROR tag 2: Item post ID */
         return array('message' => sprintf(__('%1$s: %2$sthumbnail generation failed', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $item_prefix) . ' - ' . $results['error']);
     }
     /*
      * Adjust the file name for the new item
      */
     $pathinfo = pathinfo($results['name']);
     if (isset(self::$bulk_action_options['suffix'])) {
         $pathinfo['filename'] = sanitize_file_name($pathinfo['filename'] . strtolower(self::$bulk_action_options['suffix']));
     }
     $pathinfo['extension'] = 'image/jpeg' == $results['type'] ? 'jpg' : 'png';
     $results['name'] = $pathinfo['filename'] . '.' . $pathinfo['extension'];
     $overrides = array('test_form' => false, 'test_size' => true, 'test_upload' => true);
     // move the temporary file into the uploads directory
     $results = wp_handle_sideload($results, $overrides);
     $item_data = get_post($post_id, ARRAY_A);
     unset($item_data['ID']);
     unset($item_data['post_author']);
     unset($item_data['post_date']);
     unset($item_data['post_date_gmt']);
     if (isset(self::$bulk_action_options['suffix'])) {
         $item_data['post_title'] .= self::$bulk_action_options['suffix'];
     }
     unset($item_data['post_name']);
     unset($item_data['post_modified']);
     unset($item_data['post_modified_gmt']);
     $item_parent = $item_data['post_parent'];
     unset($item_data['post_parent']);
     $item_data['guid'] = $results['url'];
     $item_data['post_mime_type'] = $results['type'];
     unset($item_data['comment_count']);
     unset($item_data['ancestors']);
     unset($item_data['post_category']);
     unset($item_data['tags_input']);
     // Insert the attachment.
     $item_id = wp_insert_attachment($item_data, $results['file'], $item_parent);
     if (empty($item_id)) {
         /* translators: 1: ERROR tag 2: Item post ID */
         return array('message' => sprintf(__('%1$s: %2$swp_insert_attachment failed.', 'media-library-assistant'), __('ERROR', 'media-library-assistant'), $item_prefix));
     }
     // Make sure that this file is included, as wp_generate_attachment_metadata() depends on it.
     require_once ABSPATH . 'wp-admin/includes/image.php';
     // Generate the metadata for the attachment, and update the database record.
     $item_data = wp_generate_attachment_metadata($item_id, $results['file']);
     wp_update_attachment_metadata($item_id, $item_data);
     // Assign the new item as the source item's Featured Image
     delete_post_thumbnail($post_id);
     set_post_thumbnail($post_id, $item_id);
     MLA_Thumbnail::$bulk_action_includes[] = $item_id;
     /* translators: 1: Item post ID, 2: new thumbnail item ID */
     $item_content = array('message' => sprintf(__('%1$sthumbnail generated as new item %2$s.', 'media-library-assistant'), $item_prefix, $item_id));
     return $item_content;
 }
Exemplo n.º 15
0
function wp_ajax_set_post_thumbnail()
{
    $post_ID = intval($_POST['post_id']);
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(-1);
    }
    $thumbnail_id = intval($_POST['thumbnail_id']);
    check_ajax_referer("set_post_thumbnail-{$post_ID}");
    if ($thumbnail_id == '-1') {
        if (delete_post_thumbnail($post_ID)) {
            wp_die(_wp_post_thumbnail_html(null, $post_ID));
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_ID, $thumbnail_id)) {
        wp_die(_wp_post_thumbnail_html($thumbnail_id, $post_ID));
    }
    wp_die(0);
}
 /**
  * Save product images.
  *
  * @since 2.5.0
  * @param array $images
  * @param int $id
  */
 private function save_product_images($id, $images)
 {
     if (is_array($images)) {
         $gallery = array();
         foreach ($images as $image) {
             if (isset($image['position']) && $image['position'] == 0) {
                 $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
                 if (0 === $attachment_id && isset($image['src'])) {
                     $upload = $this->upload_product_image(esc_url_raw($image['src']));
                     if (is_wp_error($upload)) {
                         throw new WC_CLI_Exception('woocommerce_cli_cannot_upload_product_image', $upload->get_error_message());
                     }
                     $attachment_id = $this->set_product_image_as_attachment($upload, $id);
                 }
                 set_post_thumbnail($id, $attachment_id);
             } else {
                 $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
                 if (0 === $attachment_id && isset($image['src'])) {
                     $upload = $this->upload_product_image(esc_url_raw($image['src']));
                     if (is_wp_error($upload)) {
                         throw new WC_CLI_Exception('woocommerce_cli_cannot_upload_product_image', $upload->get_error_message());
                     }
                     $gallery[] = $this->set_product_image_as_attachment($upload, $id);
                 } else {
                     $gallery[] = $attachment_id;
                 }
             }
         }
         if (!empty($gallery)) {
             update_post_meta($id, '_product_image_gallery', implode(',', $gallery));
         }
     } else {
         delete_post_thumbnail($id);
         update_post_meta($id, '_product_image_gallery', '');
     }
 }
Exemplo n.º 17
0
 /**
  * Edit a post.
  *
  * @since 1.5.0
  *
  * @param array $args Method parameters.
  * @return bool|IXR_Error True on success.
  */
 public function mw_editPost($args)
 {
     $this->escape($args);
     $post_ID = (int) $args[0];
     $username = $args[1];
     $password = $args[2];
     $content_struct = $args[3];
     $publish = isset($args[4]) ? $args[4] : 0;
     if (!($user = $this->login($username, $password))) {
         return $this->error;
     }
     /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
     do_action('xmlrpc_call', 'metaWeblog.editPost');
     $postdata = get_post($post_ID, ARRAY_A);
     // If there is no post data for the give post id, stop
     // now and return an error. Other wise a new post will be
     // created (which was the old behavior).
     if (!$postdata || empty($postdata['ID'])) {
         return new IXR_Error(404, __('Invalid post ID.'));
     }
     if (!current_user_can('edit_post', $post_ID)) {
         return new IXR_Error(401, __('Sorry, you do not have the right to edit this post.'));
     }
     // Use wp.editPost to edit post types other than post and page.
     if (!in_array($postdata['post_type'], array('post', 'page'))) {
         return new IXR_Error(401, __('Invalid post type'));
     }
     // Thwart attempt to change the post type.
     if (!empty($content_struct['post_type']) && $content_struct['post_type'] != $postdata['post_type']) {
         return new IXR_Error(401, __('The post type may not be changed.'));
     }
     // Check for a valid post format if one was given
     if (isset($content_struct['wp_post_format'])) {
         $content_struct['wp_post_format'] = sanitize_key($content_struct['wp_post_format']);
         if (!array_key_exists($content_struct['wp_post_format'], get_post_format_strings())) {
             return new IXR_Error(404, __('Invalid post format'));
         }
     }
     $this->escape($postdata);
     $ID = $postdata['ID'];
     $post_content = $postdata['post_content'];
     $post_title = $postdata['post_title'];
     $post_excerpt = $postdata['post_excerpt'];
     $post_password = $postdata['post_password'];
     $post_parent = $postdata['post_parent'];
     $post_type = $postdata['post_type'];
     $menu_order = $postdata['menu_order'];
     // Let WordPress manage slug if none was provided.
     $post_name = "";
     $post_name = $postdata['post_name'];
     if (isset($content_struct['wp_slug'])) {
         $post_name = $content_struct['wp_slug'];
     }
     // Only use a password if one was given.
     if (isset($content_struct['wp_password'])) {
         $post_password = $content_struct['wp_password'];
     }
     // Only set a post parent if one was given.
     if (isset($content_struct['wp_page_parent_id'])) {
         $post_parent = $content_struct['wp_page_parent_id'];
     }
     // Only set the menu_order if it was given.
     if (isset($content_struct['wp_page_order'])) {
         $menu_order = $content_struct['wp_page_order'];
     }
     $page_template = null;
     if (!empty($content_struct['wp_page_template']) && 'page' == $post_type) {
         $page_template = $content_struct['wp_page_template'];
     }
     $post_author = $postdata['post_author'];
     // Only set the post_author if one is set.
     if (isset($content_struct['wp_author_id']) && $user->ID != $content_struct['wp_author_id']) {
         switch ($post_type) {
             case 'post':
                 if (!current_user_can('edit_others_posts')) {
                     return new IXR_Error(401, __('You are not allowed to change the post author as this user.'));
                 }
                 break;
             case 'page':
                 if (!current_user_can('edit_others_pages')) {
                     return new IXR_Error(401, __('You are not allowed to change the page author as this user.'));
                 }
                 break;
             default:
                 return new IXR_Error(401, __('Invalid post type'));
                 break;
         }
         $post_author = $content_struct['wp_author_id'];
     }
     if (isset($content_struct['mt_allow_comments'])) {
         if (!is_numeric($content_struct['mt_allow_comments'])) {
             switch ($content_struct['mt_allow_comments']) {
                 case 'closed':
                     $comment_status = 'closed';
                     break;
                 case 'open':
                     $comment_status = 'open';
                     break;
                 default:
                     $comment_status = get_option('default_comment_status');
                     break;
             }
         } else {
             switch ((int) $content_struct['mt_allow_comments']) {
                 case 0:
                 case 2:
                     $comment_status = 'closed';
                     break;
                 case 1:
                     $comment_status = 'open';
                     break;
                 default:
                     $comment_status = get_option('default_comment_status');
                     break;
             }
         }
     }
     if (isset($content_struct['mt_allow_pings'])) {
         if (!is_numeric($content_struct['mt_allow_pings'])) {
             switch ($content_struct['mt_allow_pings']) {
                 case 'closed':
                     $ping_status = 'closed';
                     break;
                 case 'open':
                     $ping_status = 'open';
                     break;
                 default:
                     $ping_status = get_option('default_ping_status');
                     break;
             }
         } else {
             switch ((int) $content_struct["mt_allow_pings"]) {
                 case 0:
                     $ping_status = 'closed';
                     break;
                 case 1:
                     $ping_status = 'open';
                     break;
                 default:
                     $ping_status = get_option('default_ping_status');
                     break;
             }
         }
     }
     if (isset($content_struct['title'])) {
         $post_title = $content_struct['title'];
     }
     if (isset($content_struct['description'])) {
         $post_content = $content_struct['description'];
     }
     $post_category = array();
     if (isset($content_struct['categories'])) {
         $catnames = $content_struct['categories'];
         if (is_array($catnames)) {
             foreach ($catnames as $cat) {
                 $post_category[] = get_cat_ID($cat);
             }
         }
     }
     if (isset($content_struct['mt_excerpt'])) {
         $post_excerpt = $content_struct['mt_excerpt'];
     }
     $post_more = isset($content_struct['mt_text_more']) ? $content_struct['mt_text_more'] : null;
     $post_status = $publish ? 'publish' : 'draft';
     if (isset($content_struct["{$post_type}_status"])) {
         switch ($content_struct["{$post_type}_status"]) {
             case 'draft':
             case 'pending':
             case 'private':
             case 'publish':
                 $post_status = $content_struct["{$post_type}_status"];
                 break;
             default:
                 $post_status = $publish ? 'publish' : 'draft';
                 break;
         }
     }
     $tags_input = isset($content_struct['mt_keywords']) ? $content_struct['mt_keywords'] : null;
     if ('publish' == $post_status) {
         if ('page' == $post_type && !current_user_can('publish_pages')) {
             return new IXR_Error(401, __('Sorry, you do not have the right to publish this page.'));
         } else {
             if (!current_user_can('publish_posts')) {
                 return new IXR_Error(401, __('Sorry, you do not have the right to publish this post.'));
             }
         }
     }
     if ($post_more) {
         $post_content = $post_content . "<!--more-->" . $post_more;
     }
     $to_ping = null;
     if (isset($content_struct['mt_tb_ping_urls'])) {
         $to_ping = $content_struct['mt_tb_ping_urls'];
         if (is_array($to_ping)) {
             $to_ping = implode(' ', $to_ping);
         }
     }
     // Do some timestamp voodoo
     if (!empty($content_struct['date_created_gmt'])) {
         // We know this is supposed to be GMT, so we're going to slap that Z on there by force
         $dateCreated = rtrim($content_struct['date_created_gmt']->getIso(), 'Z') . 'Z';
     } elseif (!empty($content_struct['dateCreated'])) {
         $dateCreated = $content_struct['dateCreated']->getIso();
     }
     if (!empty($dateCreated)) {
         $post_date = get_date_from_gmt(iso8601_to_datetime($dateCreated));
         $post_date_gmt = iso8601_to_datetime($dateCreated, 'GMT');
     } else {
         $post_date = $postdata['post_date'];
         $post_date_gmt = $postdata['post_date_gmt'];
     }
     // We've got all the data -- post it:
     $newpost = compact('ID', 'post_content', 'post_title', 'post_category', 'post_status', 'post_excerpt', 'comment_status', 'ping_status', 'post_date', 'post_date_gmt', 'to_ping', 'post_name', 'post_password', 'post_parent', 'menu_order', 'post_author', 'tags_input', 'page_template');
     $result = wp_update_post($newpost, true);
     if (is_wp_error($result)) {
         return new IXR_Error(500, $result->get_error_message());
     }
     if (!$result) {
         return new IXR_Error(500, __('Sorry, your entry could not be edited. Something wrong happened.'));
     }
     // Only posts can be sticky
     if ($post_type == 'post' && isset($content_struct['sticky'])) {
         if ($content_struct['sticky'] == true) {
             stick_post($post_ID);
         } elseif ($content_struct['sticky'] == false) {
             unstick_post($post_ID);
         }
     }
     if (isset($content_struct['custom_fields'])) {
         $this->set_custom_fields($post_ID, $content_struct['custom_fields']);
     }
     if (isset($content_struct['wp_post_thumbnail'])) {
         // empty value deletes, non-empty value adds/updates
         if (empty($content_struct['wp_post_thumbnail'])) {
             delete_post_thumbnail($post_ID);
         } else {
             if (set_post_thumbnail($post_ID, $content_struct['wp_post_thumbnail']) === false) {
                 return new IXR_Error(404, __('Invalid attachment ID.'));
             }
         }
         unset($content_struct['wp_post_thumbnail']);
     }
     // Handle enclosures
     $thisEnclosure = isset($content_struct['enclosure']) ? $content_struct['enclosure'] : null;
     $this->add_enclosure_if_new($post_ID, $thisEnclosure);
     $this->attach_uploads($ID, $post_content);
     // Handle post formats if assigned, validation is handled
     // earlier in this function
     if (isset($content_struct['wp_post_format'])) {
         set_post_format($post_ID, $content_struct['wp_post_format']);
     }
     /**
      * Fires after a post has been successfully updated via the XML-RPC MovableType API.
      *
      * @since 3.4.0
      *
      * @param int   $post_ID ID of the updated post.
      * @param array $args    An array of arguments to update the post.
      */
     do_action('xmlrpc_call_success_mw_editPost', $post_ID, $args);
     return true;
 }
 public static function xmlrpc_delete_thumbnail($args)
 {
     global $wp_xmlrpc_server;
     $wp_xmlrpc_server->escape($args);
     $blog_id = (int) $args[0];
     $username = $args[1];
     $password = $args[2];
     $post_ID = (int) $args[3];
     $meta_key = !empty($args[4]) ? sanitize_text_field($args[4]) : '_thumbnail_id';
     if (!($user = $wp_xmlrpc_server->login($username, $password))) {
         return $wp_xmlrpc_server->error;
     }
     if (!current_user_can('edit_post', $post_ID)) {
         return new IXR_Error(401, __('Sorry, you are not allowed to post on this site.'));
     }
     if ('_thumbnail_id' == $meta_key) {
         $result = delete_post_thumbnail($post_ID);
     } else {
         $result = delete_post_meta($post_ID, $meta_key);
     }
     if (!$result) {
         return new IXR_Error(403, __('Could not remove post thumbnail.'));
     }
     return true;
 }
Exemplo n.º 19
0
         case 'restore':
             $msg = wp_restore_image($attachment_id);
             break;
     }
     wp_image_editor($attachment_id, $msg);
     die;
     break;
 case 'set-post-thumbnail':
     $post_ID = intval($_POST['post_id']);
     if (!current_user_can('edit_post', $post_ID)) {
         die('-1');
     }
     $thumbnail_id = intval($_POST['thumbnail_id']);
     check_ajax_referer("set_post_thumbnail-{$post_ID}");
     if ($thumbnail_id == '-1') {
         if (delete_post_thumbnail($post_ID)) {
             die(_wp_post_thumbnail_html());
         } else {
             die('0');
         }
     }
     if (set_post_thumbnail($post_ID, $thumbnail_id)) {
         die(_wp_post_thumbnail_html($thumbnail_id));
     }
     die('0');
     break;
 case 'date_format':
     die(date_i18n(sanitize_option('date_format', $_POST['date'])));
     break;
 case 'time_format':
     die(date_i18n(sanitize_option('time_format', $_POST['date'])));
Exemplo n.º 20
0
 function ajax_thumbnail()
 {
     check_ajax_referer('update-post_' . $_POST['post_ID']);
     if (!current_user_can('edit_post', $_POST['post_ID'])) {
         wp_send_json_error(array('message' => __('You are not allowed to edit this item.')));
     }
     if ($_POST['thumbnail_ID'] === '-1') {
         if (delete_post_thumbnail($_POST['post_ID'])) {
             wp_send_json_success('');
         }
     } else {
         if (set_post_thumbnail($_POST['post_ID'], $_POST['thumbnail_ID'])) {
             wp_send_json_success(get_the_post_thumbnail($_POST['post_ID'], $_POST['size']));
         }
     }
     die;
 }
 /**
  * Duplicate Featured Image
  *
  * When a product is duplicated, the featured image ID is copied when the post
  * meta is duplicated.
  *
  * When the featured image is attached to the duplicated product, if the image
  * is duplicated the featured image ID is updated to the duplicated image ID
  * otherwise the featured image ID is removed.
  *
  * If the featured image is not attached to the product the featured image ID
  * remains the same as the original product.
  *
  * This function will remove the featured image if the image is not attached to
  * the duplicated product and offers the opportunity to change the featured image
  * of the duplicated product via the 'wpsc_duplicate_product_thumbnail' filter.
  *
  * @since  4.0
  */
 private function duplicate_product_thumbnail()
 {
     $new_post_id = $this->get_new_post_id();
     if ($new_post_id) {
         $thumbnail_id = $original_thumbnail_id = has_post_thumbnail($new_post_id) ? get_post_thumbnail_id($new_post_id) : 0;
         // If not duplicating product attachments, ensure featured image ID is zero
         if (!apply_filters('wpsc_duplicate_product_attachment', true, $thumbnail_id, $new_post_id)) {
             $thumbnail_id = 0;
         }
         // Filter featured product image ID
         $thumbnail_id = absint(apply_filters('wpsc_duplicate_product_thumbnail', $thumbnail_id, $original_thumbnail_id, $this->get_post_id(), $new_post_id));
         if ($thumbnail_id > 0) {
             set_post_thumbnail($new_post_id, $thumbnail_id);
         } else {
             delete_post_thumbnail($new_post_id);
         }
     }
 }
Exemplo n.º 22
0
 /**
  * Unified save child function.
  *
  * @param int $parent_id
  * @param int $child_id
  * @param array $save_fields
  * @return bool|WP_Error
  */
 function save_child($parent_id, $child_id, $save_fields = array())
 {
     $parent = get_post(intval($parent_id));
     $child = get_post(intval($child_id));
     $post_data = array();
     if (empty($parent) || empty($child)) {
         return new WP_Error('wpcf-relationship-save-child', 'no parent/child post');
     }
     // Save relationship
     update_post_meta($child->ID, '_wpcf_belongs_' . $parent->post_type . '_id', $parent->ID);
     // Check if added via AJAX
     $check = get_post_meta($child->ID, '_wpcf_relationship_new', true);
     $new = !empty($check);
     delete_post_meta($child->ID, '_wpcf_relationship_new');
     // Set post data
     $post_data['ID'] = $child->ID;
     // Title needs to be checked if submitted at all
     if (!isset($save_fields['_wp_title'])) {
         // If not submitted that means it is not offered to be edited
         if (!empty($child->post_title)) {
             $post_title = $child->post_title;
         } else {
             // DO NOT LET IT BE EMPTY
             $post_title = $child->post_type . ' ' . $child->ID;
         }
     } else {
         $post_title = $save_fields['_wp_title'];
     }
     $post_data['post_title'] = $post_title;
     $post_data['post_content'] = isset($save_fields['_wp_body']) ? $save_fields['_wp_body'] : $child->post_content;
     $post_data['post_excerpt'] = isset($save_fields['_wp_excerpt']) ? $save_fields['_wp_excerpt'] : $child->post_excerpt;
     $post_data['post_type'] = $child->post_type;
     // Check post status - if new, convert to 'publish' else keep remaining
     if ($new) {
         $post_data['post_status'] = 'publish';
     } else {
         $post_data['post_status'] = get_post_status($child->ID);
     }
     /*
      *
      *
      *
      *
      *
      *
      * UPDATE POST
      */
     $cf = new WPCF_Field();
     if (isset($_POST['wpcf_post_relationship'][$parent_id]) && isset($_POST['wpcf_post_relationship'][$parent_id][$child_id])) {
         $_POST['wpcf'] = array();
         foreach ($_POST['wpcf_post_relationship'][$parent_id][$child_id] as $slug => $value) {
             $_POST['wpcf'][$cf->__get_slug_no_prefix($slug)] = $value;
             $_POST['wpcf'][$slug] = $value;
         }
     }
     unset($cf);
     /**
      * avoid send data to children
      */
     if (isset($_POST['post_ID'])) {
         $temp_post_data = $_POST;
         $_POST = array();
         foreach (array('wpcf_post_relationship', 'post_ID', '_wptoolset_checkbox', 'wpcf', '_wpnonce') as $key) {
             if (isset($temp_post_data[$key])) {
                 $_POST[$key] = $temp_post_data[$key];
             }
         }
     }
     // Workaround for types-876, see wpcf_admin_post_save_post_hook().
     add_filter('types_updating_child_post', '__return_true');
     $updated_id = wp_update_post($post_data);
     remove_filter('types_updating_child_post', '__return_true');
     if (isset($temp_post_data)) {
         $_POST = $temp_post_data;
         unset($temp_post_data);
     }
     if (empty($updated_id)) {
         return new WP_Error('relationship-update-post-failed', 'Updating post failed');
     }
     // Save parents
     if (!empty($save_fields['parents'])) {
         foreach ($save_fields['parents'] as $parent_post_type => $parent_post_id) {
             update_post_meta($child->ID, '_wpcf_belongs_' . $parent_post_type . '_id', $parent_post_id);
         }
     }
     // Update taxonomies
     if (!empty($save_fields['taxonomies']) && is_array($save_fields['taxonomies'])) {
         $_save_data = array();
         foreach ($save_fields['taxonomies'] as $taxonomy => $t) {
             if (!is_taxonomy_hierarchical($taxonomy)) {
                 $_save_data[$taxonomy] = strval($t);
                 continue;
             }
             foreach ($t as $term_id) {
                 if ($term_id != '-1') {
                     $term = get_term($term_id, $taxonomy);
                     if (empty($term)) {
                         continue;
                     }
                     $_save_data[$taxonomy][] = $term_id;
                 }
             }
         }
         wp_delete_object_term_relationships($child->ID, array_keys($save_fields['taxonomies']));
         foreach ($_save_data as $_taxonomy => $_terms) {
             wp_set_post_terms($child->ID, $_terms, $_taxonomy, $append = false);
         }
     }
     // Unset non-types
     unset($save_fields['_wp_title'], $save_fields['_wp_body'], $save_fields['parents'], $save_fields['taxonomies']);
     /**
      * add filter to remove field name from error message
      */
     /** This filter is toolset-common/toolset-forms/classes/class.validation.php */
     add_filter('toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1);
     /**
      * UPDATE Loop over fields
      */
     foreach ($save_fields as $slug => $value) {
         if (defined('WPTOOLSET_FORMS_VERSION')) {
             // Get field by slug
             $field = wpcf_fields_get_field_by_slug(str_replace(WPCF_META_PREFIX, '', $slug));
             if (empty($field)) {
                 continue;
             }
             // Set config
             $config = wptoolset_form_filter_types_field($field, $child->ID);
             // Check if valid
             $valid = wptoolset_form_validate_field('post', $config, $value);
             if (is_wp_error($valid)) {
                 $errors = $valid->get_error_data();
                 $msg = sprintf(__('Child post "%s" field "%s" not updated:', 'wpcf'), $child->post_title, $field['name']);
                 wpcf_admin_message_store($msg . ' ' . implode(', ', $errors), 'error');
                 continue;
             }
         }
         $this->cf->set($child, $field);
         $this->cf->context = 'post_relationship';
         $this->cf->save($value);
     }
     /**
      * save feature image
      */
     if (isset($save_fields['_wp_featured_image'])) {
         if ($save_fields['_wp_featured_image']) {
             set_post_thumbnail($updated_id, $save_fields['_wp_featured_image']);
         } else {
             delete_post_thumbnail($updated_id);
         }
     }
     remove_filter('toolset_common_validation_add_field_name_to_error', '__return_false', 1234, 1);
     do_action('wpcf_relationship_save_child', $child, $parent);
     clean_post_cache($parent->ID);
     clean_post_cache($child->ID);
     // Added because of caching meta 1.5.4
     wp_cache_flush();
     return true;
 }
Exemplo n.º 23
0
 public function wa_delete_post_thumbnail($post_id = false)
 {
     $is_ajax = false;
     if (!$post_id) {
         $post_id = $_POST['post_id'];
         $is_ajax = true;
     }
     $return = delete_post_thumbnail($post_id);
     if ($is_ajax) {
         wp_send_json(array('success' => $return));
     } else {
         return $return;
     }
 }
Exemplo n.º 24
0
/**
 * Process the post and Validate all. Saves or update the post and post meta.
 *
 * @package BuddyForms
 * @since 0.3 beta
 */
function buddyforms_process_post($args = array())
{
    global $current_user, $buddyforms;
    do_action('buddyforms_process_post_start', $args);
    $hasError = false;
    $info_message = '';
    get_currentuserinfo();
    extract(shortcode_atts(array('post_type' => '', 'the_post' => 0, 'post_id' => 0, 'post_parent' => 0, 'revision_id' => false, 'form_slug' => 0, 'redirect_to' => $_SERVER['REQUEST_URI']), $args));
    if (isset($_POST['bf_post_type'])) {
        $post_type = $_POST['bf_post_type'];
    }
    if ($post_id != 0) {
        if (!empty($revision_id)) {
            $the_post = get_post($revision_id);
        } else {
            $post_id = apply_filters('bf_create_edit_form_post_id', $post_id);
            $the_post = get_post($post_id);
        }
        // Check if the user is author of the post
        $user_can_edit = false;
        if ($the_post->post_author == $current_user->ID) {
            $user_can_edit = true;
        }
        $user_can_edit = apply_filters('buddyforms_user_can_edit', $user_can_edit);
        if ($user_can_edit == false) {
            $args = array('hasError' => true, 'error_message' => __('You are not allowed to edit this post. What are you doing here?', 'buddyforms'));
            return $args;
        }
    }
    // check if the user has the roles and capabilities
    $user_can_edit = false;
    if ($post_id == 0 && current_user_can('buddyforms_' . $form_slug . '_create')) {
        $user_can_edit = true;
    } elseif ($post_id != 0 && current_user_can('buddyforms_' . $form_slug . '_edit')) {
        $user_can_edit = true;
    }
    $user_can_edit = apply_filters('buddyforms_user_can_edit', $user_can_edit);
    if ($user_can_edit == false) {
        $args = array('hasError' => true, 'error_message' => __('You do not have the required user role to use this form', 'buddyforms'));
        return $args;
    }
    // If post_id == 0 a new post is created
    if ($post_id == 0) {
        require_once ABSPATH . 'wp-admin/includes/admin.php';
        $the_post = get_default_post_to_edit($post_type);
    }
    if (isset($buddyforms[$form_slug]['form_fields'])) {
        $customfields = $buddyforms[$form_slug]['form_fields'];
    }
    $comment_status = $buddyforms[$form_slug]['comment_status'];
    if (isset($_POST['comment_status'])) {
        $comment_status = $_POST['comment_status'];
    }
    $post_excerpt = '';
    if (isset($_POST['post_excerpt'])) {
        $post_excerpt = $_POST['post_excerpt'];
    }
    $action = 'save';
    $post_status = $buddyforms[$form_slug]['status'];
    if ($post_id != 0) {
        $action = 'update';
        $post_status = get_post_status($post_id);
    }
    if (isset($_POST['status'])) {
        $post_status = $_POST['status'];
    }
    $args = array('post_id' => $post_id, 'action' => $action, 'form_slug' => $form_slug, 'post_type' => $post_type, 'post_excerpt' => $post_excerpt, 'post_author' => $current_user->ID, 'post_status' => $post_status, 'post_parent' => $post_parent, 'comment_status' => $comment_status);
    extract($args = buddyforms_update_post($args));
    /*
     * Check if the update or insert was successful
     */
    if (!is_wp_error($post_id)) {
        // Check if the post has post meta / custom fields
        if (isset($customfields)) {
            $customfields = bf_update_post_meta($post_id, $customfields);
        }
        if (isset($_POST['featured_image'])) {
            set_post_thumbnail($post_id, $_POST['featured_image']);
        } else {
            delete_post_thumbnail($post_id);
        }
        // Save the Form slug as post meta
        update_post_meta($post_id, "_bf_form_slug", $form_slug);
    } else {
        $hasError = true;
        $error_message = $post_id->get_error_message();
    }
    // Display the message
    if (!$hasError) {
        if (isset($_POST['post_id']) && !empty($_POST['post_id'])) {
            $info_message .= __('The ', 'buddyforms') . $buddyforms[$form_slug]['singular_name'] . __(' 1has been successfully updated ', 'buddyforms');
            $form_notice = '<div class="info alert">' . $info_message . '</div>';
        } else {
            $info_message .= __('The ', 'buddyforms') . $buddyforms[$form_slug]['singular_name'] . __(' has been successfully created ', 'buddyforms');
            $form_notice = '<div class="info alert">' . $info_message . '</div>';
        }
    } else {
        if (empty($error_message)) {
            $error_message = __('Error! There was a problem submitting the post ;-(', 'buddyforms');
        }
        $form_notice = '<div class="error alert">' . $error_message . '</div>';
        if (!empty($fileError)) {
            $form_notice = '<div class="error alert">' . $fileError . '</div>';
        }
    }
    do_action('buddyforms_after_save_post', $post_id);
    $args2 = array('hasError' => $hasError, 'form_notice' => $form_notice, 'customfields' => $customfields, 'redirect_to' => $redirect_to, 'form_slug' => $form_slug);
    $args = array_merge($args, $args2);
    do_action('buddyforms_process_post_end', $args);
    return $args;
}
 protected function parse_and_set_featured_image($post_id, $delete_featured_image, $featured_image)
 {
     if ($delete_featured_image) {
         delete_post_thumbnail($post_id);
         return;
     }
     $featured_image = (string) $featured_image;
     // if we got a post ID, we can just set it as the thumbnail
     if (ctype_digit($featured_image) && 'attachment' == get_post_type($featured_image)) {
         set_post_thumbnail($post_id, $featured_image);
         return $featured_image;
     }
     $featured_image_id = $this->handle_media_sideload($featured_image, $post_id);
     if (empty($featured_image_id) || !is_int($featured_image_id)) {
         return false;
     }
     set_post_thumbnail($post_id, $featured_image_id);
     return $featured_image_id;
 }
Exemplo n.º 26
0
/**
 * @access private
 *
 * @uses current_user_can()             Checks user capabilities given string
 * @uses delete_post_thumbnail()        Deletes post thumbnail given thumbnail id
 * @uses set_post_thumbnail()           Sets post thumbnail given post_id and thumbnail_id
 * @uses wpsc_the_product_thumbnail()   Returns URL to the product thumbnail
 *
 * @return array    $response           Includes the thumbnail URL and success bool value
 */
function _wpsc_ajax_set_variation_product_thumbnail()
{
    $response = array('success' => false);
    $post_ID = intval($_POST['post_id']);
    if (current_user_can('edit_post', $post_ID)) {
        $thumbnail_id = intval($_POST['thumbnail_id']);
        if ($thumbnail_id == '-1') {
            delete_post_thumbnail($post_ID);
        }
        set_post_thumbnail($post_ID, $thumbnail_id);
        $thumbnail = wpsc_the_product_thumbnail(50, 50, $post_ID, '');
        if (!$thumbnail) {
            $thumbnail = WPSC_CORE_IMAGES_URL . '/no-image-uploaded.gif';
        }
        $response['src'] = $thumbnail;
        $response['success'] = true;
    }
    echo json_encode($response);
    exit;
}
Exemplo n.º 27
0
/**
 * Ajax handler for setting the featured image.
 *
 * @since 3.1.0
 */
function wp_ajax_set_post_thumbnail()
{
    $json = !empty($_REQUEST['json']);
    // New-style request
    $post_ID = intval($_POST['post_id']);
    if (!current_user_can('edit_post', $post_ID)) {
        wp_die(-1);
    }
    $thumbnail_id = intval($_POST['thumbnail_id']);
    if ($json) {
        check_ajax_referer("update-post_{$post_ID}");
    } else {
        check_ajax_referer("set_post_thumbnail-{$post_ID}");
    }
    if ($thumbnail_id == '-1') {
        if (delete_post_thumbnail($post_ID)) {
            $return = _wp_post_thumbnail_html(null, $post_ID);
            $json ? wp_send_json_success($return) : wp_die($return);
        } else {
            wp_die(0);
        }
    }
    if (set_post_thumbnail($post_ID, $thumbnail_id)) {
        $return = _wp_post_thumbnail_html($thumbnail_id, $post_ID);
        $json ? wp_send_json_success($return) : wp_die($return);
    }
    wp_die(0);
}
Exemplo n.º 28
0
function wpdm_save_package_data_frontend($post)
{
    global $wpdb, $current_user;
    get_currentuserinfo();
    if (!is_user_logged_in() || !isset($_POST['file']) || !isset($_POST['act']) || !in_array($_POST['act'], array('_ap_wpdm', '_ep_wpdm'))) {
        return;
    }
    $cdata = get_post_custom($post);
    foreach ($cdata as $k => $v) {
        $tk = str_replace("__wpdm_", "", $k);
        if (!isset($_POST['file'][$tk]) && $tk != $k) {
            delete_post_meta($post, $k);
        }
    }
    if (isset($_POST['file']['preview'])) {
        $preview = $_POST['file']['preview'];
        $attachment_id = $wpdb->get_var($wpdb->prepare("SELECT ID FROM {$wpdb->posts} WHERE guid='%s';", $preview));
        set_post_thumbnail($post, $attachment_id);
        unset($_POST['file']['preview']);
    } else {
        delete_post_thumbnail($post);
    }
    foreach ($_POST['file'] as $meta_key => $meta_value) {
        $key_name = "__wpdm_" . $meta_key;
        update_post_meta($post, $key_name, $meta_value);
    }
    update_post_meta($post, '__wpdm_masterkey', uniqid());
    if (isset($_POST['reset_key']) && $_POST['reset_key'] == 1) {
        update_post_meta($post, '__wpdm_masterkey', uniqid());
    }
    //do_action('after_update_package',$post, $_POST['file']);
}
 /**
  * Save product images
  *
  * @since 2.2
  * @param array $images
  * @param int $id
  */
 protected function save_product_images($id, $images)
 {
     if (is_array($images)) {
         $gallery = array();
         foreach ($images as $image) {
             if (isset($image['position']) && $image['position'] == 0) {
                 $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
                 if (0 === $attachment_id && isset($image['src'])) {
                     $upload = $this->upload_product_image(esc_url_raw($image['src']));
                     if (is_wp_error($upload)) {
                         throw new WC_API_Exception('woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400);
                     }
                     $attachment_id = $this->set_product_image_as_attachment($upload, $id);
                 }
                 set_post_thumbnail($id, $attachment_id);
             } else {
                 $attachment_id = isset($image['id']) ? absint($image['id']) : 0;
                 if (0 === $attachment_id && isset($image['src'])) {
                     $upload = $this->upload_product_image(esc_url_raw($image['src']));
                     if (is_wp_error($upload)) {
                         throw new WC_API_Exception('woocommerce_api_cannot_upload_product_image', $upload->get_error_message(), 400);
                     }
                     $gallery[] = $this->set_product_image_as_attachment($upload, $id);
                 } else {
                     $gallery[] = $attachment_id;
                 }
             }
             // Set the image alt if present.
             if (!empty($image['alt'])) {
                 update_post_meta($attachment_id, '_wp_attachment_image_alt', wc_clean($image['alt']));
             }
             // Set the image title if present.
             if (!empty($image['title'])) {
                 wp_update_post(array('ID' => $attachment_id, 'post_title' => $image['title']));
             }
         }
         if (!empty($gallery)) {
             update_post_meta($id, '_product_image_gallery', implode(',', $gallery));
         }
     } else {
         delete_post_thumbnail($id);
         update_post_meta($id, '_product_image_gallery', '');
     }
 }
Exemplo n.º 30
0
function fifu_save_properties($post_id)
{
    if (isset($_POST['fifu_input_url'])) {
        update_post_meta($post_id, 'fifu_image_url', esc_url($_POST['fifu_input_url']));
        if (get_option('fifu_attachment_id') && !get_post_thumbnail_id($post_id) && esc_url($_POST['fifu_input_url'])) {
            set_post_thumbnail($post_id, get_option('fifu_attachment_id'));
        }
    }
    if (isset($_POST['fifu_input_alt'])) {
        update_post_meta($post_id, 'fifu_image_alt', wp_strip_all_tags($_POST['fifu_input_alt']));
    }
    if (get_post_type(get_the_ID()) == 'product') {
        $count = 10;
        for ($i = 0; $i < 10; $i++) {
            if (isset($_POST['fifu_input_url_' . $i]) && isset($_POST['fifu_input_alt_' . $i])) {
                if ($_POST['fifu_input_url_' . $i] != '' && $_POST['fifu_input_alt_' . $i] != '') {
                    update_post_meta($post_id, 'fifu_image_url_' . $i, esc_url($_POST['fifu_input_url_' . $i]));
                    update_post_meta($post_id, 'fifu_image_alt_' . $i, wp_strip_all_tags($_POST['fifu_input_alt_' . $i]));
                    if (get_option('fifu_attachment_id') && !get_post_thumbnail_id($post_id) && esc_url($_POST['fifu_input_url_' . $i])) {
                        set_post_thumbnail($post_id, get_option('fifu_attachment_id'));
                    }
                } else {
                    delete_post_meta($post_id, 'fifu_image_url_' . $i);
                    delete_post_meta($post_id, 'fifu_image_alt_' . $i);
                    $count--;
                }
            }
        }
        if ($count == 0 && (isset($_POST['fifu_input_url']) && !esc_url($_POST['fifu_input_url'])) && get_post_thumbnail_id($post_id) == get_option('fifu_attachment_id')) {
            delete_post_thumbnail($post_id);
        }
    } else {
        if (isset($_POST['fifu_input_url']) && !esc_url($_POST['fifu_input_url']) && get_post_thumbnail_id($post_id) == get_option('fifu_attachment_id')) {
            delete_post_thumbnail($post_id);
        }
    }
}