| 1 |
<?php |
| 2 |
|
| 3 |
namespace ISC\Settings\Sections; |
| 4 |
|
| 5 |
use ISC\Settings; |
| 6 |
|
| 7 |
/** |
| 8 |
* Handle settings for general plugin options |
| 9 |
*/ |
| 10 |
class Plugin_Options 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_plugin', __( 'Plugin options', 'image-source-control-isc' ), '__return_false', 'isc_settings_page' ); |
| 18 |
add_settings_field( 'modules', __( 'Modules', 'image-source-control-isc' ), [ $this, 'render_field_modules' ], 'isc_settings_page', 'isc_settings_section_plugin' ); |
| 19 |
add_settings_field( 'images_only', __( 'Images only', 'image-source-control-isc' ), [ $this, 'render_field_images_only' ], 'isc_settings_page', 'isc_settings_section_plugin' ); |
| 20 |
add_settings_field( 'remove_on_uninstall', __( 'Delete data on uninstall', 'image-source-control-isc' ), [ $this, 'render_field_remove_on_uninstall' ], 'isc_settings_page', 'isc_settings_section_plugin' ); |
| 21 |
} |
| 22 |
|
| 23 |
|
| 24 |
/** |
| 25 |
* Render options for plugin modules |
| 26 |
*/ |
| 27 |
public function render_field_modules() { |
| 28 |
$modules_options = $this->get_modules_options(); |
| 29 |
// return keys of $modules_options |
| 30 |
$all_options = array_keys( $modules_options ); |
| 31 |
$options = $this->get_options(); |
| 32 |
// all modules are enabled, if none is selected to prevent accidental disabling; especially, after this option was introduced |
| 33 |
$modules_options_selected = isset( $options['modules'] ) && is_array( $options['modules'] ) ? $options['modules'] : $all_options; |
| 34 |
require_once ISCPATH . '/admin/templates/settings/plugin/modules.php'; |
| 35 |
|
| 36 |
do_action( 'isc_admin_settings_template_after_plugin_options' ); |
| 37 |
} |
| 38 |
|
| 39 |
/** |
| 40 |
* Render the option to restrict functionality to images only. |
| 41 |
*/ |
| 42 |
public function render_field_images_only() { |
| 43 |
$options = $this->get_options(); |
| 44 |
$checked = ! empty( $options['images_only'] ); |
| 45 |
require_once ISCPATH . '/admin/templates/settings/plugin/images-only.php'; |
| 46 |
} |
| 47 |
|
| 48 |
/** |
| 49 |
* Render the option to remove all options and meta data when the plugin is deleted. |
| 50 |
*/ |
| 51 |
public function render_field_remove_on_uninstall() { |
| 52 |
$options = $this->get_options(); |
| 53 |
$checked = ! empty( $options['remove_on_uninstall'] ); |
| 54 |
require_once ISCPATH . '/admin/templates/settings/plugin/remove-on-uninstall.php'; |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Validate settings |
| 59 |
* |
| 60 |
* @param array $output output data. |
| 61 |
* @param array $input input data. |
| 62 |
* |
| 63 |
* @return array $output |
| 64 |
*/ |
| 65 |
public function validate_settings( array $output, array $input ): array { |
| 66 |
$output['modules'] = ( isset( $input['modules'] ) && is_array( $input['modules'] ) ) ? $input['modules'] : []; |
| 67 |
$output['images_only'] = ! empty( $input['images_only'] ); |
| 68 |
$output['remove_on_uninstall'] = ! empty( $input['remove_on_uninstall'] ); |
| 69 |
|
| 70 |
// Cleanup meta data for non-images |
| 71 |
if ( |
| 72 |
! empty( $input['images_only'] ) && |
| 73 |
! empty( $input['images_only_cleanup'] ) |
| 74 |
) { |
| 75 |
\ISC_Model::remove_plugin_meta_from_non_images(); |
| 76 |
} |
| 77 |
|
| 78 |
return $output; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Get the options for modules |
| 83 |
*/ |
| 84 |
public function get_modules_options() { |
| 85 |
$modules_options = [ |
| 86 |
'image_sources' => [ |
| 87 |
'label' => __( 'Image Sources', 'image-source-control-isc' ), |
| 88 |
'description' => __( 'Manage and display author attributions for images.', 'image-source-control-isc' ), |
| 89 |
'value' => 'image_sources', |
| 90 |
'coming_soon' => false, |
| 91 |
], |
| 92 |
'unused_images' => [ |
| 93 |
'label' => __( 'Unused Images', 'image-source-control-isc' ), |
| 94 |
'description' => __( 'Identify unused images to clean up your site.', 'image-source-control-isc' ), |
| 95 |
'value' => 'body_img', |
| 96 |
'is_pro' => true, |
| 97 |
], |
| 98 |
'media_trash' => [ |
| 99 |
'label' => __( 'Media Trash', 'image-source-control-isc' ), |
| 100 |
'description' => __( 'Safely move media files to a custom trash folder for easy recovery. Ideal for testing effects of deleting unused images.', 'image-source-control-isc' ), |
| 101 |
'value' => 'trash', |
| 102 |
'is_pro' => true, |
| 103 |
], |
| 104 |
]; |
| 105 |
|
| 106 |
return apply_filters( 'isc_plugin_options_modules', $modules_options ); |
| 107 |
} |
| 108 |
} |
| 109 |
|