1414 * Domain Path: /languages
1515 * Update URI: false
1616 */
17+ // Exit if accessed directly.
1718if ( ! defined ( 'ABSPATH ' ) ) {
1819 exit ;
1920}
2021
22+ // Plugin version and requirements.
2123define ( 'REDIS_QUEUE_VERSION ' , '2.0.0 ' );
2224define ( 'REDIS_QUEUE_MIN_PHP ' , '8.3 ' );
25+
26+ // Plugin file paths and URLs.
2327define ( 'REDIS_QUEUE_PLUGIN_FILE ' , __FILE__ );
2428define ( 'REDIS_QUEUE_PLUGIN_BASENAME ' , plugin_basename ( __FILE__ ) );
2529define ( 'REDIS_QUEUE_PLUGIN_DIR ' , plugin_dir_path ( __FILE__ ) );
3337
3438/**
3539 * One-time migration of options from old prefix redis_queue_demo_ to redis_queue_.
40+ *
41+ * Migrates plugin options from the old naming convention (redis_queue_demo_*)
42+ * to the new naming convention (redis_queue_*). This runs once during plugin
43+ * upgrade from version 1.x to 2.0.0.
3644 */
3745function redis_queue_migrate_options_v2 () {
46+ // Check if migration has already been performed.
3847 if ( get_option ( 'redis_queue_migrated_options_v2 ' ) ) {
3948 return ;
4049 }
50+
51+ // Define all option keys that need to be migrated.
4152 $ option_suffixes = [
4253 'redis_host ' , 'redis_port ' , 'redis_password ' , 'redis_database ' , 'default_queue ' , 'max_jobs_per_run ' ,
4354 'worker_timeout ' , 'max_retries ' , 'retry_backoff ' , 'enable_logging ' , 'cleanup_completed_jobs ' , 'cleanup_after_days ' ,
4455 ];
56+
57+ // Migrate each option from old to new key name.
4558 foreach ( $ option_suffixes as $ suffix ) {
4659 $ old_key = 'redis_queue_demo_ ' . $ suffix ;
4760 $ new_key = 'redis_queue_ ' . $ suffix ;
4861 $ old_val = get_option ( $ old_key , null );
62+
63+ // Only migrate if old value exists and new value doesn't exist.
4964 if ( $ old_val !== null && get_option ( $ new_key , null ) === null ) {
5065 update_option ( $ new_key , $ old_val );
5166 }
5267 }
68+
69+ // Mark migration as complete.
5370 update_option ( 'redis_queue_migrated_options_v2 ' , '1 ' );
5471}
5572add_action ( 'plugins_loaded ' , 'redis_queue_migrate_options_v2 ' , 1 );
5673
57- // Bootstrap namespaced main class (new namespace & class name).
74+ /**
75+ * Bootstrap the main plugin class.
76+ *
77+ * Initializes the Redis Queue plugin if all dependencies are loaded.
78+ * If dependencies are missing (e.g., Composer autoloader not run),
79+ * displays an admin notice to inform the user.
80+ */
5881if ( class_exists ( 'Soderlind\RedisQueue\Core\Redis_Queue ' ) ) {
82+ // Initialize the plugin singleton instance.
5983 Soderlind \RedisQueue \Core \Redis_Queue::get_instance ();
6084} else {
85+ // Show error notice if dependencies are missing.
6186 add_action ( 'admin_notices ' , function () {
6287 if ( current_user_can ( 'activate_plugins ' ) ) {
6388 echo '<div class="notice notice-error"><p> ' ;
@@ -68,13 +93,32 @@ function redis_queue_migrate_options_v2() {
6893 } );
6994}
7095
96+ /**
97+ * Get the main plugin instance.
98+ *
99+ * Provides global access to the Redis Queue plugin singleton instance.
100+ * This is the recommended way to access the plugin's public API.
101+ *
102+ * @return \Soderlind\RedisQueue\Core\Redis_Queue|null Plugin instance or null if not loaded.
103+ */
71104function redis_queue () {
72105 if ( class_exists ( 'Soderlind\RedisQueue\Core\Redis_Queue ' ) ) {
73106 return Soderlind \RedisQueue \Core \Redis_Queue::get_instance ();
74107 }
75108 return null ;
76109}
77110
111+ /**
112+ * Enqueue a background job.
113+ *
114+ * Convenience function to add a job to the Redis queue. This is a wrapper
115+ * around the main plugin's enqueue_job method.
116+ *
117+ * @param string $job_type The type of job (email, image_processing, api_sync, or custom).
118+ * @param array $payload Job-specific data to be processed.
119+ * @param array $options Optional settings: priority, queue, delay.
120+ * @return string|false Job ID on success, false on failure.
121+ */
78122function redis_queue_enqueue_job ( $ job_type , $ payload = array (), $ options = array () ) {
79123 $ instance = redis_queue ();
80124 if ( ! $ instance ) {
@@ -83,11 +127,23 @@ function redis_queue_enqueue_job( $job_type, $payload = array(), $options = arra
83127 return $ instance ->enqueue_job ( $ job_type , $ payload , $ options );
84128}
85129
130+ /**
131+ * Process queued jobs synchronously.
132+ *
133+ * Creates a synchronous worker and processes jobs from the specified queue(s).
134+ * This can be called from WP-CLI, cron jobs, or custom worker scripts.
135+ *
136+ * @param string|array $queue Queue name(s) to process. Default 'default'.
137+ * @param int|null $max_jobs Maximum number of jobs to process. Default from settings.
138+ * @return array|false Processing results array or false on failure.
139+ */
86140function redis_queue_process_jobs ( $ queue = 'default ' , $ max_jobs = null ) {
87141 $ instance = redis_queue ();
88142 if ( ! $ instance || ! $ instance ->get_queue_manager () || ! $ instance ->get_job_processor () ) {
89143 return false ;
90144 }
145+
146+ // Create a synchronous worker and process jobs.
91147 $ worker = new \Soderlind \RedisQueue \Workers \Sync_Worker ( $ instance ->get_queue_manager (), $ instance ->get_job_processor () );
92148 return $ worker ->process_jobs ( (array ) $ queue , $ max_jobs );
93149}
0 commit comments