PluginProbe
Social Media Auto Poster – Schedule & Publish to Buffer / 6.2.5
Social Media Auto Poster – Schedule & Publish to Buffer v6.2.5
6.2.5 6.2.4 6.2.3 6.2.2 6.2.1 6.2.0 6.1.2 6.1.1 6.1.0 6.0.9 6.0.8 6.0.7 6.0.6 6.0.5 6.0.4 6.0.3 6.0.2 6.0.1 6.0.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.8.6 All 126 releases
wp-to-buffer / lib / social / includes / class-media-library.php

class-media-library.php in Social Media Auto Poster – Schedule & Publish to Buffer 6.2.5, at lib/social/includes/class-media-library.php

195 lines 4.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Media Library class.
4 *
5 * @package WPZinc\Social
6 * @author WP Zinc
7 */
8
9 namespace WPZinc\Social;
10
11 /**
12 * Handles uploading and deleting images generated by this Plugin.
13 *
14 * @package WPZinc\Social
15 * @author WP Zinc
16 * @version 4.2.0
17 */
18 class Media_Library {
19
20 /**
21 * Holds the base object.
22 *
23 * @since 4.2.0
24 *
25 * @var object
26 */
27 public $base;
28
29 /**
30 * Constructor.
31 *
32 * @since 4.2.0
33 *
34 * @param object $base Base Plugin Class.
35 */
36 public function __construct( $base ) {
37
38 // Store base class.
39 $this->base = $base;
40
41 }
42
43 /**
44 * Uploads the given image path and file into the WordPress Media Library
45 *
46 * @since 4.2.0
47 *
48 * @param string $source Source Path and Filename.
49 * @param int $post_id Post ID.
50 * @param string|false $filename Target Filename to save source as.
51 * @param string $title Image Title (optional).
52 * @param string $caption Image Caption (optional).
53 * @param string $alt_tag Image Alt Tag (optional).
54 * @param string $description Image Description (optional).
55 * @return \WP_Error|int \WP_Error | Image ID
56 */
57 public function upload_local_image( $source, $post_id = 0, $filename = false, $title = '', $caption = '', $alt_tag = '', $description = '' ) {
58
59 // If GD support is available, enable it now.
60 if ( $this->is_gd_available() ) {
61 add_filter( 'wp_image_editors', array( $this, 'enable_gd_image_support' ) );
62 }
63
64 // Load required image / media classes and functions.
65 require_once ABSPATH . 'wp-admin/includes/image.php';
66 require_once ABSPATH . 'wp-admin/includes/file.php';
67 require_once ABSPATH . 'wp-admin/includes/media.php';
68
69 // Get image type.
70 $type = getimagesize( $source );
71 if ( ! isset( $type['mime'] ) ) {
72 return new \WP_Error(
73 sprintf(
74 /* translators: Image Source Path and Filename */
75 __( 'Could not identify MIME type of source image %s. Is this an image?', 'wp-to-buffer' ),
76 $source
77 )
78 );
79 }
80 list( $type, $ext ) = explode( '/', $type['mime'] );
81 unset( $type );
82
83 // Define image filename.
84 $file_array['name'] = ( $filename !== false ? $filename : basename( $source ) );
85 $file_array['tmp_name'] = $source;
86
87 // Add the extension to the filename if it doesn't exist.
88 // This happens if we streamed an image URL e.g. http://placehold.it/400x400.
89 if ( strpos( $file_array['name'], '.' . $ext ) === false ) {
90 $file_array['name'] .= '.' . $ext;
91 }
92
93 // Import the image into the Media Library.
94 $image_id = media_handle_sideload( $file_array, $post_id, '' );
95 if ( is_wp_error( $image_id ) ) {
96 return $image_id;
97 }
98
99 // Store the Plugin Name against the Attachment's meta.
100 update_post_meta( $image_id, '_' . $this->base->plugin->filter_name, 1 );
101
102 // If a title or caption has been defined, set them now.
103 if ( ! empty( $title ) || ! empty( $caption ) ) {
104 $attachment = get_post( $image_id );
105 wp_update_post(
106 array(
107 'ID' => $image_id,
108 'post_title' => sanitize_text_field( $title ),
109 'post_content' => sanitize_text_field( $description ),
110 'post_excerpt' => sanitize_text_field( $caption ),
111 )
112 );
113 }
114
115 // If an alt tag has been specified, set it now.
116 if ( ! empty( $alt_tag ) ) {
117 update_post_meta( $image_id, '_wp_attachment_image_alt', $alt_tag );
118 }
119
120 // Return the image ID.
121 return $image_id;
122
123 }
124
125 /**
126 * Deletes attachments that were added by an upload function in this class
127 * (i.e. that include the meta key flag)
128 *
129 * @since 4.2.0
130 */
131 public function cleanup() {
132
133 // Build query to fetch images created by the plugin.
134 $args = array(
135 'post_type' => 'attachment',
136 'post_status' => 'any',
137 'posts_per_page' => -1,
138 'meta_query' => array(
139 array(
140 'key' => '_' . $this->base->plugin->filter_name,
141 'value' => 1,
142 ),
143 ),
144 'fields' => 'ids',
145 );
146
147 // Get all Attachments belonging to the given Post IDs.
148 $attachments = new \WP_Query( $args );
149
150 // If no Attachments found, return false, as there's nothing to delete.
151 if ( count( $attachments->posts ) === 0 ) {
152 return false;
153 }
154
155 // Delete attachments.
156 foreach ( $attachments->posts as $attachment_id ) {
157 wp_delete_attachment( $attachment_id );
158 }
159
160 return true;
161
162 }
163
164 /**
165 * Flag to denote if the GD image processing library is available
166 *
167 * @since 4.2.0
168 *
169 * @return bool GD Library Available in PHP
170 */
171 private function is_gd_available() {
172
173 return extension_loaded( 'gd' ) && function_exists( 'gd_info' );
174
175 }
176
177 /**
178 * Force using the GD Image Library for processing WordPress Images.
179 *
180 * @since 4.2.0
181 *
182 * @param array $editors WordPress Image Editors.
183 * @return array WordPress Image Editors.
184 */
185 public function enable_gd_image_support( $editors ) {
186
187 $gd_editor = 'WP_Image_Editor_GD';
188 $editors = array_diff( $editors, array( $gd_editor ) );
189 array_unshift( $editors, $gd_editor );
190 return $editors;
191
192 }
193
194 }
195