Skip to content

Instantly share code, notes, and snippets.

@pavlo-bondarchuk
Created April 3, 2024 11:49
Show Gist options
  • Save pavlo-bondarchuk/cd7179a723a57013505c6adce1b9f284 to your computer and use it in GitHub Desktop.
Save pavlo-bondarchuk/cd7179a723a57013505c6adce1b9f284 to your computer and use it in GitHub Desktop.
gittify custom theme development
<?php
// Automatic theme updates from the GitHub repository
add_filter('pre_set_site_transient_update_themes', 'automatic_GitHub_updates', 100, 1);
function automatic_GitHub_updates($data) {
// Theme information
$theme_dtls = array(
'theme_parent' => get_option('template'),
'theme_parent_uri' => get_template_directory_uri(),
'theme_name' => get_option('stylesheet'),
'theme_template' => get_stylesheet_directory(), // Folder name of the current theme
'theme_uri' => get_stylesheet_directory_uri(), // URL of the current theme folder
'theme_slug' => 'your_theme_slug',
'theme_dir' => get_theme_root(), // Folder name of the theme root
);
$theme = $theme_dtls['theme_slug'];
error_log('Theme dtls: ' . print_r( $theme_dtls , true ));
// if ($theme != $theme_dtls['theme_slug']) {
// $theme = $theme_dtls['theme_slug'];
// }
error_log('Theme: ' . $theme);
$current = wp_get_theme()->get('Version'); // Get the version of the current theme
// GitHub information - you might want to call this from an environment variable or something similar
$token = 'your_token'; //https://github.com/settings/personal-access-tokens/new
$user = 'your_username'; // The GitHub username hosting the repository
$repo = 'target_repo'; // Repository name as it appears in the URL
$git = array(
"user" => $user,
"repo" => $repo,
"token" => $token
);
$user = $git['user']; // The GitHub username hosting the repository
$repo = $git['repo']; // Repository name as it appears in the URL
$token = $git['token']; // Personal access token
$url = "https://api.github.com/repos/$user/$repo/releases/latest";
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_HTTPHEADER, [
'User-Agent: ' . $user,
'Authorization: token ' . $token,
'Accept: application/json'
]);
$response = curl_exec($curl);
if (curl_errno($curl)) {
error_log('cURL Error: ' . curl_error($curl));
}
curl_close($curl);
$file = json_decode($response);
if ($file) {
$update = preg_replace('/[^0-9.]/', '', $file->tag_name);
//error_log('Automatic GitHub updates: ' . print_r($file, true));
// Only return a response if the new version number is higher than the current version
if ($update > $current) {
$data->response[$theme] = array(
'theme' => $theme,
'new_version' => $update,
'url' => 'https://github.com/'.$user.'/'.$repo,
'package' => $file->zipball_url,
'slug' => $theme,
);
error_log('Theme data: ' . print_r($data->response[$theme], true));
} else {
error_log('No update available');
}
//error_log('Latest version: ' . $update);
//error_log('Current version: ' . $current);
}
error_log('Automatic GitHub updates Check: ' . print_r($data->checked, true));
error_log('Automatic GitHub updates Response: ' . print_r($data->response, true));
// Rename the theme folder back to its original name
if ($theme != $theme_dtls['theme_name']) {
//Rename theme folder back to its original name: ' . $theme_dtls['theme_name'] . ' to ' . $theme
error_log('Theme dtls after: ' . print_r( $theme_dtls , true ));
if ($theme_dtls['theme_name'] != $theme_dtls['theme_slug']) {
$new_dir = $theme_dtls['theme_dir'] . '/' . $theme_dtls['theme_slug'];
rename(get_stylesheet_directory(), $new_dir);
error_log('Theme renamed: ' . $theme_dtls['theme_name'] . ' to ' . get_stylesheet_directory());
error_log('After Theme dtls: ' . print_r( $theme_dtls , true ));
} else {
error_log('Theme not renamed: ' . $theme_dtls['theme_template'] . ' to ' . get_stylesheet_directory());
error_log('After Theme dtls: ' . print_r( $theme_dtls , true ));
}
//rename(get_stylesheet_directory(), $theme);
update_option('template', $theme);
update_option('stylesheet', $theme);
update_option('current_theme', $theme);
//$data->response[$theme_dtls['theme_name']] = $data->response[$theme];
//unset($data->response[$theme]);
}
return $data;
}
add_filter('http_request_args', function($parsed_args, $url) {
/* == Check if the filter has already been applied to the current request == */
if (isset($parsed_args['wpse_http_request_args_modified'])) {
error_log('wpse_http_request_args_modified: ' . print_r($parsed_args, true) . ' ' . print_r($url, true));
return $parsed_args;
}
// Mark the parsed_args to indicate that the filter has been applied
$parsed_args['wpse_http_request_args_modified'] = true;
$token = 'your_token'; //https://github.com/settings/personal-access-tokens/new
$user = 'your_username'; // The GitHub username hosting the repository
$repo = 'target_repo'; // Repository name as it appears in the URL
$git = array(
"user" => $user,
"repo" => $repo,
"token" => $token
);
$user = $git['user']; // The GitHub username hosting the repository
$repo = $git['repo']; // Repository name as it appears in the URL
$token = $git['token']; // Personal access token
if (strpos($url, "$user/$repo") !== false) {
//error_log('contains: ' . print_r($url, true));
$headers = array(
'User-Agent' => $user,
'Authorization' => 'token ' . $token,
'Accept' => 'application/json, application/octet-stream'
);
$parsed_args['headers'] = $headers;
//$parsed_args['headers'] = get_stylesheet_directory( ); // Folder name of the current theme;
error_log('headers: ' . print_r($parsed_args['headers'], true));
$parsed_args['reject_unsafe_urls'] = false;
}
error_log('wpse_http_request_args: ' . print_r($parsed_args, true) . ' ' . print_r($url, true) );
return $parsed_args;
}, 10, 2);
@pavlo-bondarchuk
Copy link
Author

pavlo-bondarchuk commented Apr 4, 2024

class GitHub_Theme_Updater {
    private $user;
    private $repo;
    private $token;

    public function __construct($user, $repo, $token) {
        $this->user = $user;
        $this->repo = $repo;
        $this->token = $token;
        add_filter('pre_set_site_transient_update_themes', [$this, 'automatic_GitHub_updates'], 100, 1);
        add_filter('http_request_args', [$this, 'modify_http_request_args'], 10, 2);
    }

    public function automatic_GitHub_updates($data) {
        $theme_details = $this->get_theme_details();
        $theme_slug = $theme_details['theme_slug'];
        $current_version = wp_get_theme()->get('Version');
        
        $update_data = $this->get_latest_release_info();
        if ($update_data && version_compare($update_data->tag_name, $current_version, '>')) {
            $data->response[$theme_slug] = [
                'theme' => $theme_slug,
                'new_version' => $update_data->tag_name,
                'url' => 'https://github.com/' . $this->user . '/' . $this->repo,
                'package' => $update_data->zipball_url,
                'slug' => $theme_slug,
            ];
        }
        return $data;
    }

    private function get_latest_release_info() {
        $url = "https://api.github.com/repos/{$this->user}/{$this->repo}/releases/latest";
        $curl = curl_init();
        curl_setopt_array($curl, [
            CURLOPT_URL => $url,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_HTTPHEADER => [
                'User-Agent: ' . $this->user,
                'Authorization: token ' . $this->token,
                'Accept: application/json',
            ],
        ]);
        $response = curl_exec($curl);
        curl_close($curl);
        return json_decode($response);
    }

    private function get_theme_details() {
        return [
            'theme_parent' => get_option('template'),
            'theme_parent_uri' => get_template_directory_uri(),
            'theme_name' => get_option('stylesheet'),
            'theme_template' => get_stylesheet_directory(),
            'theme_uri' => get_stylesheet_directory_uri(),
            'theme_slug' => 'your_theme_slug',
            'theme_dir' => get_theme_root(),
        ];
    }

    public function modify_http_request_args($parsed_args, $url) {
        if (strpos($url, "{$this->user}/{$this->repo}") !== false) {
            $parsed_args['headers'] = [
                'User-Agent' => $this->user,
                'Authorization' => 'token ' . $this->token,
                'Accept' => 'application/json, application/octet-stream',
            ];
            $parsed_args['reject_unsafe_urls'] = false;
        }
        return $parsed_args;
    }
}

// Usage
new GitHub_Theme_Updater('your_username', 'target_repo', 'your_token');

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment