Esempio n. 1
0
/**
 * Install
 *
 * Runs on plugin install by setting up the post types, custom taxonomies,
 * flushing rewrite rules to initiate the new 'sell_media_item' slug and also
 * creates the plugin and populates the settings fields for those plugin
 * pages.
 *
 * @since 1.8.5
 * @global $wpdb
 * @global $wp_version
 * @return void
 */
function sell_media_install()
{
    $version = get_option('sell_media_version');
    if ($version && $version > SELL_MEDIA_VERSION) {
        return;
    }
    // Register Custom Post Types
    sell_media_register_post_types();
    // Register Taxonomies
    sell_media_register_taxonomies();
    // Flush the permalinks
    flush_rewrite_rules();
    // Don't forget registration hook is called
    // BEFORE! taxonomies are registered! therefore
    // these terms and taxonomies are NOT derived from our object!
    $settings = sell_media_get_plugin_options();
    $admin_columns = empty($settings->admin_columns) ? null : $settings->admin_columns;
    // Install new table for term meta
    $taxonomy_metadata = new SellMediaTaxonomyMetadata();
    $taxonomy_metadata->activate();
    $taxonomy = 'licenses';
    // Add Personal and Commerical default license terms
    $r_personal = wp_insert_term('Personal', $taxonomy, array('slug' => 'personal'));
    $r_commercial = wp_insert_term('Commercial', $taxonomy, array('slug' => 'commercial'));
    // Install protected folder for uploading files and prevent hotlinking
    $downloads_url = sell_media_get_upload_dir();
    if (wp_mkdir_p($downloads_url) && !file_exists($downloads_url . '/.htaccess')) {
        if ($file_handle = @fopen($downloads_url . '/.htaccess', 'w')) {
            fwrite($file_handle, 'deny from all');
            fclose($file_handle);
        }
    }
    // Add a new Customer role
    add_role('sell_media_customer', 'Customer', array('read' => true));
    // This is a new install so add the defaults to the options table
    if (empty($version)) {
        $defaults = sell_media_get_plugin_option_defaults();
        update_option(sell_media_get_current_plugin_id() . "_options", $defaults);
        // A version number exists, so run upgrades
    } else {
        require_once SELL_MEDIA_PLUGIN_DIR . '/inc/admin-upgrade.php';
    }
    // Update the version number
    update_option('sell_media_version', SELL_MEDIA_VERSION);
}
 /**
  * Parse IPTC info and move the uploaded file into the protected area
  *
  * In order to "protect" our uploaded file, we resize the original
  * file down to the largest WordPress size set in Media Settings.
  * Then we take the uploaded file and move it to the "protected area".
  * Last, we copy (rename) our resized uploaded file to be the original
  * file.
  *
  * @param $attachment_id As WordPress sees it in *postmeta table
  * "_wp_attached_file", i.e., YYYY/MM/file-name.ext
  * @since 1.0.1
  */
 public function move_image_from_attachment($attachment_id = null)
 {
     $original_file = get_attached_file($attachment_id);
     if (file_exists($original_file)) {
         $this->parse_iptc_info($original_file, $attachment_id);
         // Assign the FULL PATH to our destination file.
         $wp_upload_dir = wp_upload_dir();
         $destination_file = sell_media_get_upload_dir() . $wp_upload_dir['subdir'] . '/' . basename($original_file);
         $destination_dir = sell_media_get_upload_dir() . $wp_upload_dir['subdir'] . '/';
         // Check if the destination directory exists, i.e.
         // wp-content/uploads/sell_media/YYYY/MM if not we create it.
         if (!file_exists(dirname($destination_file))) {
             wp_mkdir_p(dirname($destination_file));
         }
         /**
          * Resize original file down to the largest size set in the Media Settings
          *
          * Determine which version of WP we are using.
          * Would rather check if the correct function exists
          * but the function 'image_make_intermediate_size' uses other
          * functions that are in trunk and not in 3.4
          */
         global $wp_version;
         if (version_compare($wp_version, '3.5', '>=')) {
             /**
              * Resize the "original" to our largest size set in the Media Settings.
              *
              * This creates a file named filename-[width]x[height].jpg
              * From here the "original" file is still in our uploads dir, its needed to create
              * the additional image sizes. Once we're done making the additional sizes, we rename
              * the filename-[width]x[height].jpg to filename.jpg, thus having a resized "original"
              * file.
              */
             $image_new_size = image_make_intermediate_size($original_file, get_option('large_size_w'), get_option('large_size_h'), false);
             /**
              * If for some reason the image resize fails we just fall back to the original image.
              * Example, the image the user is trying to sell is smaller than our "max width".
              */
             if (empty($image_new_size)) {
                 $resized_image = $original_file;
                 $keep_original = true;
             } else {
                 $keep_original = false;
                 $resized_image = $wp_upload_dir['path'] . '/' . $image_new_size['file'];
             }
             if (!file_exists($destination_file)) {
                 /**
                  * Move our originally upload file into the protected area
                  */
                 copy($original_file, $destination_file);
                 if (!$keep_original) {
                     unlink($original_file);
                 }
                 /**
                  * We rename our resize original file i.e., "filename-[width]x[height].jpg" located in our uploads directory
                  * to "filename.jpg"
                  */
                 $new_path_source = dirname($original_file) . '/' . basename($resized_image);
                 $new_path_destination = $original_file;
                 copy($new_path_source, $new_path_destination);
             }
         } else {
             $resized_image = image_resize($original_file, get_option('large_size_w'), get_option('large_size_h'), false, null, $wp_upload_dir['path'], 90);
             if (!file_exists($destination_file)) {
                 // Copy original to our protected area
                 @copy($original_file, $destination_file);
                 // Copy (rename) our resized image to the original
                 @copy($resized_image, dirname($resized_image) . '/' . basename($original_file));
             }
         }
     }
 }
Esempio n. 3
0
/**
 * Deletes the uploaded file in sell_media/ when the
 * trash bin is emptied.
 *
 * @since 1.0.4
 */
function sell_media_before_delete_post($postid, $attachment_id = null)
{
    $post_type = get_post_type($postid);
    if ($post_type != 'sell_media_item') {
        return;
    }
    /**
     * Get the attachment/thumbnail file so we can replace the "original", i.e.
     * lower quality "original" with the file in the protected area.
     */
    $attached_file = get_post_meta($postid, '_sell_media_attached_file', true);
    if (empty($attachment_id)) {
        $attachment_id = get_post_meta($postid, '_sell_media_attachment_id', true);
    } else {
        delete_post_meta($attachment_id, '_sell_media_for_sale_product_id');
    }
    delete_post_meta($attachment_id, '_sell_media_for_sale_product_id');
    $attached_file_path = sell_media_get_upload_dir() . '/' . $attached_file;
    // Delete the file stored in sell_media
    if (file_exists($attached_file_path)) {
        /**
         * Due to how WordPress handles attachments that are NOT
         * images we check if the "_wp_attached_file" is in fact
         * stored in the sell_media/ directory, i.e. there's only
         * "one" copy of the attachment.
         */
        $pos = strpos($attached_file, 'sell_media/');
        if ($pos !== false) {
            $attached_file = str_replace('sell_media/', '', $attached_file);
        }
        // Copy our "original" back
        @copy($attached_file_path, $wp_upload_dir['basedir'] . '/' . $attached_file);
        @unlink($attached_file_path);
    }
    return;
}