| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Settings\Sections; |
| 4 |
|
| 5 |
use ISC\Settings; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle settings for miscellaneous options |
| 9 |
*/ |
| 10 |
class Miscellaneous extends Settings\Section { |
| 11 |
|
| 12 |
/** |
| 13 |
* Add settings section |
| 14 |
*/ |
| 15 |
public function add_settings_section() { |
| 16 |
// Misc settings group |
| 17 |
add_settings_section( 'isc_settings_section_misc', __( 'Miscellaneous settings', 'image-source-control-isc' ), '__return_false', 'isc_settings_page' ); |
| 18 |
add_settings_field( 'standard_source', __( 'Standard source', 'image-source-control-isc' ), [ $this, 'render_field_standard_source' ], 'isc_settings_page', 'isc_settings_section_misc' ); |
| 19 |
add_settings_field( 'block_options', __( 'Block options', 'image-source-control-isc' ), [ $this, 'render_field_block_options' ], 'isc_settings_page', 'isc_settings_section_misc' ); |
| 20 |
add_settings_field( 'warning_one_source', __( 'Warn about missing sources', 'image-source-control-isc' ), [ $this, 'render_field_warning_source_missing' ], 'isc_settings_page', 'isc_settings_section_misc' ); |
| 21 |
add_settings_field( 'enable_log', __( 'Debug log', 'image-source-control-isc' ), [ $this, 'render_field_enable_log' ], 'isc_settings_page', 'isc_settings_section_misc' ); |
| 22 |
} |
| 23 |
|
| 24 |
|
| 25 |
/** |
| 26 |
* Render options for standard image sources |
| 27 |
*/ |
| 28 |
public function render_field_standard_source() { |
| 29 |
$standard_source = \ISC\Standard_Source::get_standard_source(); |
| 30 |
$standard_source_text = \ISC\Standard_Source::get_standard_source_text(); |
| 31 |
require_once ISCPATH . '/admin/templates/settings/miscellaneous/standard-source.php'; |
| 32 |
|
| 33 |
do_action( 'isc_admin_settings_template_after_standard_source' ); |
| 34 |
} |
| 35 |
|
| 36 |
/** |
| 37 |
* Render options for block editor support |
| 38 |
*/ |
| 39 |
public function render_field_block_options() { |
| 40 |
$options = $this->get_options(); |
| 41 |
$checked = \ISC_Block_Options::enabled(); |
| 42 |
$disabled = apply_filters( 'isc_force_block_options', false ); |
| 43 |
require_once ISCPATH . '/admin/templates/settings/miscellaneous/block-options.php'; |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Render the option to display a warning in the admin area if an image source is missing. |
| 48 |
*/ |
| 49 |
public function render_field_warning_source_missing() { |
| 50 |
$options = $this->get_options(); |
| 51 |
require_once ISCPATH . '/admin/templates/settings/miscellaneous/warn-source-missing.php'; |
| 52 |
} |
| 53 |
|
| 54 |
/** |
| 55 |
* Render the option to log image source activity in isc.log |
| 56 |
*/ |
| 57 |
public function render_field_enable_log() { |
| 58 |
$options = $this->get_options(); |
| 59 |
$checked = ! empty( $options['enable_log'] ); |
| 60 |
$file_exists = \ISC_Log::log_file_exists(); |
| 61 |
$log_file_url = \ISC_Log::get_log_file_url(); |
| 62 |
require_once ISCPATH . '/admin/templates/settings/miscellaneous/log-enable.php'; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Validate settings |
| 67 |
* |
| 68 |
* @param array $output output data. |
| 69 |
* @param array $input input data. |
| 70 |
* |
| 71 |
* @return array $output |
| 72 |
*/ |
| 73 |
public function validate_settings( array $output, array $input ): array { |
| 74 |
|
| 75 |
$output['warning_onesource_missing'] = ! empty( $input['warning_onesource_missing'] ); |
| 76 |
|
| 77 |
// remove the debug log file when it was disabled |
| 78 |
if ( isset( $output['enable_log'] ) && ! isset( $input['enable_log'] ) ) { |
| 79 |
\ISC_Log::delete_log_file(); |
| 80 |
} |
| 81 |
$output['enable_log'] = ! empty( $input['enable_log'] ); |
| 82 |
$output['block_options'] = ! empty( $input['block_options'] ); |
| 83 |
|
| 84 |
/** |
| 85 |
* 2.0 moved the options to handle "own images" into "standard sources" and only offers a single choice for one of the options now |
| 86 |
* this section maps old to new settings |
| 87 |
*/ |
| 88 |
if ( ! empty( $input['exclude_own_images'] ) ) { |
| 89 |
// don’t show sources for marked images |
| 90 |
$output['standard_source'] = 'exclude'; |
| 91 |
} elseif ( ! empty( $input['use_authorname'] ) ) { |
| 92 |
// show author name |
| 93 |
$output['standard_source'] = 'author_name'; |
| 94 |
} else { |
| 95 |
$output['standard_source'] = isset( $input['standard_source'] ) ? esc_attr( $input['standard_source'] ) : 'custom_text'; |
| 96 |
} |
| 97 |
|
| 98 |
// custom source text |
| 99 |
if ( isset( $input['by_author_text'] ) ) { |
| 100 |
$output['standard_source_text'] = esc_html( $input['by_author_text'] ); |
| 101 |
} else { |
| 102 |
$output['standard_source_text'] = isset( $input['standard_source_text'] ) ? esc_attr( $input['standard_source_text'] ) : \ISC\Standard_Source::get_standard_source_text(); |
| 103 |
} |
| 104 |
|
| 105 |
return $output; |
| 106 |
} |
| 107 |
} |
| 108 |
|