| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC; |
| 4 |
|
| 5 |
/** |
| 6 |
* Render the standard source |
| 7 |
*/ |
| 8 |
class Standard_Source { |
| 9 |
|
| 10 |
/** |
| 11 |
* Get the ISC options |
| 12 |
* |
| 13 |
* @return array |
| 14 |
*/ |
| 15 |
protected static function get_options(): ?array { |
| 16 |
return Plugin::get_options(); |
| 17 |
} |
| 18 |
|
| 19 |
/** |
| 20 |
* Get the standard source text as set up under Settings > Standard Source > Custom text |
| 21 |
* if there was no input, yet |
| 22 |
* |
| 23 |
* @return string |
| 24 |
*/ |
| 25 |
public static function get_standard_source_text(): string { |
| 26 |
$options = self::get_options(); |
| 27 |
|
| 28 |
if ( ! empty( $options['standard_source_text'] ) ) { |
| 29 |
return $options['standard_source_text']; |
| 30 |
} elseif ( isset( $options['by_author_text'] ) ) { |
| 31 |
return $options['by_author_text']; |
| 32 |
} else { |
| 33 |
return sprintf( '© %s', get_home_url() ); |
| 34 |
} |
| 35 |
} |
| 36 |
|
| 37 |
/** |
| 38 |
* Return the standard source string for a given image if standard source is used for it |
| 39 |
* |
| 40 |
* @param int $attachment_id attachment ID. |
| 41 |
* @return string |
| 42 |
*/ |
| 43 |
public static function get_standard_source_text_for_attachment( int $attachment_id ): string { |
| 44 |
|
| 45 |
if ( self::standard_source_is( 'author_name' ) ) { |
| 46 |
$author = get_post_field( 'post_author', $attachment_id ); |
| 47 |
$source = ! empty( $author ) ? get_the_author_meta( 'display_name', $author ) : ''; |
| 48 |
} else { |
| 49 |
$source = self::get_standard_source_text(); |
| 50 |
} |
| 51 |
|
| 52 |
/** |
| 53 |
* Filter the standard source text |
| 54 |
* |
| 55 |
* @param string $source standard source text. |
| 56 |
* @param int $attachment_id attachment ID. |
| 57 |
*/ |
| 58 |
return apply_filters( 'isc_standard_source_text_for_attachment', $source, $attachment_id ); |
| 59 |
} |
| 60 |
|
| 61 |
/** |
| 62 |
* Verify the standard source option |
| 63 |
* |
| 64 |
* @param string $value value of the [standard_source] option. |
| 65 |
* |
| 66 |
* @return bool whether $value is identical to the standard source option or not. |
| 67 |
*/ |
| 68 |
public static function standard_source_is( string $value ): bool { |
| 69 |
$options = self::get_options(); |
| 70 |
|
| 71 |
if ( isset( $options['standard_source'] ) ) { |
| 72 |
return $options['standard_source'] === $value; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* 2.0 moved the options to handle "own images" into "standard sources" and only offers a single choice for one of the options now |
| 77 |
* this section maps old to new settings |
| 78 |
*/ |
| 79 |
if ( ! empty( $options['exclude_own_images'] ) ) { |
| 80 |
return 'exclude' === $value; |
| 81 |
} elseif ( ! empty( $options['use_authorname'] ) ) { |
| 82 |
return 'author_name' === $value; |
| 83 |
} |
| 84 |
|
| 85 |
return false; |
| 86 |
} |
| 87 |
|
| 88 |
/** |
| 89 |
* Get the standard source setting |
| 90 |
* |
| 91 |
* @return string |
| 92 |
*/ |
| 93 |
public static function get_standard_source() { |
| 94 |
$options = self::get_options(); |
| 95 |
|
| 96 |
// options since 2.0 |
| 97 |
if ( ! empty( $options['standard_source'] ) ) { |
| 98 |
return $options['standard_source']; |
| 99 |
} |
| 100 |
|
| 101 |
/** |
| 102 |
* 2.0 moved the options to handle "own images" into "standard sources" and only offers a single choice for one of the options now |
| 103 |
* this section maps old to new settings |
| 104 |
*/ |
| 105 |
if ( ! empty( $options['exclude_own_images'] ) ) { |
| 106 |
return 'exclude'; |
| 107 |
} elseif ( ! empty( $options['use_authorname'] ) ) { |
| 108 |
return 'author_name'; |
| 109 |
} elseif ( ! empty( $options['by_author_text'] ) ) { |
| 110 |
return 'custom_text'; |
| 111 |
} |
| 112 |
|
| 113 |
return ''; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* Get the label of the standard source label |
| 118 |
* |
| 119 |
* @param string $value optional value, if missing, will use the stored value. |
| 120 |
* |
| 121 |
* @return string |
| 122 |
*/ |
| 123 |
public static function get_standard_source_label( string $value = '' ) { |
| 124 |
$labels = [ |
| 125 |
'exclude' => __( 'Exclude from lists', 'image-source-control-isc' ), |
| 126 |
'author_name' => __( 'Author name', 'image-source-control-isc' ), |
| 127 |
'wp_caption' => __( 'Caption', 'image-source-control-isc' ), |
| 128 |
'custom_text' => __( 'Custom text', 'image-source-control-isc' ), |
| 129 |
'iptc' => __( 'IPTC meta data', 'image-source-control-isc' ), |
| 130 |
]; |
| 131 |
|
| 132 |
if ( ! $value ) { |
| 133 |
$value = self::get_standard_source(); |
| 134 |
} |
| 135 |
|
| 136 |
return $labels[ $value ] ?? $value; |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Check if the given attachment ought to use the standard source |
| 141 |
* |
| 142 |
* @param int $attachment_id attachment ID. |
| 143 |
* |
| 144 |
* @return bool true if standard source is used |
| 145 |
*/ |
| 146 |
public static function use_standard_source( int $attachment_id ): bool { |
| 147 |
|
| 148 |
return (bool) apply_filters( |
| 149 |
'isc_use_standard_source_for_attachment', |
| 150 |
get_post_meta( $attachment_id, 'isc_image_source_own', true ), |
| 151 |
$attachment_id |
| 152 |
); |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* Return true if the standard source is hidden for the given attachment |
| 157 |
* this is the case if the standard source is set to "exclude" and the attachment uses the standard source |
| 158 |
* |
| 159 |
* @param int $attachment_id attachment ID. |
| 160 |
* @return bool |
| 161 |
*/ |
| 162 |
public static function hide_standard_source_for_image( int $attachment_id ): bool { |
| 163 |
return self::standard_source_is( 'exclude' ) && self::use_standard_source( $attachment_id ); |
| 164 |
} |
| 165 |
} |