prefix . 'devnotes_items'; } public static function create_table() { global $wpdb; $table_name = self::get_table_name(); $charset_collate = $wpdb->get_charset_collate(); // phpcs:disable WordPress.DB.PreparedSQL.InterpolatedNotPrepared -- dbDelta parses this statement; the name is the site prefix plus a fixed suffix. $sql = "CREATE TABLE {$table_name} ( id bigint(20) NOT NULL AUTO_INCREMENT, item_type varchar(20) NOT NULL, title text NOT NULL, content longtext NOT NULL, status varchar(20) DEFAULT 'active' NOT NULL, due_date datetime DEFAULT NULL, finished_date datetime DEFAULT NULL, assigned_to bigint(20) DEFAULT NULL, post_id bigint(20) DEFAULT NULL, context_url varchar(2048) DEFAULT NULL, pinned tinyint(1) DEFAULT 0 NOT NULL, created_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, updated_at datetime DEFAULT CURRENT_TIMESTAMP NOT NULL, PRIMARY KEY (id), KEY item_type (item_type), KEY status (status), KEY assigned_to (assigned_to), KEY post_id (post_id), KEY due_date (due_date), KEY pinned (pinned) ) {$charset_collate};"; // phpcs:enable WordPress.DB.PreparedSQL.InterpolatedNotPrepared require_once ABSPATH . 'wp-admin/includes/upgrade.php'; dbDelta( $sql ); } /** * Keep a same-site http(s) URL, or nothing. * * @param mixed $value Raw URL. * @return string|null */ public static function sanitize_context_url( $value ) { if ( ! is_string( $value ) ) { return null; } $value = trim( $value ); if ( '' === $value ) { return null; } $url = esc_url_raw( $value ); if ( '' === $url ) { return null; } $scheme = strtolower( (string) wp_parse_url( $url, PHP_URL_SCHEME ) ); $url_host = strtolower( (string) wp_parse_url( $url, PHP_URL_HOST ) ); $home = strtolower( (string) wp_parse_url( home_url(), PHP_URL_HOST ) ); if ( ! in_array( $scheme, array( 'http', 'https' ), true ) || '' === $url_host || $url_host !== $home ) { return null; } if ( strlen( $url ) > 2048 ) { $url = substr( $url, 0, 2048 ); } return $url; } /** * Accept a real post ID, or null when the link is cleared. * * @param mixed $value Raw post ID. * @return int|null|WP_Error */ public static function sanitize_post_id( $value ) { if ( null === $value || '' === $value || false === $value ) { return null; } $post_id = (int) $value; if ( $post_id <= 0 ) { return null; } $post = get_post( $post_id ); if ( ! $post instanceof WP_Post ) { return new WP_Error( 'invalid_post_id', __( 'The linked item does not exist.', 'devnotes' ), array( 'status' => 400 ) ); } return $post_id; } /** * @param mixed $value Raw pin flag. * @return int */ public static function sanitize_pinned( $value ) { return rest_sanitize_boolean( $value ) ? 1 : 0; } /** * User-meta key for assignment notices on one site. * * @param int|null $blog_id Site ID. Defaults to the current site. * @return string */ public static function notices_meta_key( $blog_id = null ) { if ( null === $blog_id ) { $blog_id = get_current_blog_id(); } return 'devnotes_assignment_notices_' . (int) $blog_id; } /** * Shared key used before notices were stored per site. * * @return string */ public static function legacy_notices_meta_key() { return 'devnotes_assignment_notices'; } /** * Notices without a site id belong to the main site. * * @param mixed $notice Stored notice. * @param int $blog_id Site ID. * @return bool */ public static function notice_belongs_to_blog( $notice, $blog_id ) { if ( ! is_array( $notice ) ) { return false; } if ( isset( $notice['blog_id'] ) && '' !== $notice['blog_id'] && null !== $notice['blog_id'] ) { return (int) $notice['blog_id'] === (int) $blog_id; } $main_id = function_exists( 'get_main_site_id' ) ? (int) get_main_site_id() : 1; return (int) $blog_id === $main_id; } /** * Take this site's notices out of the old shared user-meta key. * * @param int $user_id User ID. * @param int $blog_id Site ID. * @return array> */ public static function take_legacy_notices( $user_id, $blog_id ) { $key = self::legacy_notices_meta_key(); $notices = get_user_meta( $user_id, $key, true ); if ( ! is_array( $notices ) || empty( $notices ) ) { return array(); } $mine = array(); $keep = array(); foreach ( $notices as $notice ) { if ( self::notice_belongs_to_blog( $notice, $blog_id ) ) { $mine[] = $notice; } else { $keep[] = $notice; } } if ( empty( $mine ) ) { return array(); } if ( empty( $keep ) ) { delete_user_meta( $user_id, $key ); } else { update_user_meta( $user_id, $key, $keep ); } return $mine; } /** * Delete assignment notices for one site. * * @param int $blog_id Site ID. * @return bool False when the legacy rows could not be read. */ public static function delete_notices_for_blog( $blog_id ) { delete_metadata( 'user', 0, self::notices_meta_key( $blog_id ), '', true ); global $wpdb; $key = self::legacy_notices_meta_key(); // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- read this plugin's legacy user meta so one site can be cleared $rows = $wpdb->get_results( $wpdb->prepare( 'SELECT user_id, meta_value FROM %i WHERE meta_key = %s', $wpdb->usermeta, $key ) ); if ( ! is_array( $rows ) ) { return false; } foreach ( $rows as $row ) { $notices = maybe_unserialize( $row->meta_value ); if ( ! is_array( $notices ) ) { delete_user_meta( (int) $row->user_id, $key ); continue; } $keep = array(); foreach ( $notices as $notice ) { if ( self::notice_belongs_to_blog( $notice, $blog_id ) ) { continue; } $keep[] = $notice; } if ( empty( $keep ) ) { delete_user_meta( (int) $row->user_id, $key ); } elseif ( count( $keep ) !== count( $notices ) ) { update_user_meta( (int) $row->user_id, $key, $keep ); } } return true; } /** * Delete every assignment-notice row this plugin owns. * * @return bool */ public static function delete_all_notices() { global $wpdb; $like = $wpdb->esc_like( 'devnotes_assignment_notices_' ) . '%'; // phpcs:ignore WordPress.DB.DirectDatabaseQuery.DirectQuery, WordPress.DB.DirectDatabaseQuery.NoCaching -- remove this plugin's user meta during uninstall $result = $wpdb->query( $wpdb->prepare( 'DELETE FROM %i WHERE meta_key = %s OR meta_key LIKE %s', $wpdb->usermeta, self::legacy_notices_meta_key(), $like ) ); return false !== $result; } }