{"id":3797,"date":"2017-07-08T19:06:04","date_gmt":"2017-07-08T17:06:04","guid":{"rendered":"https:\/\/florianbrinkmann.com\/en\/?p=3797"},"modified":"2020-02-09T10:59:48","modified_gmt":"2020-02-09T09:59:48","slug":"update-wordpress-themes-plugins-gitlab","status":"publish","type":"post","link":"https:\/\/florianbrinkmann.com\/en\/update-wordpress-themes-plugins-gitlab-3797\/","title":{"rendered":"Auto updates for WordPress themes and plugins from private GitLab repos"},"content":{"rendered":"<p class=\"wp-block-paragraph\">The last few days I worked on the following scenario: a WordPress plugin or theme gets its update from a private GitLab repo and checks the Git tags for a new version. The result is a WordPress plugin, which also can be integrated into a plugin or theme directly.<\/p>\n\n\n\n<!--more-->\n\n\n\n<p class=\"wp-block-paragraph\"><a href=\"https:\/\/github.com\/Moenus\/wp-gitlab-updater\/\">You can find the plugin on GitHub<\/a>. It is pretty much my first class orientated PHP code (perhaps even object orientated(?)), and I would be happy about feedback and suggestions to improve it \u2014 here in the comments or in the GitHub repo \ud83d\ude42 I get inspiration for namespaces and tags in code comments from <a href=\"https:\/\/github.com\/schlessera\/better-settings-v1\">a repo<\/a> by <a href=\"https:\/\/www.alainschlesser.com\/\">Alain Schlesser<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Usage as a WordPress plugin<\/h2>\n\n\n\n<figure class=\"wp-block-image alignnone size-full wp-image-3798\"><noscript><img decoding=\"async\" width=\"1229\" height=\"929\" src=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen.png\" alt class=\"wp-image-3798\" srcset=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen.png 1229w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-265x200.png 265w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-300x227.png 300w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-768x581.png 768w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-806x609.png 806w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-600x454.png 600w\" sizes=\"(max-width: 1229px) 100vw, 1229px\"><\/noscript><img decoding=\"async\" width=\"1229\" height=\"929\" src=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201229%20929%22%3E%3C%2Fsvg%3E\" alt class=\"wp-image-3798 lazyload\" srcset=\"data:image\/svg+xml,%3Csvg%20xmlns%3D%22http%3A%2F%2Fwww.w3.org%2F2000%2Fsvg%22%20viewBox%3D%220%200%201229%20929%22%3E%3C%2Fsvg%3E 1229w\" sizes=\"(max-width: 1229px) 100vw, 1229px\" data-srcset=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen.png 1229w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-265x200.png 265w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-300x227.png 300w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-768x581.png 768w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-806x609.png 806w, https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen-600x454.png 600w\" data-src=\"https:\/\/florianbrinkmann.com\/en\/wp-content\/uploads\/sites\/11\/2017\/07\/gitlab-updater-settings-screen.png\"><figcaption>The themes tab of the settings page from the GitLab updater. (Screenshot: own installation)<\/figcaption><\/figure>\n\n\n\n<p class=\"wp-block-paragraph\">But now to the tool. You can use it \u2014 as already said \u2014 as a plugin or included into the theme or plugin which should get updates. If you use it as a plugin, you get a new settings page under <em>Settings<\/em> \u203a <em>GitLab Updater<\/em>, which shows a list with all your themes and one with all your plugins, and for each of that three fields (above in the screenshot you can see the theme tab):<\/p>\n\n\n\n<ul class=\"wp-block-list\"><li><em>Access token<\/em> is the GitLab access token, which needs the <em>api<\/em> and <em>read_registry<\/em> scope. It may be the safest way to create that token for a user with reporter capabilities, who has only access to that particular repo.<\/li><li><em>GitLab URL<\/em> is the URL of the GitLab installation, for example, <code class=\"lang-markup\">https:\/\/gitlab.com\/<\/code>.<\/li><li><em>Repo<\/em> is the ID of the repo like shown in the URL if you open it in GitLab: <code class=\"lang-markup\">username\/project<\/code> or <code class=\"lang-markup\">group\/project<\/code>.<\/li><\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Implemented into a theme or plugin<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">If you only want to use the feature for one theme or plugin in an installation, you can implement it directly (also possible with multiple usages per installation \u2014 for that you need to change the namespace to avoid a conflict with multiple classes of the same name).<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">In a theme<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">To use it in a theme, grab the <code class=\"lang-markup\">src\/ThemeUpdater.php<\/code> and the <code class=\"lang-markup\">src\/UpdaterBase.php<\/code>, throw it into your theme and run the following code from the <code class=\"lang-markup\">functions.php<\/code> (or another file which gets included into the <code class=\"lang-markup\">functions.php<\/code>):<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/**\n * Include the file with the ThemeUpdater class.\n *\/<\/span>\n <span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'wp-gitlab-updater\/theme-updater.php'<\/span>;\n  \n<span class=\"hljs-comment\">\/**\n * Init the theme updater.\n *\/<\/span>\n<span class=\"hljs-keyword\">new<\/span> Moenus\\GitLabUpdater\\ThemeUpdater( &#91;\n    <span class=\"hljs-string\">'slug'<\/span> =&gt; <span class=\"hljs-string\">'SlugOfTheTheme'<\/span>, \n    <span class=\"hljs-string\">'access_token'<\/span> =&gt; <span class=\"hljs-string\">'YourGitLabAccessToken'<\/span>,\n    <span class=\"hljs-string\">'gitlab_url'<\/span> =&gt; <span class=\"hljs-string\">'URLtoGitLabInstall'<\/span>,\n    <span class=\"hljs-string\">'repo'<\/span> =&gt; <span class=\"hljs-string\">'RepoIdentifier'<\/span>,\n] );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">The params are the same as above \u2014 the only additional one is the theme\u2019s <code class=\"lang-markup\">slug<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">In a plugin<\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">For usage of the script inside a plugin, you need the <code class=\"lang-markup\">src\/PluginUpdater.php<\/code> and the <code class=\"lang-markup\">src\/UpdaterBase.php<\/code>. This is how the call can look like:<\/p>\n\n\n<pre class=\"wp-block-code lang-php\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"PHP\" data-shcb-language-slug=\"php\"><span><code class=\"hljs language-php\"><span class=\"hljs-comment\">\/**\n * Include the file with the PluginUpdater class.\n *\/<\/span>\n<span class=\"hljs-keyword\">require_once<\/span> <span class=\"hljs-string\">'wp-gitlab-updater\/plugin-updater.php'<\/span>;\n  \n<span class=\"hljs-comment\">\/**\n * Init the plugin updater with the plugin base name.\n *\/<\/span>\n<span class=\"hljs-keyword\">new<\/span> Moenus\\GitLabUpdater\\PluginUpdater( &#91;\n    <span class=\"hljs-string\">'slug'<\/span> =&gt; <span class=\"hljs-string\">'SlugOfPlugin'<\/span>, \n    <span class=\"hljs-string\">'plugin_base_name'<\/span> =&gt; <span class=\"hljs-string\">'BaseNameOfThePlugin'<\/span>, \n    <span class=\"hljs-string\">'access_token'<\/span> =&gt; <span class=\"hljs-string\">'YourGitLabAccessToken'<\/span>, \n    <span class=\"hljs-string\">'gitlab_url'<\/span> =&gt; <span class=\"hljs-string\">'URLtoGitLabInstall'<\/span>,\n    <span class=\"hljs-string\">'repo'<\/span> =&gt; <span class=\"hljs-string\">'RepoIdentifier'<\/span>,\n] );<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">PHP<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">php<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p class=\"wp-block-paragraph\">One more param here: <code class=\"lang-markup\">plugin_base_name<\/code> is the plugin directory and the name of the main file \u2014 for example, <code class=\"lang-markup\">svg-social-menu\/svg-social-menu.php<\/code>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last few days I worked on the following scenario: a WordPress plugin or theme gets its update from a private GitLab repo and checks the Git tags for a new version. The result is a WordPress plugin, which also can be integrated into a plugin or theme directly.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"wpf_show_in_dewp_planet_feed":false,"flobn_post_versions":"","webmentions_disabled_pings":false,"webmentions_disabled":false,"lazy_load_responsive_images_disabled":false,"footnotes":""},"categories":[42],"tags":[],"class_list":["post-3797","post","type-post","status-publish","format-standard","hentry","category-wordpress-plugins"],"wp-worthy-pixel":{"ignored":false,"public":"18d4e1b6d74148848d78328ddb05a264","server":"vg07.met.vgwort.de","url":"https:\/\/vg07.met.vgwort.de\/na\/18d4e1b6d74148848d78328ddb05a264"},"wp-worthy-type":"normal","_links":{"self":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3797","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/comments?post=3797"}],"version-history":[{"count":2,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3797\/revisions"}],"predecessor-version":[{"id":5873,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/posts\/3797\/revisions\/5873"}],"wp:attachment":[{"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/media?parent=3797"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/categories?post=3797"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/florianbrinkmann.com\/en\/wp-json\/wp\/v2\/tags?post=3797"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}