init();
add_action( 'init', array( $this, 'check_version' ) );
add_action( 'admin_bar_menu', array( $this, 'add_admin_bar_menu' ), 100 );
add_action( 'breakdance_builder_footer', array( $this, 'render_breakdance_builder' ) );
if ( is_admin() ) {
add_action( 'admin_menu', array( $this, 'add_admin_menu' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_admin_scripts' ) );
add_action( 'admin_footer', array( $this, 'render_modal' ) );
add_action( 'admin_notices', array( $this, 'render_assignment_notices' ) );
add_action( 'add_meta_boxes', array( $this, 'add_post_meta_boxes' ) );
add_action( 'wp_dashboard_setup', array( $this, 'add_dashboard_widgets' ) );
add_action( 'admin_post_devnotes_export_csv', array( $this, 'handle_export_csv' ) );
add_action( 'admin_post_devnotes_import_csv', array( $this, 'handle_import_csv' ) );
add_action( 'admin_init', array( $this, 'register_settings' ) );
add_action( 'admin_post_devnotes_clear_data', array( $this, 'handle_clear_data' ) );
return;
}
add_action( 'wp_enqueue_scripts', array( $this, 'enqueue_frontend_scripts' ) );
add_action( 'wp_footer', array( $this, 'render_modal' ) );
}
private function get_script_data( $page, $is_app, $is_post_edit, $post_id = null ) {
return array(
'root' => esc_url_raw( rest_url( 'devnotes/v1' ) ),
'nonce' => wp_create_nonce( 'wp_rest' ),
'page' => $page,
'isApp' => $is_app ? 1 : 0,
'isPostEdit' => $is_post_edit ? 1 : 0,
'adminUsers' => $this->get_admin_users_list(),
'currentUserId' => get_current_user_id(),
'today' => current_time( 'Y-m-d' ),
'postId' => null === $post_id ? $this->get_current_admin_post_id() : absint( $post_id ),
'homeUrl' => home_url( '/' ),
'appUrl' => admin_url( 'admin.php?page=devnotes' ),
'initialTab' => $this->get_requested_tab(),
'strings' => array(
'modalEditTask' => __( 'Edit Task', 'devnotes' ),
'modalAddTask' => __( 'Add Task', 'devnotes' ),
'modalEditNote' => __( 'Edit Note', 'devnotes' ),
'modalAddNote' => __( 'Add Note', 'devnotes' ),
'unassigned' => __( '-- Unassigned --', 'devnotes' ),
'fetchError' => __( 'Error fetching items', 'devnotes' ),
'upcomingTasks' => __( 'Upcoming Tasks', 'devnotes' ),
'recentNotes' => __( 'Recent Notes', 'devnotes' ),
'noUpcomingTasks' => __( 'No upcoming tasks.', 'devnotes' ),
'noRecentNotes' => __( 'No recent notes.', 'devnotes' ),
'openDevNotes' => __( 'Open DevNotes', 'devnotes' ),
/* translators: %s: Due date of the overdue task */
'overdueLabel' => __( 'Overdue: %s', 'devnotes' ),
'dueToday' => __( 'Due Today', 'devnotes' ),
'noMatchingNotes' => __( 'No matching notes found.', 'devnotes' ),
'noMatchingTasks' => __( 'No matching tasks found.', 'devnotes' ),
'noMatchingArchiveItems' => __( 'No matching items in archive.', 'devnotes' ),
'tasksHeading' => __( 'Tasks', 'devnotes' ),
'notesHeading' => __( 'Notes', 'devnotes' ),
'noItemsFound' => __( 'No items found.', 'devnotes' ),
/* translators: %s: Date the task was finished */
'finishedLabel' => __( 'Finished: %s', 'devnotes' ),
/* translators: %s: Due date of the task */
'dueLabel' => __( 'Due: %s', 'devnotes' ),
'restore' => __( 'Restore', 'devnotes' ),
'delete' => __( 'Delete', 'devnotes' ),
'finish' => __( 'Finish', 'devnotes' ),
'reopen' => __( 'Reopen', 'devnotes' ),
'edit' => __( 'Edit', 'devnotes' ),
'archive' => __( 'Archive', 'devnotes' ),
'deleteConfirm' => __( 'Are you sure you want to permanently delete this item?', 'devnotes' ),
'saving' => __( 'Saving...', 'devnotes' ),
'save' => __( 'Save', 'devnotes' ),
'saveSuccess' => __( 'Saved successfully to DevNotes!', 'devnotes' ),
'saveError' => __( 'Error saving item', 'devnotes' ),
'updateError' => __( 'Error updating item', 'devnotes' ),
'deleteError' => __( 'Error deleting item', 'devnotes' ),
'clearDataConfirm' => __( 'Are you absolutely sure you want to delete ALL notes and tasks? This cannot be undone.', 'devnotes' ),
'pin' => __( 'Pin', 'devnotes' ),
'unpin' => __( 'Unpin', 'devnotes' ),
'pinned' => __( 'Pinned', 'devnotes' ),
/* translators: %s: title of the linked post or page */
'linkedTo' => __( 'Linked to %s', 'devnotes' ),
'openContext' => __( 'Open screen', 'devnotes' ),
'noLinkedNotes' => __( 'No notes linked to this item yet.', 'devnotes' ),
'noLinkedTasks' => __( 'No tasks linked to this item yet.', 'devnotes' ),
'linkAfterSave' => __( 'Save this item to link notes and tasks to it.', 'devnotes' ),
'itemTypes' => array(
'note' => __( 'Note', 'devnotes' ),
'task' => __( 'Task', 'devnotes' ),
),
),
);
}
private function is_valid_date_value( $value, $formats = array( 'Y-m-d', 'Y-m-d H:i:s', 'Y-m-d H:i' ) ) {
foreach ( $formats as $format ) {
$parsed = DateTime::createFromFormat( $format, $value );
$errors = DateTime::getLastErrors();
if (
$parsed instanceof DateTime
&& $parsed->format( $format ) === $value
&& ( false === $errors || ( 0 === $errors['warning_count'] && 0 === $errors['error_count'] ) )
) {
return true;
}
}
return false;
}
/**
* Post being edited, when the current admin screen has one.
*
* @return int
*/
private function get_current_admin_post_id() {
if ( ! is_admin() ) {
return 0;
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen context, not a form submission
$post_id = isset( $_GET['post'] ) ? absint( wp_unslash( $_GET['post'] ) ) : 0;
if ( $post_id > 0 && get_post( $post_id ) ) {
return $post_id;
}
return 0;
}
/**
* Tab requested on the DevNotes screen.
*
* @return string
*/
private function get_requested_tab() {
if ( ! is_admin() ) {
return '';
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation param used to choose a tab
$page = isset( $_GET['page'] ) ? sanitize_key( wp_unslash( $_GET['page'] ) ) : '';
if ( 'devnotes' !== $page ) {
return '';
}
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only navigation param used to choose a tab
$tab = isset( $_GET['tab'] ) ? sanitize_key( wp_unslash( $_GET['tab'] ) ) : '';
$allowed = array( 'dashboard', 'notes', 'tasks', 'archive' );
return in_array( $tab, $allowed, true ) ? $tab : '';
}
/**
* Active tasks whose due date is today or earlier, in the site timezone.
*
* @return int
*/
private function count_due_tasks() {
global $wpdb;
$table = DevNotes_DB::get_table_name();
$today = current_time( 'Y-m-d' ) . ' 23:59:59';
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- small count for the admin bar badge
$count = $wpdb->get_var(
$wpdb->prepare(
'SELECT COUNT(*) FROM %i WHERE item_type = %s AND status = %s AND due_date IS NOT NULL AND due_date <= %s',
$table,
'task',
'active',
$today
)
);
return null === $count ? 0 : (int) $count;
}
public function render_assignment_notices() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$user_id = get_current_user_id();
$blog_id = get_current_blog_id();
$key = DevNotes_DB::notices_meta_key( $blog_id );
$notices = get_user_meta( $user_id, $key, true );
if ( ! is_array( $notices ) ) {
$notices = array();
}
$legacy = DevNotes_DB::take_legacy_notices( $user_id, $blog_id );
if ( ! empty( $legacy ) ) {
$notices = array_merge( $notices, $legacy );
}
if ( empty( $notices ) ) {
return;
}
delete_user_meta( $user_id, $key );
$titles = array();
foreach ( $notices as $notice ) {
if ( is_array( $notice ) && ! empty( $notice['title'] ) ) {
$titles[] = '“' . sanitize_text_field( $notice['title'] ) . '”';
}
}
if ( empty( $titles ) ) {
return;
}
echo '
';
}
public function check_version() {
if ( ! is_admin() && ! current_user_can( 'manage_options' ) ) {
return;
}
if ( get_option( 'devnotes_version' ) !== DEVNOTES_VERSION ) {
DevNotes_DB::create_table();
update_option( 'devnotes_version', DEVNOTES_VERSION );
}
}
public function add_admin_menu() {
add_menu_page(
__( 'DevNotes', 'devnotes' ),
__( 'DevNotes', 'devnotes' ),
'manage_options',
'devnotes',
array( $this, 'render_admin_page' ),
'dashicons-text-page',
30
);
add_submenu_page(
'devnotes',
__( 'Settings', 'devnotes' ),
__( 'Settings', 'devnotes' ),
'manage_options',
'devnotes-settings',
array( $this, 'render_settings_page' )
);
}
private function get_admin_users_list() {
$admins = get_users( array( 'capability' => 'manage_options' ) );
$admin_list = array();
foreach ( $admins as $admin ) {
$admin_list[] = array(
'id' => $admin->ID,
'name' => $admin->display_name,
);
}
return $admin_list;
}
public function enqueue_admin_scripts( $hook_suffix ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$is_devnotes_page = ( 'toplevel_page_devnotes' === $hook_suffix || 'devnotes_page_devnotes-settings' === $hook_suffix || 'index.php' === $hook_suffix );
$is_post_edit = ( 'post.php' === $hook_suffix || 'post-new.php' === $hook_suffix );
wp_enqueue_style( 'devnotes-admin-css', DEVNOTES_URL . 'assets/css/admin.css', array(), DEVNOTES_VERSION );
wp_enqueue_script(
'devnotes-admin-js',
DEVNOTES_URL . 'assets/js/admin.js',
array( 'jquery', 'wp-api-fetch' ),
DEVNOTES_VERSION,
true
);
wp_localize_script(
'devnotes-admin-js',
'devnotesApp',
$this->get_script_data( $hook_suffix, $is_devnotes_page, $is_post_edit )
);
}
public function enqueue_frontend_scripts() {
if ( ! current_user_can( 'manage_options' ) || ! is_admin_bar_showing() || $this->is_breakdance_builder_request() ) {
return;
}
wp_enqueue_style( 'devnotes-admin-css', DEVNOTES_URL . 'assets/css/admin.css', array(), DEVNOTES_VERSION );
wp_enqueue_script(
'devnotes-admin-js',
DEVNOTES_URL . 'assets/js/admin.js',
array( 'jquery', 'wp-api-fetch' ),
DEVNOTES_VERSION,
true
);
wp_localize_script(
'devnotes-admin-js',
'devnotesApp',
$this->get_script_data( 'frontend', false, false )
);
}
public function render_modal() {
if ( ! current_user_can( 'manage_options' ) || $this->is_breakdance_builder_request() ) {
return;
}
include DEVNOTES_DIR . 'templates/modal.php';
}
/**
* Notes sidebar inside the Breakdance builder.
*
* The builder template does not call wp_head() or wp_footer(). Breakdance 2.8+
* prints this hook in the builder page, so assets are sent from here.
*/
public function render_breakdance_builder() {
if ( ! $this->is_breakdance_builder_request() || ! current_user_can( 'manage_options' ) ) {
return;
}
$post_id = $this->get_breakdance_post_id();
include DEVNOTES_DIR . 'templates/builder-panel.php';
include DEVNOTES_DIR . 'templates/modal.php';
wp_register_style( 'devnotes-admin-css', DEVNOTES_URL . 'assets/css/admin.css', array(), DEVNOTES_VERSION );
wp_register_style( 'devnotes-builder-css', DEVNOTES_URL . 'assets/css/builder.css', array( 'devnotes-admin-css' ), DEVNOTES_VERSION );
wp_enqueue_style( 'devnotes-builder-css' );
wp_register_script(
'devnotes-admin-js',
DEVNOTES_URL . 'assets/js/admin.js',
array( 'jquery', 'wp-api-fetch' ),
DEVNOTES_VERSION,
true
);
wp_localize_script(
'devnotes-admin-js',
'devnotesApp',
$this->get_script_data( 'breakdance-builder', false, true, $post_id )
);
wp_register_script(
'devnotes-builder-js',
DEVNOTES_URL . 'assets/js/builder.js',
array( 'devnotes-admin-js' ),
DEVNOTES_VERSION,
true
);
wp_enqueue_script( 'devnotes-builder-js' );
global $wp_styles, $wp_scripts;
if ( $wp_styles instanceof WP_Styles ) {
$wp_styles->do_items( array( 'devnotes-builder-css' ) );
}
if ( $wp_scripts instanceof WP_Scripts ) {
$wp_scripts->do_items( array( 'devnotes-builder-js' ) );
}
}
/**
* Whether this request is the Breakdance builder shell.
*
* @return bool
*/
private function is_breakdance_builder_request() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen context, not a form submission
$builder = isset( $_GET['breakdance'] ) ? sanitize_key( wp_unslash( $_GET['breakdance'] ) ) : '';
return 'builder' === $builder;
}
/**
* Document open in the Breakdance builder.
*
* @return int
*/
private function get_breakdance_post_id() {
// phpcs:ignore WordPress.Security.NonceVerification.Recommended -- read-only screen context, not a form submission
$post_id = isset( $_GET['id'] ) ? absint( wp_unslash( $_GET['id'] ) ) : 0;
if ( $post_id > 0 && get_post( $post_id ) ) {
return $post_id;
}
return 0;
}
public function add_post_meta_boxes( $post_type ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$skip = array( 'attachment', 'wp_block', 'wp_template', 'wp_template_part', 'wp_navigation', 'wp_font_family', 'wp_font_face' );
if ( in_array( $post_type, $skip, true ) ) {
return;
}
$post_type_object = get_post_type_object( $post_type );
if ( ! $post_type_object || empty( $post_type_object->show_ui ) ) {
return;
}
add_meta_box(
'devnotes_meta_box_id',
__( 'DevNotes', 'devnotes' ),
array( $this, 'render_meta_box' ),
$post_type,
'side',
'high'
);
}
public function render_meta_box( $post ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include DEVNOTES_DIR . 'templates/meta-box.php';
}
public function add_admin_bar_menu( $admin_bar ) {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
$due_count = $this->count_due_tasks();
$label = '' . esc_html__( 'DevNotes', 'devnotes' );
if ( $due_count > 0 ) {
$label .= ' ' . esc_html( number_format_i18n( $due_count ) ) . '';
}
$label .= '';
$href = admin_url( 'admin.php?page=devnotes' );
$meta = array();
if ( $due_count > 0 ) {
$href = admin_url( 'admin.php?page=devnotes&tab=tasks' );
$meta['title'] = sprintf(
/* translators: %d: number of tasks due today or overdue */
_n( '%d task is due today or overdue', '%d tasks are due today or overdue', $due_count, 'devnotes' ),
$due_count
);
}
// Top level menu.
$admin_bar->add_node(
array(
'id' => 'devnotes',
'title' => $label,
'href' => esc_url( $href ),
'meta' => $meta,
)
);
// Submenu: New Note.
$admin_bar->add_node(
array(
'parent' => 'devnotes',
'id' => 'devnotes-new-note',
'title' => __( 'New Note', 'devnotes' ),
'href' => '#',
'meta' => array( 'class' => 'devnotes-admin-bar-btn' ),
)
);
// Submenu: New Task.
$admin_bar->add_node(
array(
'parent' => 'devnotes',
'id' => 'devnotes-new-task',
'title' => __( 'New Task', 'devnotes' ),
'href' => '#',
'meta' => array( 'class' => 'devnotes-admin-bar-btn' ),
)
);
}
public function render_admin_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include DEVNOTES_DIR . 'templates/admin-page.php';
}
public function add_dashboard_widgets() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
wp_add_dashboard_widget(
'devnotes_dashboard_widget',
__( 'DevNotes', 'devnotes' ),
array( $this, 'render_dashboard_widget' )
);
}
public function render_dashboard_widget() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
echo ''; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- hardcoded safe HTML, no dynamic content
}
public function render_settings_page() {
if ( ! current_user_can( 'manage_options' ) ) {
return;
}
include DEVNOTES_DIR . 'templates/settings-page.php';
}
public function handle_export_csv() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'devnotes' ) );
}
check_admin_referer( 'devnotes_export_csv', 'devnotes_export_nonce' );
header( 'Content-Type: text/csv' );
header( 'Content-Disposition: attachment; filename="devnotes_export_' . gmdate( 'Y-m-d' ) . '.csv"' );
$out = fopen( 'php://output', 'w' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- php://output stream; WP_Filesystem does not support this
if ( ! is_resource( $out ) ) {
wp_die( esc_html__( 'Failed to open output stream for CSV export.', 'devnotes' ) );
}
fputcsv( $out, array( 'id', 'title', 'content', 'item_type', 'status', 'due_date', 'finished_date', 'created_at', 'post_id', 'context_url', 'pinned' ) );
global $wpdb;
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- export handler, caching not applicable
$items = $wpdb->get_results(
$wpdb->prepare(
'SELECT id, title, content, item_type, status, due_date, finished_date, created_at, post_id, context_url, pinned FROM %i ORDER BY created_at DESC',
DevNotes_DB::get_table_name()
),
ARRAY_A
);
$items = is_array( $items ) ? $items : array();
foreach ( $items as $item ) {
$row = array();
foreach ( $item as $value ) {
if ( null === $value ) {
$value = '';
}
$row[] = $this->neutralize_csv_formula( $value );
}
fputcsv( $out, $row );
}
fclose( $out ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- php://output stream; WP_Filesystem does not support this
exit;
}
public function handle_import_csv() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'devnotes' ) );
}
check_admin_referer( 'devnotes_import_csv', 'devnotes_import_nonce' );
if ( empty( $_FILES['devnotes_csv']['tmp_name'] ) ) {
wp_die( esc_html__( 'No file uploaded.', 'devnotes' ) );
}
$file = sanitize_text_field( wp_unslash( $_FILES['devnotes_csv']['tmp_name'] ) );
if ( ! is_uploaded_file( $file ) ) {
wp_die( esc_html__( 'Invalid file upload.', 'devnotes' ) );
}
$file_name = isset( $_FILES['devnotes_csv']['name'] ) ? sanitize_file_name( wp_unslash( $_FILES['devnotes_csv']['name'] ) ) : '';
$file_type = wp_check_filetype( $file_name, array( 'csv' => 'text/csv' ) );
if ( empty( $file_type['ext'] ) ) {
wp_die( esc_html__( 'Invalid file type. Only CSV files are allowed.', 'devnotes' ) );
}
$imported_count = 0;
$failed_count = 0;
$handle = fopen( $file, 'r' ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fopen -- reading an uploaded temp file; WP_Filesystem does not support this
if ( false === $handle ) {
$this->redirect_import_result( 'import_failed' );
}
$headers = $this->normalize_csv_headers( fgetcsv( $handle ) );
if ( ! $this->csv_headers_are_usable( $headers ) ) {
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- uploaded temp file handle; WP_Filesystem does not support this
$this->redirect_import_result( 'import_failed' );
}
global $wpdb;
while ( ( $data = fgetcsv( $handle ) ) !== false ) { // phpcs:ignore WordPress.CodeAnalysis.AssignmentInCondition.FoundInWhileCondition -- standard CSV reading pattern
// Skip empty rows or rows that don't match the header column count.
if ( ! is_array( $data ) || count( $headers ) !== count( $data ) ) {
$failed_count++;
continue;
}
$row = array_combine( $headers, $data );
if ( ! is_array( $row ) ) {
$failed_count++;
continue;
}
$title = sanitize_text_field( $this->restore_csv_formula( isset( $row['title'] ) ? $row['title'] : '' ) );
if ( empty( $title ) ) {
$failed_count++;
continue;
}
// Assignments are not imported because user IDs differ between sites.
$post_id = null;
if ( isset( $row['post_id'] ) && '' !== $row['post_id'] ) {
$sanitized_post_id = DevNotes_DB::sanitize_post_id( $row['post_id'] );
if ( ! is_wp_error( $sanitized_post_id ) ) {
$post_id = $sanitized_post_id;
}
}
$content = isset( $row['content'] ) ? sanitize_textarea_field( $this->restore_csv_formula( $row['content'] ) ) : '';
$insert_data = array(
'title' => $title,
'content' => $content,
'item_type' => ( isset( $row['item_type'] ) && in_array( $row['item_type'], array( 'note', 'task' ), true ) ) ? $row['item_type'] : 'note',
'status' => ( isset( $row['status'] ) && in_array( $row['status'], array( 'active', 'archived', 'finished' ), true ) ) ? $row['status'] : 'active',
'due_date' => ( isset( $row['due_date'] ) && ! empty( $row['due_date'] ) && $this->is_valid_date_value( $row['due_date'] ) ) ? sanitize_text_field( $row['due_date'] ) : null,
'finished_date' => ( isset( $row['finished_date'] ) && ! empty( $row['finished_date'] ) && $this->is_valid_date_value( $row['finished_date'] ) ) ? sanitize_text_field( $row['finished_date'] ) : null,
'created_at' => ( isset( $row['created_at'] ) && ! empty( $row['created_at'] ) && $this->is_valid_date_value( $row['created_at'] ) ) ? sanitize_text_field( $row['created_at'] ) : current_time( 'mysql' ),
'assigned_to' => get_current_user_id(),
'post_id' => $post_id,
'context_url' => isset( $row['context_url'] ) ? DevNotes_DB::sanitize_context_url( $row['context_url'] ) : null,
'pinned' => isset( $row['pinned'] ) ? DevNotes_DB::sanitize_pinned( $row['pinned'] ) : 0,
);
$insert_result = $wpdb->insert( DevNotes_DB::get_table_name(), $insert_data ); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery -- write operation during CSV import, caching not applicable
if ( $insert_result ) {
$imported_count++;
} else {
$failed_count++;
}
}
fclose( $handle ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_system_operations_fclose -- uploaded temp file handle; WP_Filesystem does not support this
if ( $failed_count > 0 ) {
$this->redirect_import_result( 'partial' );
}
$this->redirect_import_result( 0 === $imported_count ? 'empty' : '1' );
}
public function handle_clear_data() {
if ( ! current_user_can( 'manage_options' ) ) {
wp_die( esc_html__( 'Unauthorized', 'devnotes' ) );
}
check_admin_referer( 'devnotes_clear_data', 'devnotes_clear_nonce' );
global $wpdb;
$table = DevNotes_DB::get_table_name();
// phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- intentional full data clear
$deleted = $wpdb->query( $wpdb->prepare( 'DELETE FROM %i', $table ) );
if ( false === $deleted ) {
wp_die( esc_html__( 'Could not delete DevNotes data.', 'devnotes' ) );
}
if ( ! DevNotes_DB::delete_notices_for_blog( get_current_blog_id() ) ) {
wp_die( esc_html__( 'DevNotes items were deleted, but assignment notices could not be cleared.', 'devnotes' ) );
}
wp_safe_redirect( admin_url( 'admin.php?page=devnotes-settings&success=cleared' ) );
exit;
}
public function register_settings() {
register_setting(
'devnotes_settings',
'devnotes_email_assignees',
array(
'type' => 'string',
'sanitize_callback' => array( $this, 'sanitize_email_assignees' ),
'default' => '1',
'show_in_rest' => false,
)
);
}
/**
* Store the assignee email option as 1 or 0.
*
* An unchecked checkbox is missing from the request, so an empty value means off.
*
* @param mixed $value Submitted value.
* @return string
*/
public function sanitize_email_assignees( $value ) {
return '1' === $value ? '1' : '0';
}
/**
* @param string $result success query value.
*/
private function redirect_import_result( $result ) {
$allowed = array( '1', 'partial', 'empty', 'import_failed' );
if ( ! in_array( $result, $allowed, true ) ) {
$result = 'import_failed';
}
wp_safe_redirect( admin_url( 'admin.php?page=devnotes-settings&success=' . $result ) );
exit;
}
/**
* @param mixed $headers Header row from fgetcsv().
* @return array
*/
private function normalize_csv_headers( $headers ) {
if ( ! is_array( $headers ) ) {
return array();
}
$normalized = array();
foreach ( $headers as $index => $header ) {
$header = is_string( $header ) ? trim( $header ) : '';
if ( 0 === $index && '' !== $header ) {
$header = preg_replace( '/^\xEF\xBB\xBF/', '', $header );
$header = is_string( $header ) ? trim( $header ) : '';
}
$normalized[] = $header;
}
return $normalized;
}
/**
* @param array $headers Header cells.
* @return bool
*/
private function csv_headers_are_usable( $headers ) {
if ( empty( $headers ) ) {
return false;
}
foreach ( $headers as $header ) {
if ( ! is_string( $header ) || '' === $header ) {
return false;
}
}
return count( $headers ) === count( array_unique( $headers ) );
}
/**
* Stop spreadsheet apps from treating a cell as a formula.
*
* A leading tab keeps the original text and survives a later import.
*
* @param mixed $value Cell value.
* @return string
*/
private function neutralize_csv_formula( $value ) {
$value = (string) $value;
if ( '' === $value ) {
return $value;
}
$first = $value[0];
if ( '=' === $first || '+' === $first || '-' === $first || '@' === $first || "\t" === $first || "\r" === $first ) {
return "\t" . $value;
}
return $value;
}
/**
* Undo the formula prefix added on export.
*
* @param mixed $value Cell value.
* @return string
*/
private function restore_csv_formula( $value ) {
if ( ! is_string( $value ) || strlen( $value ) < 2 || "\t" !== $value[0] ) {
return is_string( $value ) ? $value : '';
}
$second = $value[1];
if ( '=' === $second || '+' === $second || '-' === $second || '@' === $second || "\t" === $second || "\r" === $second ) {
return substr( $value, 1 );
}
return $value;
}
}