| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Image_Sources; |
| 4 |
|
| 5 |
use ISC\Plugin; |
| 6 |
use \ISC\Admin_Utils; |
| 7 |
|
| 8 |
/** |
| 9 |
* Add fields for the image sources to the backend |
| 10 |
*/ |
| 11 |
class Admin_Fields { |
| 12 |
use \ISC\Options; |
| 13 |
|
| 14 |
/** |
| 15 |
* Constructor |
| 16 |
*/ |
| 17 |
public function __construct() { |
| 18 |
// register attachment fields |
| 19 |
add_filter( 'attachment_fields_to_edit', [ $this, 'add_isc_fields' ], 10, 2 ); |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Add custom field to attachment |
| 24 |
* |
| 25 |
* @param array $form_fields field fields. |
| 26 |
* @param \WP_Post $post post object. |
| 27 |
* |
| 28 |
* @return array with form fields |
| 29 |
*/ |
| 30 |
public function add_isc_fields( $form_fields, $post ) { |
| 31 |
// Check if we should process this attachment based on settings |
| 32 |
if ( ! \ISC\Media_Type_Checker::should_process_attachment( $post ) ) { |
| 33 |
return $form_fields; |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Return, when the ISC fields are enabled for blocks, and we are not using the block editor. |
| 38 |
* It is tricky to detect and easy to break, so here is more information on it: |
| 39 |
* |
| 40 |
* Media modal on the block editor: uses AJAX and doesn’t "know" it is on a block editor page. But it knows that it comes from "wp-admin/post.php" |
| 41 |
* Media modal in the Grid view of the Media Library: also uses AJAX, but the referrer is "wp-admin/upload.php" |
| 42 |
* The List view of the Media Library does not open the modal, nor uses AJAX, but links to the attachment page; when testing, make sure to test a reload of the attachment page and when it was saved since the Referer then changes |
| 43 |
* Classic Editor: not supported. I wasn’t able to find reliably parameters for it; technically, it looks like the media modal on the block editor; users can disable block support actively on these sites |
| 44 |
*/ |
| 45 |
if ( ! empty( $_SERVER['HTTP_REFERER'] ) |
| 46 |
// phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized,WordPress.Security.ValidatedSanitizedInput.MissingUnslash |
| 47 |
&& strpos( $_SERVER['HTTP_REFERER'], 'wp-admin/post.php' ) !== false |
| 48 |
&& wp_doing_ajax() |
| 49 |
// the filter allows users to force the ISC fields and Block options at the same time |
| 50 |
&& ( \ISC_Block_Options::enabled() && ! apply_filters( 'isc_force_block_options', false ) ) ) { |
| 51 |
|
| 52 |
$form_fields['isc_field_note'] = [ |
| 53 |
'label' => __( 'Image Source', 'image-source-control-isc' ), |
| 54 |
'input' => 'html', |
| 55 |
'html' => __( 'Find the image source fields in the image block options or media library.', 'image-source-control-isc' ), |
| 56 |
]; |
| 57 |
|
| 58 |
return $form_fields; |
| 59 |
} |
| 60 |
|
| 61 |
if ( ! Plugin::is_pro() ) { |
| 62 |
$form_fields['isc_image_source_pro']['label'] = ''; |
| 63 |
$form_fields['isc_image_source_pro']['input'] = 'html'; |
| 64 |
$form_fields['isc_image_source_pro']['html'] = Admin_Utils::get_pro_link( 'attachment-edit' ); |
| 65 |
} |
| 66 |
|
| 67 |
// add input field for source |
| 68 |
$form_fields['isc_image_source']['label'] = __( 'Image Source', 'image-source-control-isc' ); |
| 69 |
$form_fields['isc_image_source']['value'] = Image_Sources::get_image_source_text_raw( $post->ID ); |
| 70 |
$form_fields['isc_image_source']['helps'] = __( 'Include the image source here.', 'image-source-control-isc' ); |
| 71 |
|
| 72 |
// add checkbox to mark as your own image |
| 73 |
$form_fields['isc_image_source_own']['input'] = 'html'; |
| 74 |
$form_fields['isc_image_source_own']['label'] = __( 'Use standard source', 'image-source-control-isc' ); |
| 75 |
$form_fields['isc_image_source_own']['helps'] = |
| 76 |
sprintf( |
| 77 |
// translators: %%1$s is an opening link tag, %2$s is the closing one |
| 78 |
__( 'Show a %1$sstandard source%2$s instead of the one entered above.', 'image-source-control-isc' ), |
| 79 |
'<a href="' . admin_url( 'options-general.php?page=isc-settings#isc_settings_section_misc' ) . '" target="_blank">', |
| 80 |
'</a>' |
| 81 |
) . '<br/>' . |
| 82 |
sprintf( |
| 83 |
// translators: %s is the name of an option |
| 84 |
__( 'Currently selected: %s', 'image-source-control-isc' ), |
| 85 |
\ISC\Standard_Source::get_standard_source_label() |
| 86 |
); |
| 87 |
$form_fields['isc_image_source_own']['html'] = |
| 88 |
"<input type='checkbox' value='1' name='attachments[{$post->ID}][isc_image_source_own]' id='attachments[{$post->ID}][isc_image_source_own]' " |
| 89 |
. checked( get_post_meta( $post->ID, 'isc_image_source_own', true ), 1, false ) |
| 90 |
. ' style="width:14px"/> '; |
| 91 |
|
| 92 |
// add input field for source url |
| 93 |
$form_fields['isc_image_source_url']['label'] = __( 'Image Source URL', 'image-source-control-isc' ); |
| 94 |
$form_fields['isc_image_source_url']['value'] = Image_Sources::get_image_source_url( $post->ID ); |
| 95 |
$form_fields['isc_image_source_url']['helps'] = __( 'URL to link the source text to.', 'image-source-control-isc' ); |
| 96 |
|
| 97 |
$options = $this->get_options(); |
| 98 |
|
| 99 |
if ( ! empty( $options['ai_images']['show_label'] ) ) { |
| 100 |
$form_fields['isc_ai_label']['input'] = 'html'; |
| 101 |
$form_fields['isc_ai_label']['label'] = __( 'AI label', 'image-source-control-isc' ); |
| 102 |
$form_fields['isc_ai_label']['helps'] = __( 'Choose a label for AI-generated images.', 'image-source-control-isc' ); |
| 103 |
$html = '<select name="attachments[' . $post->ID . '][isc_ai_label]" id="attachments[' . $post->ID . '][isc_ai_label]">'; |
| 104 |
$html .= '<option value="">--</option>'; |
| 105 |
foreach ( Ai_Labels::get_labels() as $value => $label ) { |
| 106 |
$html .= '<option value="' . esc_attr( $value ) . '" ' . selected( Image_Sources::get_ai_label( $post->ID ), $value, false ) . '>' . esc_html( $label ) . '</option>'; |
| 107 |
} |
| 108 |
$html .= '</select>'; |
| 109 |
$form_fields['isc_ai_label']['html'] = $html; |
| 110 |
} |
| 111 |
// add input field for license, if enabled |
| 112 |
$licences = Utils::licences_text_to_array( $options['licences'] ); |
| 113 |
if ( $options['enable_licences'] && $licences ) { |
| 114 |
$form_fields['isc_image_licence']['input'] = 'html'; |
| 115 |
$form_fields['isc_image_licence']['label'] = __( 'Image License', 'image-source-control-isc' ); |
| 116 |
$form_fields['isc_image_licence']['helps'] = __( 'Choose the image license.', 'image-source-control-isc' ); |
| 117 |
$html = '<select name="attachments[' . $post->ID . '][isc_image_licence]" id="attachments[' . $post->ID . '][isc_image_licence]">'; |
| 118 |
$html .= '<option value="">--</option>'; |
| 119 |
foreach ( $licences as $_licence_name => $_licence_data ) { |
| 120 |
$html .= '<option value="' . $_licence_name . '" ' . selected( Image_Sources::get_image_license( $post->ID ), $_licence_name, false ) . '>' . $_licence_name . '</option>'; |
| 121 |
} |
| 122 |
$html .= '</select>'; |
| 123 |
$form_fields['isc_image_licence']['html'] = $html; |
| 124 |
} |
| 125 |
|
| 126 |
// list posts the image is used in |
| 127 |
$form_fields['isc_image_usage']['input'] = 'html'; |
| 128 |
$form_fields['isc_image_usage']['label'] = __( 'Appearances', 'image-source-control-isc' ); |
| 129 |
$form_fields['isc_image_usage']['html'] = __( 'Where is this file used?', 'image-source-control-isc' ); |
| 130 |
// add pro link |
| 131 |
$form_fields['isc_image_usage']['html'] .= ' ' . Admin_Utils::get_pro_link( 'media-library-usage' ); |
| 132 |
|
| 133 |
return apply_filters( 'isc_admin_attachment_form_fields', $form_fields, $post, $options ); |
| 134 |
} |
| 135 |
} |