class-media-library-organizer-common.php 16.9 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 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677
<?php
/**
 * Common class.
 *
 * @package Media_Library_Organizer
 * @author WP Media Library
 */

/**
 * Functions that don't particularly fit in one specific class.
 *
 * @since   1.0.0
 */
class Media_Library_Organizer_Common {

	/**
	 * Holds the base class object.
	 *
	 * @since   1.0.5
	 *
	 * @var     object
	 */
	public $base;

	/**
	 * Constructor
	 *
	 * @since   1.0.5
	 *
	 * @param   object $base    Base Plugin Class.
	 */
	public function __construct( $base ) {

		// Store base class.
		$this->base = $base;

	}

	/**
	 * Returns an array of Javascript DOM selectors to enable the keyword
	 * autocomplete functionality on.
	 *
	 * @since   1.1.6
	 *
	 * @return  array   Javascript DOM Selectors
	 */
	public function get_autocomplete_enabled_fields() {

		// Get fields.
		$fields = array(
			'input.wpzinc-autocomplete',
		);

		/**
		 * Defines an array of Javascript DOM selectors to enable the keyword
		 * autocomplete functionality on.
		 *
		 * @since   1.1.6
		 *
		 * @param   array   $fields  Supported Fields.
		 */
		$fields = apply_filters( 'media_library_organizer_defaults_common_get_autocomplete_enabled_fields', $fields );

		// Return filtered results.
		return $fields;

	}

	/**
	 * Helper method to return the author options for WP_Query calls
	 *
	 * @since   1.0.0
	 *
	 * @return  array   Author options
	 */
	public function get_author_options() {

		// Get users.
		$users = get_users();

		// Build options.
		foreach ( $users as $user ) {
			$options[ $user->ID ] = $user->user_login;
		}

		/**
		 * Defines the available Author options.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Authors (WordPress Users).
		 */
		$options = apply_filters( 'media_library_organizer_common_get_author_options', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the operator options for WP_Query calls
	 *
	 * @since   1.0.7
	 *
	 * @return  array           Operator Options
	 */
	public function get_operator_options() {

		// Build options.
		$options = array(
			'AND' => __( 'All', 'media-library-organizer' ),
			'OR'  => __( 'Any', 'media-library-organizer' ),
		);

		/**
		 * Defines the available comparison operator options.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Comparison Operators
		 */
		$options = apply_filters( 'media_library_organizer_common_get_operator_options', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the position options
	 *
	 * @since   1.0.7
	 *
	 * @return  array           Position Options
	 */
	public function get_position_options() {

		// Build options.
		$options = array(
			'top'        => __( 'Above', 'media-library-organizer' ),
			'bottom'     => __( 'Below', 'media-library-organizer' ),
			'top_bottom' => __( 'Above and Below', 'media-library-organizer' ),
		);

		/**
		 * Defines the available layout position options.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Layout Positions
		 */
		$options = apply_filters( 'media_library_organizer_common_get_position_options', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the file type options for WP_Query calls
	 *
	 * @since   1.1.1
	 *
	 * @return  array   File Type options
	 */
	public function get_file_type_options() {

		global $post_mime_types, $avail_post_mime_types;

		$options = array(
			'all' => __( 'All media items', 'media-library-organizer' ),
		);

		foreach ( $post_mime_types as $mime_type => $label ) {
			$options[ 'post_mime_type:' . esc_attr( $mime_type ) ] = $label[0];
		}

		$options['detached'] = __( 'Unattached', 'media-library-organizer' );
		$options['mine']     = _x( 'Mine', 'media items', 'media-library-organizer' );

		/**
		 * Defines the available file type options for WP_Query calls
		 *
		 * @since   1.1.1
		 *
		 * @param   array   $options                File Type Options.
		 * @param   array   $post_mime_types        Post MIME Types.
		 * @param   array   $avail_post_mime_types  Available Post MIME Types.
		 * @return  array                           File Type Options
		 */
		$options = apply_filters( 'media_library_organizer_common_get_file_type_options', $options, $post_mime_types, $avail_post_mime_types );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the orderby options for WP_Query calls
	 *
	 * @since   1.0.0
	 *
	 * @return  array   orderby options
	 */
	public function get_orderby_options() {

		// Build options.
		$options = array(
			'ID'        => __( 'Attachment ID', 'media-library-organizer' ),
			'author'    => __( 'Author (Uploader)', 'media-library-organizer' ),
			'date'      => __( 'Date', 'media-library-organizer' ),
			'name'      => __( 'Filename', 'media-library-organizer' ),
			'modified'  => __( 'Modified Date', 'media-library-organizer' ),
			'parent'    => __( 'Uploaded to', 'media-library-organizer' ),
			'title'     => __( 'Title', 'media-library-organizer' ),
			'post_date' => __( 'Uploaded Date', 'media-library-organizer' ),
		);

		/**
		 * Defines the available WP_Query compatible order by options.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Order By Options.
		 */
		$options = apply_filters( 'media_library_organizer_common_get_orderby_options', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the default orderby option
	 *
	 * @since   1.0.0
	 *
	 * @return  string  orderby default
	 */
	public function get_orderby_default() {

		$default = 'date';

		/**
		 * Defines the default order_by value for WP_Query queries
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Layout Positions.
		 */
		$default = apply_filters( 'media_library_organizer_common_get_orderby_default', $default );

		// Return filtered results.
		return $default;

	}

	/**
	 * Helper method to return the order options for WP_Query calls
	 *
	 * @since   1.0.0
	 *
	 * @return  array   order options
	 */
	public function get_order_options() {

		// Build options.
		$options = array(
			'ASC'  => __( 'Ascending (A-Z)', 'media-library-organizer' ),
			'DESC' => __( 'Descending (Z-A)', 'media-library-organizer' ),
		);

		/**
		 * Defines the available WP_Query compatible order options.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Order Options.
		 */
		$options = apply_filters( 'media_library_organizer_common_get_order_options', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the default order option
	 *
	 * @since   1.0.0
	 *
	 * @return  string  order default
	 */
	public function get_order_default() {

		$default = 'DESC';

		/**
		 * Defines the default order_by value for WP_Query queries
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Layout Positions.
		 */
		$default = apply_filters( 'media_library_organizer_common_get_order_default', $default );

		// Return filtered results.
		return $default;

	}

	/**
	 * Helper method to return the Attachment Display Settings: Alignment options
	 *
	 * @since   1.0.5
	 *
	 * @return  array   Alignment Options
	 */
	public function get_attachment_display_settings_alignment() {

		$options = array(
			'none'   => __( 'None', 'media-library-organizer' ),
			'left'   => __( 'Left', 'media-library-organizer' ),
			'center' => __( 'Center', 'media-library-organizer' ),
			'right'  => __( 'Right', 'media-library-organizer' ),
		);

		$options = apply_filters( 'media_library_organizer_common_get_attachment_display_settings_alignment', $options );

		return $options;

	}

	/**
	 * Helper method to return the Attachment Display Settings: Link To options
	 *
	 * @since   1.0.5
	 *
	 * @param   string $file_type  File Type.
	 * @return  array               Alignment Options
	 */
	public function get_attachment_display_settings_link_to( $file_type ) {

		switch ( $file_type ) {

			/**
			 * Image.
			 */
			case 'image':
				$options = array(
					'none'   => __( 'None', 'media-library-organizer' ),
					'file'   => __( 'Media File', 'media-library-organizer' ),
					'post'   => __( 'Attachment Page', 'media-library-organizer' ),
					'custom' => __( 'Custom URL', 'media-library-organizer' ),
				);
				break;

			/**
			 * Audio.
			 */
			case 'video':
			case 'audio':
				$options = array(
					'file'  => __( 'Link to Media File', 'media-library-organizer' ),
					'embed' => __( 'Embed Media Player', 'media-library-organizer' ),
					'post'  => __( 'Link to Attachment Page', 'media-library-organizer' ),
				);
				break;

			/**
			 * Document.
			 */
			case 'document':
				$options = array(
					'none' => __( 'None', 'media-library-organizer' ),
					'file' => __( 'Media File', 'media-library-organizer' ),
					'post' => __( 'Attachment Page', 'media-library-organizer' ),
				);
				break;

			/**
			 * Other File Types.
			 */
			default:
				$options = array(
					'none' => __( 'None', 'media-library-organizer' ),
					'file' => __( 'Media File', 'media-library-organizer' ),
					'post' => __( 'Attachment Page', 'media-library-organizer' ),
				);
				break;

		}

		/**
		 * Defines the available Attachment Display Settings: Link To options.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Attachment Display Settings: Link To Options.
		 */
		$options = apply_filters( 'media_library_organizer_common_get_attachment_display_settings_link_to', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Helper method to return the Attachment Display Settings: Size options
	 *
	 * @since   1.0.5
	 *
	 * @return  array   Alignment Options
	 */
	public function get_attachment_display_settings_size() {

		$options = apply_filters(
			'image_size_names_choose',
			array(
				'thumbnail' => __( 'Thumbnail', 'media-library-organizer' ),
				'medium'    => __( 'Medium', 'media-library-organizer' ),
				'large'     => __( 'Large', 'media-library-organizer' ),
				'full'      => __( 'Full Size', 'media-library-organizer' ),
			)
		);

		/**
		 * Defines the available WordPress registered Image Sizes.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $options    Attachment Display Settings: Link To Options.
		 */
		$options = apply_filters( 'media_library_organizer_common_get_attachment_display_settings_size', $options );

		// Return filtered results.
		return $options;

	}

	/**
	 * Returns a flat array ordered by parent > child > child.
	 * When iterated through and output, would produce structure
	 * the same as wp_dropdown_cats().
	 *
	 * @since   1.0.0
	 *
	 * @param   string $taxonomy  Taxonomy.
	 * @return  mixed               false | array of Terms
	 */
	public function get_terms_hierarchical( $taxonomy ) {

		// Build args for fetching Top Level Terms that don't have parents.
		$args = array(
			'taxonomy'   => $taxonomy,
			'hide_empty' => false,
			'parent'     => 0,
		);

		// If logged in as an Administrator, prevent PublishPress Permissions from attempting to filter Term counts,
		// otherwise they will display as zero for Administrators (other User Roles are unaffected).
		if ( is_user_logged_in() && wp_get_current_user()->roles[0] === 'administrator' ) {
			$args['pp_no_filter'] = true;
		}

		// Get Top Level Terms that don't have parents.
		$terms = get_terms( $args );

		// Bail if this fails.
		if ( is_wp_error( $terms ) ) {
			return false;
		}
		if ( empty( $terms ) ) {
			return false;
		}

		// Get hierarchy of Terms.
		// We don't use _get_term_hierarchy(), as this is a private WordPress function that returns child term IDs ordered by ID, not name.
		$hierarchy = $this->get_term_hierarchy( $taxonomy );

		// Build final term array, comprising of top level terms and all children.
		$hierarchical_terms = array();
		foreach ( $terms as $term ) {
			$hierarchical_terms[] = $term;
			$hierarchical_terms   = $this->add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $term->term_id, 1 );
		}

		/**
		 * Defines the available hierarchical terms for the given Taxonomy.
		 *
		 * @since   1.0.7
		 *
		 * @param   array   $hierarchical_terms    Hierarchical Terms.
		 */
		$hierarchical_terms = apply_filters( 'media_library_organizer_common_get_terms_hierarchical', $hierarchical_terms, $taxonomy, $hierarchy );

		// Return filtered results.
		return $hierarchical_terms;

	}

	/**
	 * Modified version of _get_term_hierarchy(), which returns Term IDs ordered by Term Name.
	 *
	 * @since   1.4.5
	 *
	 * @param   string $taxonomy   Taxonomy.
	 * @return  array               Child Terms
	 */
	private function get_term_hierarchy( $taxonomy ) {

		// Bail if the taxonomy is not hierarchical.
		if ( ! is_taxonomy_hierarchical( $taxonomy ) ) {
			return array();
		}

		$children = array();
		$terms    = get_terms(
			array(
				'taxonomy'               => $taxonomy,
				'get'                    => 'all',
				'orderby'                => 'name',
				'fields'                 => 'id=>parent',
				'update_term_meta_cache' => false,
			)
		);
		foreach ( $terms as $term_id => $parent ) {
			if ( $parent > 0 ) {
				$children[ $parent ][] = $term_id;
			}
		}

		return $children;

	}

	/**
	 * Recursive function to keep adding child terms through all depths until they are exhausted
	 *
	 * @since   1.0.0
	 *
	 * @param   string $taxonomy               Taxonomy.
	 * @param   array  $hierarchical_terms     Hierarchical Terms.
	 * @param   array  $hierarchy              Term ID / Child ID Hierarchy.
	 * @param   int    $current_term_id        Current Term ID.
	 * @param   int    $current_depth          Current Depth.
	 * @return          $hierarchical_terms     Hierarchical Terms
	 */
	private function add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $current_term_id, $current_depth ) {

		// Bail if no Children exist for the current term.
		if ( ! isset( $hierarchy[ $current_term_id ] ) ) {
			return $hierarchical_terms;
		}

		// Iterate through Child Term IDs, adding them to the array.
		foreach ( $hierarchy[ $current_term_id ] as $child_term_id ) {
			// Get the Child Term.
			$child_term = get_term( $child_term_id, $taxonomy );

			// Depending on its depth, pad the label.
			$child_term->name = str_pad( '', $current_depth, '-', STR_PAD_LEFT ) . ' ' . $child_term->name;

			// Assign to the flat array of hierarchical terms.
			$hierarchical_terms[] = $child_term;

			// Add Child Terms.
			$hierarchical_terms = $this->add_child_terms_recursive( $taxonomy, $hierarchical_terms, $hierarchy, $child_term_id, ( $current_depth + 1 ) );
		}

		// If here, we've finished.
		return $hierarchical_terms;

	}

	/**
	 * Returns a string to indicate the current Media View the user is on (either list or grid)
	 *
	 * @since   1.0.0
	 *
	 * @return  string  View (list|grid)
	 */
	public function get_media_view() {

		$media_view = ( get_user_option( 'media_library_mode', get_current_user_id() ) ? get_user_option( 'media_library_mode', get_current_user_id() ) : 'grid' );

		/**
		 * Defines the current Media View the user is on (either list or grid).
		 *
		 * @since   1.0.7
		 *
		 * @param   string  $media_view     Media View.
		 */
		$media_view = apply_filters( 'media_library_organizer_common_get_media_view', $media_view );

		// Return filtered result.
		return $media_view;

	}

	/**
	 * Insert an array value after the given key for the given array
	 *
	 * @since   1.1.4
	 *
	 * @param   array  $array      Current Array.
	 * @param   string $key        Key (new array will be inserted after this key).
	 * @param   array  $new        Array data to insert.
	 * @return  array               New Array
	 */
	public function array_insert_after( array $array, $key, array $new ) {

		$keys  = array_keys( $array );
		$index = array_search( $key, $keys, true );
		$pos   = false === $index ? count( $array ) : $index + 1;
		return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );

	}

	/**
	 * Determines if the WordPress URL is a local, non-web accessible URL.
	 *
	 * @since   1.1.0
	 *
	 * @return  bool    Locally Hosted Site
	 */
	public function is_local_host() {

		// Get URL of site and its information.
		$url = wp_parse_url( get_bloginfo( 'url' ) );

		// Iterate through local host addresses to check if they exist
		// in part of the site's URL host.
		foreach ( $this->get_local_hosts() as $local_host ) {
			if ( strpos( $url['host'], $local_host ) !== false ) {
				return true;
			}
		}

		// If here, we're not on a local host.
		return false;

	}

	/**
	 * Returns an array of domains and IP addresses that are non-web accessible
	 *
	 * @since   1.1.0
	 *
	 * @return  array   Non-web accessible Domains and IP addresses
	 */
	private function get_local_hosts() {

		// If domain is 127.0.0.1, localhost or .dev, don't count it towards the domain limit
		// The user has a valid license key if they're here, so that's enough.
		// See: https://www.sqa.org.uk/e-learning/WebTech01CD/page_12.htm.
		$local_hosts = array(
			'localhost',
			'127.0.0.1',
			'10.0.',
			'192.168.',
			'.dev',
			'.local',
			'.localhost',
			'.test',
		);

		// Add 172.16.0.* to 172.16.31.*.
		for ( $i = 0; $i <= 31; $i++ ) {
			$local_hosts[] = '172.16.' . $i . '.';
		}

		return $local_hosts;

	}

}