PluginProbe
Image Source Control Lite – Show Image Credits and Captions / trunk
Image Source Control Lite – Show Image Credits and Captions vtrunk
3.12.0 3.11.0 trunk 1.1 1.1.1 1.1.2 1.1.2.1 1.1.3 1.10 1.10.1 1.10.2 1.10.3 1.10.4 1.10.5 1.2 1.2.0.1 1.2.0.2 1.2.0.3 1.3.0 1.3.1 1.3.2 1.3.3 1.3.4 1.3.4.1 1.3.5 All 110 releases
image-source-control-isc / includes / standard-source.php

standard-source.php in Image Source Control Lite – Show Image Credits and Captions trunk, at includes/standard-source.php

165 lines 4.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 }