{"id":21661074,"url":"https://github.com/soulseekah/wp-lock","last_synced_at":"2025-08-11T21:17:02.373Z","repository":{"id":54221106,"uuid":"146006613","full_name":"soulseekah/wp-lock","owner":"soulseekah","description":"Locking and mutexes for the WordPress Core","archived":false,"fork":false,"pushed_at":"2023-05-15T16:02:03.000Z","size":45,"stargazers_count":50,"open_issues_count":1,"forks_count":6,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-08-01T17:12:25.916Z","etag":null,"topics":["concurrency","wordpress"],"latest_commit_sha":null,"homepage":null,"language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/soulseekah.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"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}},"created_at":"2018-08-24T15:09:30.000Z","updated_at":"2024-11-26T16:04:46.000Z","dependencies_parsed_at":"2024-11-24T12:19:01.542Z","dependency_job_id":null,"html_url":"https://github.com/soulseekah/wp-lock","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"purl":"pkg:github/soulseekah/wp-lock","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulseekah%2Fwp-lock","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulseekah%2Fwp-lock/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulseekah%2Fwp-lock/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulseekah%2Fwp-lock/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/soulseekah","download_url":"https://codeload.github.com/soulseekah/wp-lock/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/soulseekah%2Fwp-lock/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":269958590,"owners_count":24503588,"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","status":"online","status_checked_at":"2025-08-11T02:00:10.019Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":["concurrency","wordpress"],"created_at":"2024-11-25T09:43:29.040Z","updated_at":"2025-08-11T21:17:02.308Z","avatar_url":"https://github.com/soulseekah.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# `WP_Lock`\n\n## because WordPress is not thread-safe\n\n[![PHPUnit Tests](https://github.com/soulseekah/wp-lock/actions/workflows/phpunit.yml/badge.svg)](https://github.com/soulseekah/wp-lock/actions/workflows/phpunit.yml)\n\nWordPress is no longer just a blogging platform. It's a framework. And like all mature frameworks it drastically needs a lock API.\n\n## Example\n\nConsider the following user balance topup function that is susceptible to a race condition:\n\n```php\n// topup function that is not thread-safe\npublic function topup_user_balance( $user_id, $topup ) {\n\t$balance = get_user_meta( $user_id, 'balance', true );\n\t$balance = $balance + $topup;\n\tupdate_user_meta( $user_id, 'balance', $balance );\n\treturn $balance;\n}\n```\n\nTry to call the above code 100 times in 16 threads. The balance will be less than it is supposed to be.\n\n```php\n// A thread-safe version of the above topup function.\npublic function topup_user_balance( $user_id, $topup ) {\n\t$user_balance_lock = new WP_Lock( \"$user_id:meta:balance\" );\n\t$user_balance_lock-\u003eacquire( WP_Lock::WRITE );\n\n\t$balance = get_user_meta( $user_id, 'balance', true );\n\t$balance = $balance + $topup;\n\tupdate_user_meta( $user_id, 'balance', $balance );\n\n\t$user_balance_lock-\u003erelease();\n\n\treturn $balance;\n}\n```\n\nThe above code is thread safe.\n\n## Lock levels\n\n- `WP_Lock::READ` - other processes can acquire READ but not WRITE until the original lock is released. A shared read lock.\n- `WP_Lock::WRITE` (default) - other processes can't acquire READ or WRITE locks until the original lock is released. An exclusive read-write lock\n\n## Usage\n\nRequire via Composer `composer require soulseekah/wp-lock` in your plugin.\n\n```php\nuse soulseekah\\WP_Lock;\n\nrequire 'vendor/autoload.php';\n\n$lock = new WP_Lock\\WP_Lock( 'my-first-lock' );\n```\n\n## Caveats\n\nIn highly concurrent setups you may get Deadlock errors from MySQL. This is normal. The library handles these gracefully and retries the query as needed.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoulseekah%2Fwp-lock","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsoulseekah%2Fwp-lock","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsoulseekah%2Fwp-lock/lists"}