class-media-library-organizer-attachment.php 9.27 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482
<?php
/**
 * Attachment class.
 *
 * @package Media_Library_Organizer
 * @author WP Media Library
 */

/**
 * Represents an existing Attachment in the Media Library.
 *
 * @since   1.0.5
 */
class Media_Library_Organizer_Attachment {

	/**
	 * Holds the Attachment ID
	 *
	 * @since   1.0.6
	 *
	 * @var     int
	 */
	private $attachment_id;

	/**
	 * Holds the Attachment Title
	 *
	 * @since   1.0.6
	 *
	 * @var     string
	 */
	private $title;

	/**
	 * Holds the Attachment Alt Text
	 *
	 * @since   1.0.6
	 *
	 * @var     string
	 */
	private $alt_text;

	/**
	 * Holds the Attachment Caption
	 *
	 * @since   1.0.6
	 *
	 * @var     string
	 */
	private $caption;

	/**
	 * Holds the Attachment Description
	 *
	 * @since   1.0.6
	 *
	 * @var     string
	 */
	private $description;

	/**
	 * Holds the Attachment Terms
	 *
	 * @since   1.0.6
	 *
	 * @var     array
	 */
	private $terms;

	/**
	 * Creates a new Attachment object, representing the given Attachment ID.
	 *
	 * @since   1.0.5
	 *
	 * @param   int $attachment_id  Attachment ID.
	 */
	public function __construct( $attachment_id ) {

		// Get attachment.
		$attachment = get_post( $attachment_id );

		// Store attachment data in class.
		$this->attachment_id = $attachment_id;
		$this->set_title( $attachment->post_title );
		$this->set_alt_text( get_post_meta( $this->attachment_id, '_wp_attachment_image_alt', true ) );
		$this->set_caption( $attachment->post_excerpt );
		$this->set_description( $attachment->post_content );
		$this->set_filename( basename( get_attached_file( $attachment_id ) ) );

		// Iterate through Registered Taxonomies.
		foreach ( Media_Library_Organizer()->get_class( 'taxonomies' )->get_taxonomies() as $taxonomy_name => $taxonomy ) {
			$this->set_terms(
				$taxonomy_name,
				wp_get_object_terms(
					$this->attachment_id,
					$taxonomy_name,
					array(
						'fields' => 'ids',
					)
				)
			);
		}

	}

	/**
	 * Returns the ID for the current Attachment
	 *
	 * @since   1.1.6
	 *
	 * @return  int  ID
	 */
	public function get_attachment_id() {

		return $this->attachment_id;

	}

	/**
	 * Returns the Title for the current Attachment
	 *
	 * @since   1.0.5
	 *
	 * @return  string  Title
	 */
	public function get_title() {

		return $this->title;

	}

	/**
	 * Returns the Alt Text for the current Attachment
	 *
	 * @since   1.0.5
	 *
	 * @return  string  Alt Text
	 */
	public function get_alt_text() {

		return $this->alt_text;

	}

	/**
	 * Returns the Caption for the current Attachment
	 *
	 * @since   1.0.5
	 *
	 * @return  string  Caption
	 */
	public function get_caption() {

		return $this->caption;

	}

	/**
	 * Returns the Description for the current Attachment
	 *
	 * @since   1.0.5
	 *
	 * @return  string  Description
	 */
	public function get_description() {

		return $this->description;

	}

	/**
	 * Returns the Terms for the current Attachment
	 *
	 * @since   1.0.5
	 *
	 * @param   string $taxonomy_name  Taxonomy Name.
	 * @return  array                   Terms
	 */
	public function get_terms( $taxonomy_name ) {

		return $this->terms[ $taxonomy_name ];

	}

	/**
	 * Returns the Filename for the current Attachment
	 *
	 * @since   1.1.6
	 *
	 * @return  string  Filename
	 */
	public function get_filename() {

		return $this->filename;

	}

	/**
	 * Sets the Title for the current Attachment
	 * To save the Title, subsequently call update().
	 *
	 * @since   1.0.5
	 *
	 * @param   string $title  Title.
	 */
	public function set_title( $title ) {

		$this->title = sanitize_text_field( $title );

	}

	/**
	 * Sets the Alt Text for the current Attachment
	 * To save the Alt Text, subsequently call update().
	 *
	 * @since   1.0.5
	 *
	 * @param   string $alt_text   Alt Text.
	 */
	public function set_alt_text( $alt_text ) {

		$this->alt_text = sanitize_text_field( $alt_text );

	}

	/**
	 * Sets the Caption for the current Attachment
	 * To save the Caption, subsequently call update().
	 *
	 * @since   1.0.5
	 *
	 * @param   string $caption    Caption.
	 */
	public function set_caption( $caption ) {

		$this->caption = sanitize_text_field( $caption );

	}

	/**
	 * Sets the Description for the current Attachment
	 * To save the Description, subsequently call update().
	 *
	 * @since   1.0.5
	 *
	 * @param   string $description    Description.
	 */
	public function set_description( $description ) {

		$this->description = sanitize_text_field( $description );

	}

	/**
	 * Sets the Media Categories for the current Attachment
	 * To save the Media Categories, subsequently call update().
	 *
	 * @since   1.0.5
	 *
	 * @param   string $taxonomy_name       Taxonomy Name.
	 * @param   array  $terms               Media Categories.
	 */
	public function set_terms( $taxonomy_name, $terms ) {

		// Set this Taxonomy's Terms array if it hasn't been set.
		if ( ! isset( $this->terms[ $taxonomy_name ] ) ) {
			$this->terms[ $taxonomy_name ] = array();
		}

		// Bail if no Terms were assigned.
		if ( ! is_array( $terms ) ) {
			$this->terms[ $taxonomy_name ] = array();
			return;
		}
		if ( ! count( $terms ) ) {
			$this->terms[ $taxonomy_name ] = array();
			return;
		}

		// Cast Terms as Term IDs.
		foreach ( $terms as $index => $term_id ) {
			$terms[ $index ] = absint( sanitize_text_field( $term_id ) );
		}

		// Assign Term IDs.
		$this->terms[ $taxonomy_name ] = $terms;

	}

	/**
	 * Sets the Filename for the current Attachment
	 *
	 * @since   1.1.6
	 *
	 * @param   string $filename    Filename.
	 */
	public function set_filename( $filename ) {

		$this->filename = $filename;

	}

	/**
	 * Appends Terms for the current Attachment, preseving
	 * any existing Terms.
	 *
	 * To save the Terms, subsequently call update().
	 *
	 * @since   1.1.1
	 *
	 * @param   string $taxonomy_name   Taxonomy Name.
	 * @param   array  $terms          Terms.
	 */
	public function append_terms( $taxonomy_name, $terms ) {

		// Set this Taxonomy's Terms array if it hasn't been set.
		if ( ! isset( $this->terms[ $taxonomy_name ] ) ) {
			$this->terms[ $taxonomy_name ] = array();
		}

		// Bail if no Terms were assigned.
		if ( ! is_array( $terms ) ) {
			return;
		}
		if ( ! count( $terms ) ) {
			return;
		}

		// Cast Terms as Term IDs.
		foreach ( $terms as $index => $term_id ) {
			$terms[ $index ] = absint( sanitize_text_field( $term_id ) );
		}

		// Append.
		$this->terms[ $taxonomy_name ] = array_merge( $this->terms[ $taxonomy_name ], $terms );

	}

	/**
	 * Removes all Terms for the current Attachment.
	 *
	 * To save changes, subsequently call update().
	 *
	 * @since   1.4.0
	 *
	 * @param   string $taxonomy_name   Taxonomy Name.
	 */
	public function remove_terms( $taxonomy_name ) {

		$this->terms[ $taxonomy_name ] = array( -1 );

	}

	/**
	 * Determines if the current Attachment has a Title
	 *
	 * @since   1.0.5
	 *
	 * @return  bool    Has Title
	 */
	public function has_title() {

		return ( ! empty( $this->title ) );

	}

	/**
	 * Determines if the current Attachment has Alt Text
	 *
	 * @since   1.0.5
	 *
	 * @return  bool    Has Alt Text
	 */
	public function has_alt_text() {

		return ( ! empty( $this->alt_text ) );

	}

	/**
	 * Determines if the current Attachment has a Caption
	 *
	 * @since   1.0.5
	 *
	 * @return  bool    Has Caption
	 */
	public function has_caption() {

		return ( ! empty( $this->caption ) );

	}

	/**
	 * Determines if the current Attachment has a Description
	 *
	 * @since   1.0.5
	 *
	 * @return  bool    Has Description
	 */
	public function has_description() {

		return ( ! empty( $this->description ) );

	}

	/**
	 * Determines if the current Attachment has Media Categories
	 *
	 * @since   1.0.5
	 *
	 * @param   string $taxonomy_name  Taxonomy Name.
	 * @return  bool                    Has Terms
	 */
	public function has_terms( $taxonomy_name ) {

		if ( ! isset( $this->terms[ $taxonomy_name ] ) ) {
			return false;
		}
		if ( ! count( $this->terms[ $taxonomy_name ] ) ) {
			return false;
		}

		return true;

	}

	/**
	 * Updates the Attachment in the WordPress database.
	 *
	 * @since   1.0.5
	 *
	 * @return  mixed   WP_Error | bool
	 */
	public function update() {

		// Update the Post (Attachment).
		$result = wp_update_post(
			array(
				'ID'           => (int) $this->attachment_id,
				'post_title'   => $this->title,
				'post_excerpt' => $this->caption,
				'post_content' => $this->description,
			),
			true
		);

		// Bail if an error occured.
		if ( is_wp_error( $result ) ) {
			return $result;
		}

		// Update the Alt Tag.
		update_post_meta( $this->attachment_id, '_wp_attachment_image_alt', $this->alt_text );

		// Iterate through Registered Taxonomies.
		foreach ( Media_Library_Organizer()->get_class( 'taxonomies' )->get_taxonomies() as $taxonomy_name => $taxonomy ) {
			// Skip if no Terms to assign to this Taxonomy.
			if ( ! $this->has_terms( $taxonomy_name ) ) {
				continue;
			}

			// If there is only a single Term assigned to this Attachment and it is -1, we need to remove all Terms
			// assigned to this Attachment.
			if ( count( $this->terms[ $taxonomy_name ] ) === 1 && $this->terms[ $taxonomy_name ][0] === -1 ) {
				// Delete Terms.
				$result = wp_set_object_terms( $this->attachment_id, '', $taxonomy_name, false );
			} else {
				// Update Terms.
				$result = wp_set_object_terms( $this->attachment_id, $this->terms[ $taxonomy_name ], $taxonomy_name, false );
			}

			// Bail if an error occured.
			if ( is_wp_error( $result ) ) {
				return $result;
			}
		}

		return true;

	}

}