{"id":20102086,"url":"https://github.com/doiftrue/kama_cron","last_synced_at":"2025-05-06T07:31:02.150Z","repository":{"id":57678412,"uuid":"159592516","full_name":"doiftrue/Kama_Cron","owner":"doiftrue","description":"Wrapper for WP Cron for easy adding Cron events","archived":false,"fork":false,"pushed_at":"2025-03-04T18:30:27.000Z","size":108,"stargazers_count":9,"open_issues_count":0,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-20T14:42:26.507Z","etag":null,"topics":["cron","wordpress"],"latest_commit_sha":null,"homepage":"","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/doiftrue.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2018-11-29T02:01:06.000Z","updated_at":"2025-03-04T18:30:31.000Z","dependencies_parsed_at":"2025-03-04T19:35:21.094Z","dependency_job_id":null,"html_url":"https://github.com/doiftrue/Kama_Cron","commit_stats":null,"previous_names":[],"tags_count":4,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doiftrue%2FKama_Cron","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doiftrue%2FKama_Cron/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doiftrue%2FKama_Cron/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/doiftrue%2FKama_Cron/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/doiftrue","download_url":"https://codeload.github.com/doiftrue/Kama_Cron/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252639919,"owners_count":21780833,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["cron","wordpress"],"created_at":"2024-11-13T17:28:52.733Z","updated_at":"2025-05-06T07:31:01.848Z","avatar_url":"https://github.com/doiftrue.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"A small class for easy adding WP Cron tasks (jobs).\n\nThis Class allow you to create WordPress Cron tasks in a quick and simple way. In order not to confuse anything, all tasks settings are specified in the first parameter when crating the class instance. The class takes care of all the routine for proper registration of Cron tasks and their intervals. The task handler (callback function) must already exist in PHP or need to be written separately!\n\n\nRequirements\n------------\n\n- PHP: \u003e=7.1\n- WordPress: \u003e=5.0 \n\n\n\nExamples\n--------\n\n\u003e By default, the tasks are registered automatically (it works very fast) when you visit the admin panel OR at WP_CLI request OR at any cron request. If automatic registration is not needed, specify parameter ``auto_activate' =\u003e false`` and activate tasks manually using method ``activate()``. See the example below.\n\n\u003e INFO: You can call ``Kama_Cron`` at the earliest stage of loading WP, starting from the earliest `muplugins_loaded` hook.\n\n\u003e IMPORTANT: The ``Kama_Cron'' code should also work in cron requests, because it registers the necessary WP hooks that will be executed during cron requests. In other words, you CANNOT register a cron job with this code and delete it.\n\n\n### Repeatable job\n\n#### Use the known WP interval (hourly):\n\n```php\nnew \\Kama\\WP\\Kama_Cron( [\n\t'wpkama_core_data_check_update' =\u003e [\n\t\t'callback'      =\u003e 'wpkama_core_data_check_update',\n\t\t'interval_name' =\u003e 'hourly',\n\t]\n] );\n\n/**\n * Cron callback (handler) function.\n */\nfunction wpkama_core_data_check_update(){\n\t// your code to do the cron job\n}\n```\n\n``wpkama_core_data_check_update`` - it's an internal name of WP hook you don't need to use it anywhere in your code - just specify unique understandable name (I think it's a good idea to name it same as callback function).\n\n#### Use the unknown WP interval (10 minutes):\n\n```php\nnew \\Kama\\WP\\Kama_Cron( [\n\t'wpkama_cron_hook' =\u003e [\n\t\t'callback'      =\u003e 'wpkama_cron_func',\n\t\t'interval_name' =\u003e '10 minutes',\n\t],\n] );\n\nfunction wpkama_cron_func(){\n\t// your code to do the cron job\n}\n```\n\n\u003e In this case the class will parse the string ``10 minutes`` and fill in the ``interval_sec`` and ``interval_desc`` parameters itself.\n\n\u003e In ``interval_name`` you can specify the name in the following format: `N (min|minutes|hour|day|month)s` — ``10 minutes``, ``2 hours``, ``5 days``, ``2 months``, then the number will be taken to 'interval_sec' parameter. OR you can specify an existing WP interval: ``hourly``, ``twicedaily``, ``daily``.\n\n\n\n### Single job\n\n#### Single job (once):\n\n```php\nnew \\Kama\\WP\\Kama_Cron( [\n    'single_job' =\u003e [\n        'callback' =\u003e 'single_job_func',\n        'start_time' =\u003e 1679205600, //= strtotime('tomorrow 6am') - (int) get_option('gtm_offset'),\n    ],\n] );\n```\n\n#### Repeatable Single job (once at time):\n\n```php\nnew \\Kama\\WP\\Kama_Cron( [\n    'single_job' =\u003e [\n        'callback' =\u003e 'single_job_func',\n        // start event every day at 6am by site time\n        'start_time' =\u003e strtotime('tomorrow 6am') - (int) get_option('gtm_offset'),\n    ],\n] );\n```\n\n\n### Register more than one task at once:\n\nLet's create 4 task with different intervals. Tasks are registered automatically (it works very fast) when you visit admin panel OR from CLI OR from Cron request. \n\nAdd following code anywhere, for example in `functions.php` OR in plugin.\n\n```php\nnew \\Kama\\WP\\Kama_Cron( [\n\t'id'     =\u003e 'my_cron_jobs',\n\t'events' =\u003e [\n\t\t// first task\n\t\t'wpkama_cron_func' =\u003e [\n\t\t\t'callback'      =\u003e [ MyCronCallbacks::class, 'wpkama_cron_func' ],\n\t\t\t'interval_name' =\u003e '10 min',\n\t\t],\n\t\t// \n\t\t'wpkama_cron_func_2' =\u003e [\n\t\t\t'callback'      =\u003e [ MyCronCallbacks::class, 'wpkama_cron_func_2' ],\n\t\t\t'interval_name' =\u003e '2 hours',\n\t\t\t'start_time'    =\u003e 1679205600, // start at specified UNIX time\n\t\t],\n\t\t// second task\n\t\t'wpkama_cron_func_3' =\u003e [\n\t\t\t'callback'      =\u003e [ MyCronCallbacks::class, 'wpkama_cron_func_3' ],\n\t\t\t'interval_name' =\u003e '2 hours',\n\t\t\t'start_time'    =\u003e strtotime('tomorrow 6am'), // run at 6 a.m. (site time will be added to this time)\n\t\t],\n\t\t// \n\t\t'wpkama_cron_func_4' =\u003e [\n\t\t\t'callback'      =\u003e [ MyCronCallbacks::class, 'wpkama_cron_func_4' ],\n\t\t\t'interval_name' =\u003e 'hourly', // this is already a known WP interval\n\t\t],\n\t],\n] );\n\nclass MyCronCallbacks {\n\n\tpublic static function wpkama_cron_func(){\n\t\t$file = dirname( ABSPATH ) .'/__cron_check.txt';\n\t\t$content = current_time('mysql') .\"\\n\";\n\t\tfile_put_contents( $file, $content, FILE_APPEND );\n\t}\n\t\n\tpublic static function wpkama_cron_func_2(){\n\t\t// do something\n\t}\n\t\n\tpublic static function wpkama_cron_func_3(){\n\t\t// do something\n\t}\n\t\n\tpublic static function wpkama_cron_func_4(){\n\t\t// do something\n\t}\n}\n```\n\n\n\n### Register tasks when activating the plugin\n\nThe code below shows how to activate and deactivate tasks customly - when activating/deactivating the plugin.\n\nIMPORTANT: in this case the parameter auto_activate must be false: `'auto_activate' =\u003e false`!\n\n```php\n// Example of activation and deactivation when the `auto_activate = false`\nregister_activation_hook( __FILE__, function(){\n\t\\Kama\\WP\\Kama_Cron::get( 'my_cron_jobs_2' )-\u003eactivate();\n} );\n\nregister_deactivation_hook( __FILE__, function(){\n\t\\Kama\\WP\\Kama_Cron::get( 'my_cron_jobs_2' )-\u003edeactivate();\n} );\n\nnew \\Kama\\WP\\Kama_Cron( [\n\t'id' =\u003e 'my_cron_jobs_2',\n\t'auto_activate' =\u003e false, // !IMPORTANT\n\t'events' =\u003e [\n\t\t'wpkama_cron_func_4' =\u003e [\n\t\t\t'callback'      =\u003e 'wpkama_cron_func_4',\n\t\t\t'interval_name' =\u003e 'twicedaily',\n\t\t],\n\t\t'wpkama_cron_func_5' =\u003e [\n\t\t\t'callback'      =\u003e 'wpkama_cron_func_5',\n\t\t\t'interval_name' =\u003e '2 hours',\n\t\t],\n\t],\n] );\n\nfunction wpkama_cron_func_4(){\n\t// code here\n}\n\nfunction wpkama_cron_func_5(){\n\t// code here\n}\n```\n\n\u003e INFO: deactivate() method will deactivate all the jobs from current pack (in the example above these are two jobs).\n\n--\n\nPlugin page: https://wp-kama.com/1353/kama_cron\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoiftrue%2Fkama_cron","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdoiftrue%2Fkama_cron","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdoiftrue%2Fkama_cron/lists"}