register_settings( aiou()->settings() ); ( new Feature() )->register(); ( new AdminPage() )->register(); } /** * Register the Duplicate Posts settings tab. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_settings( SettingsRegistry $settings ): void { $settings->add_tab( 'duplicates', __( 'Duplicate Posts', 'all-in-one-utilities' ), __( 'Find and remove posts, pages, or any other post type that share an identical title.', 'all-in-one-utilities' ), 45 ); $this->register_matching_section( $settings ); $this->register_exclusions_section( $settings ); $this->register_automation_section( $settings ); $this->register_notifications_section( $settings ); } /** * Which post types to scan, and which copy to keep. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_matching_section( SettingsRegistry $settings ): void { $settings->add_section( 'duplicates', 'matching', __( 'Matching', 'all-in-one-utilities' ), __( 'Posts are only compared against others of the same post type and an identical title.', 'all-in-one-utilities' ), 10 ); $choices = array(); foreach ( get_post_types( array( 'public' => true ), 'objects' ) as $post_type ) { $choices[ $post_type->name ] = $post_type->labels->singular_name; } $settings->add_field( 'duplicates', 'matching', new Field( id: 'post_types', label: __( 'Post types to scan', 'all-in-one-utilities' ), type: 'post_types', default: array( 'post', 'page' ), args: array( 'choices' => $choices ), priority: 10 ) ); $settings->add_field( 'duplicates', 'matching', new Field( id: 'keep', label: __( 'When duplicates are found, keep the', 'all-in-one-utilities' ), type: 'select', default: 'oldest', args: array( 'choices' => array( 'oldest' => __( 'Oldest copy', 'all-in-one-utilities' ), 'newest' => __( 'Newest copy', 'all-in-one-utilities' ), ), ), doc: new Doc( title: __( 'Which copy is kept', 'all-in-one-utilities' ), short: __( 'The other copy (or copies) sharing that title are the ones offered for removal.', 'all-in-one-utilities' ) ), priority: 20 ) ); } /** * Post IDs that should never be removed. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_exclusions_section( SettingsRegistry $settings ): void { $settings->add_section( 'duplicates', 'exclusions', __( 'Exclusions', 'all-in-one-utilities' ), '', 20 ); $settings->add_field( 'duplicates', 'exclusions', new Field( id: 'exclude_ids', label: __( 'Never remove these post IDs', 'all-in-one-utilities' ), type: 'textarea', default: '', args: array( 'rows' => 3, 'description' => __( 'One ID per line, or comma-separated. These posts are never offered for removal, even if another post shares their title.', 'all-in-one-utilities' ), ), priority: 10 ) ); } /** * The optional scheduled scan. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_automation_section( SettingsRegistry $settings ): void { $settings->add_section( 'duplicates', 'automation', __( 'Automation', 'all-in-one-utilities' ), __( 'Optionally run the scan on a schedule, instead of only reviewing and removing duplicates manually.', 'all-in-one-utilities' ), 30 ); $settings->add_field( 'duplicates', 'automation', new Field( id: 'schedule_mode', label: __( 'Scheduled scan', 'all-in-one-utilities' ), type: 'select', default: 'off', args: array( 'choices' => array( 'off' => __( 'Off -- review and remove manually', 'all-in-one-utilities' ), 'report' => __( 'Report only -- log (and optionally email) what would be removed', 'all-in-one-utilities' ), 'delete' => __( 'Automatically move duplicates to Trash', 'all-in-one-utilities' ), ), ), doc: new Doc( title: __( 'Scheduled scan modes', 'all-in-one-utilities' ), short: __( '"Report only" never deletes anything -- it only logs (and optionally emails) what the scan found.', 'all-in-one-utilities' ), warnings: array( __( 'The "automatically move to Trash" mode deletes without a manual review step each time it runs. Trashed posts can still be restored from the Trash until it\'s emptied.', 'all-in-one-utilities' ) ) ), priority: 10 ) ); $settings->add_field( 'duplicates', 'automation', new Field( id: 'schedule_interval', label: __( 'How often', 'all-in-one-utilities' ), type: 'select', default: 'daily', args: array( 'choices' => array( 'hourly' => __( 'Hourly', 'all-in-one-utilities' ), 'twicedaily' => __( 'Twice daily', 'all-in-one-utilities' ), 'daily' => __( 'Daily', 'all-in-one-utilities' ), ), 'depends_on' => 'schedule_mode', ), priority: 20 ) ); } /** * Optional status emails. * * @param SettingsRegistry $settings The settings registry. * @return void */ private function register_notifications_section( SettingsRegistry $settings ): void { $settings->add_section( 'duplicates', 'notifications', __( 'Notifications', 'all-in-one-utilities' ), __( 'Get an email after a scheduled scan finds or removes duplicates.', 'all-in-one-utilities' ), 40 ); $this->migrate_legacy_notification_settings(); aiou()->emails()->add( new DuplicatePostsReportEmail(), 'duplicates', 'notifications', 10 ); } /** * One-time move of the old "Status emails" toggle and "Send to" field * into the report email's own `{ enabled, recipient }` value. * * @return void */ private function migrate_legacy_notification_settings(): void { $option = get_option( 'aiou_settings_duplicates' ); if ( ! is_array( $option ) || ! isset( $option['notifications'] ) || ! is_array( $option['notifications'] ) ) { return; } $values = $option['notifications']; if ( ! array_key_exists( 'email_enabled', $values ) && ! array_key_exists( 'email_recipient', $values ) ) { return; } if ( ! isset( $values[ DuplicatePostsReportEmail::ID ] ) ) { $values[ DuplicatePostsReportEmail::ID ] = array_filter( array( 'enabled' => empty( $values['email_enabled'] ) ? 0 : 1, 'recipient' => (string) ( $values['email_recipient'] ?? '' ), ), static fn( $value ): bool => '' !== $value ); } unset( $values['email_enabled'], $values['email_recipient'] ); $option['notifications'] = $values; update_option( 'aiou_settings_duplicates', $option ); } }