plugin_locale = 'drawattention-fi'; $this->plugin_options_slug = 'drag-drop-featured-image'; $this->plugin_dirname = dirname( plugin_basename( __FILE__ ) ); $this->plugin_directory = DrawAttention::get_plugin_url() . '/public/includes/lib/drag-drop-featured-image/'; $this->selected_post_types = $this->get_option_post_types(); $this->selected_file_types = $this->get_option_file_types(); $this->selected_page_reload = $this->get_option_page_reload(); // Set WordPress version: $this->wordpress_version = substr( str_replace( '.', '', $wp_version ), 0, 2 ); } /** * Get selected post types. * * @return array */ public function get_option_post_types() { return array( 'da_image' ); } /** * Get selected file types. * * @return array */ public function get_option_file_types() { return array( 'jpg', 'jpeg', 'png', 'gif', 'webp' ); } /** * Get page reload status. * * @return boolean */ public function get_option_page_reload() { return 0; } /** * Load plugin textdomain. */ public function load_textdomain() { load_plugin_textdomain( 'drawattention-fi', false, dirname( plugin_basename( __FILE__ ) ) . '/languages/' ); } /** * Print current post ID in header. */ public function print_header_post_id() { global $post, $current_screen; if ( $current_screen->base === 'post' ) { echo ' '; } } /** * Toggle the metaboxes. */ public function toggle_meta_box_functionality() { global $wp_meta_boxes; // Fetch selected post types: $selected = $this->get_option_post_types(); // Add theme support: add_theme_support( 'post-thumbnails', $selected ); // Remove default meta box: foreach ( $selected as $post_type ) { add_post_type_support( $post_type, 'thumbnail' ); remove_meta_box( 'postimagediv', $post_type, 'side' ); // needed for reported conflict with plugin: conditionally-display-featured-image-on-singular-pages if ( isset( $wp_meta_boxes[ $post_type ]['side']['low']['postimagediv'] ) && false === $wp_meta_boxes[ $post_type ]['side']['low']['postimagediv'] ) { unset( $wp_meta_boxes[ $post_type ]['side']['low']['postimagediv'] ); } remove_meta_box( 'postimagediv', $post_type, 'normal' ); remove_meta_box( 'postimagediv', $post_type, 'advanced' ); } // Add the enhanced meta box: foreach ( $selected as $post_type ) { add_meta_box( 'drag_to_upload', __( 'Image' ), array( &$this, 'updated_upload_meta_box' ), $post_type, 'side', 'default' ); } } /** * Handle script loading. * * @param $hook */ public function handle_plugin_script_loading( $hook ) { // Globalize: global $post; // Get current post type: if ( ! is_null( $post ) ) { $current_post_type = get_post_type( $post->ID ); } // Post edit screen: if ( $hook == 'post.php' || $hook == 'post-new.php' ) { if ( in_array( $current_post_type, $this->selected_post_types ) ) { // Stylesheets: wp_register_style( 'dgd_uploaderStyle', $this->plugin_directory . 'assets/style/drag-drop-uploader.css', false ); wp_enqueue_style( 'dgd_uploaderStyle' ); // Scripts: wp_enqueue_script( 'plupload-all' ); wp_register_script( 'dgd_uploaderScript', $this->plugin_directory . 'assets/scripts/drag-drop-uploader.js', 'jquery' ); wp_enqueue_script( 'dgd_uploaderScript' ); // Localize JavaScript: wp_localize_script( 'dgd_uploaderScript', 'dgd_strings', array( 'panel' => array( 'title' => __( 'Set featured image' ), 'button' => __( 'Set featured image' ), ), ) ); } } // Plugin options screen: if ( $hook === 'settings_page_' . $this->plugin_options_slug ) { // Stylesheets: wp_register_style( 'itoggle_style', $this->plugin_directory . 'assets/style/engage.itoggle.css', false ); wp_register_style( 'dgd_panelStyle', $this->plugin_directory . 'assets/style/drag-to-feature.css', false ); wp_enqueue_style( 'itoggle_style' ); wp_enqueue_style( 'dgd_panelStyle' ); // Scripts: wp_register_script( 'itoggle_script', $this->plugin_directory . 'assets/scripts/engage.itoggle.1.7.min.js', 'jquery' ); wp_register_script( 'dgd_panelScript', $this->plugin_directory . 'assets/scripts/drag-to-feature.js', 'jquery' ); wp_enqueue_script( 'itoggle_script' ); wp_enqueue_script( 'dgd_panelScript' ); } } /** * Render options page. */ public function render_options_page() { require_once 'assets/views/options.php'; } /** * Byte formatting function: * * @param $bytes * @param int $precision * @return float */ public function format_bytes( $bytes, $precision = 2 ) { $units = array( 'B', 'KB', 'MB', 'GB', 'TB' ); $bytes = max( $bytes, 0 ); $pow = floor( ( $bytes ? log( $bytes ) : 0 ) / log( 1024 ) ); $pow = min( $pow, count( $units ) - 1 ); $bytes /= pow( 1024, $pow ); return round( $bytes, $precision ); } /** * Uploading functionality trigger. * (Most of the code comes from media.php and handlers.js) */ function updated_upload_meta_box() { ?>
ID ); ?> ID" ); ?>

Loading...

plugin_locale); ?>

wordpress_version >= 35 ) : ?> plugin_locale ); ?> plugin_locale ); ?>

'html5,silverlight,flash,html4', 'browse_button' => 'plupload-browse-button', 'container' => 'plupload-upload-ui', 'drop_element' => 'drag-drop-area', 'file_data_name' => 'async-upload', 'multiple_queues' => false, 'multi_selection' => false, 'max_file_size' => wp_max_upload_size() . 'b', 'url' => admin_url( 'admin-ajax.php' ), 'flash_swf_url' => includes_url( 'js/plupload/plupload.flash.swf' ), 'silverlight_xap_url' => includes_url( 'js/plupload/plupload.silverlight.xap' ), 'filters' => array( array( 'title' => __( 'Allowed Files', $this->plugin_locale ), 'extensions' => implode( ',', $this->get_option_file_types() ), ), ), 'multipart' => true, 'urlstream_upload' => true, // Additional parameters: 'multipart_params' => array( '_ajax_nonce' => wp_create_nonce( 'photo-upload' ), 'action' => 'dgd_upload_featured_image', 'postID' => $post->ID, ), ); // Apply filters to initiate plupload: $plupload_init = apply_filters( 'plupload_init', $plupload_init ); ?> true, 'action' => 'dgd_upload_featured_image', ) ); // Fetch post ID: $post_id = $_POST['postID']; // Insert uploaded file as attachment: $attach_id = wp_insert_attachment( array( 'post_mime_type' => $status['type'], 'post_title' => preg_replace( '/\.[^.]+$/', '', basename( $file['name'] ) ), 'post_content' => '', 'post_status' => 'inherit', ), $status['file'], $post_id ); // Include the image handler library: require_once ABSPATH . 'wp-admin/includes/image.php'; // Generate meta data and update attachment: $attach_data = wp_generate_attachment_metadata( $attach_id, $status['file'] ); wp_update_attachment_metadata( $attach_id, $attach_data ); // Check for current meta (update / add): if ( $prevValue = get_post_meta( $post_id, '_thumbnail_id', true ) ) { update_post_meta( $post_id, '_thumbnail_id', $attach_id, $prevValue ); } else { add_post_meta( $post_id, '_thumbnail_id', $attach_id ); } // Get image sizes and correct thumb: $croppedImage = wp_get_attachment_image_src( $attach_id, 'full' ); $imageDetails = getimagesize( $croppedImage[0] ); // Create response array: $uploadResponse = array( 'image' => $croppedImage[0], 'width' => $imageDetails[0], 'height' => $imageDetails[1], 'postID' => $post_id, ); // Return response and exit: die( json_encode( $uploadResponse ) ); } /** * AJAX function: Set featured image. */ public function ajax_set_featured_image() { $postID = isset( $_POST['postID'] ) ? (int) $_POST['postID'] : false; $attachmentID = isset( $_POST['attachmentID'] ) ? (int) $_POST['attachmentID'] : false; $dgd_set_featured_image_nonce = isset( $_POST['nonce'] ) ? $_POST['nonce'] : false; if ( $postID && $attachmentID ) { if ( current_user_can( 'edit_post', $postID ) && wp_verify_nonce( $dgd_set_featured_image_nonce, 'dgd_set_featured_image_nonce' ) ) { // Update / add post meta: if ( $status = get_post_meta( $postID, '_thumbnail_id', true ) ) { update_post_meta( $postID, '_thumbnail_id', $attachmentID ); } else { add_post_meta( $postID, '_thumbnail_id', $attachmentID ); } $response = array( 'response_code' => 200, 'response_content' => 'success', ); } else { $response = array( 'response_code' => 403, 'response_content' => __( 'Drag & Drop Feaured image: You do not have permission to edit this post!', $this->plugin_locale ), ); } } else { $response = array( 'response_code' => 500, 'response_content' => __( 'Drag & Drop Feaured image: Unknown post och attachment ID, please try again!', $this->plugin_locale ), ); } // Return response: die( json_encode( $response ) ); } /** * AJAX function: Get featured image. */ public function ajax_get_featured_image() { $postID = isset( $_POST['post_id'] ) ? (int) $_POST['post_id'] : false; if ( $postID ) { $featured = wp_get_attachment_image_src( get_post_thumbnail_id( $postID ), 'full' ); $response = array( 'response_code' => 200, 'response_content' => $featured[0], ); } else { $response = array( 'response_code' => 500, 'response_content' => __( 'Drag & Drop Feaured image: An error occured, please reload page!', $this->plugin_locale ), ); } // Return response: die( json_encode( $response ) ); } /** * Add body classes to the admin. * * @param $classes * @return string */ public function modify_admin_body_class( $classes ) { if ( $this->wordpress_version >= 38 ) { $classes .= ' wp38'; } return $classes; } }