Repository navigation
Expand file tree
/
Copy pathgh-commit-widget.php
More file actions
69 lines (56 loc) · 2.26 KB
/
Copy pathgh-commit-widget.php
File metadata and controls
69 lines (56 loc) · 2.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<?php
/**
* Plugin Name: GitHub Commit Widget
* Plugin URI: https://github.com/KokkieH/Custom-Woo-Project
* Description: Block widget to embed GitHub commits for a particular repository
* Version: 1.0.0
* Requires at least: 5.3
* Requires PHP: 5.6
* Author: Herman Kok
* Author URI: https://kokkieh.blog/
* License: GPL v2 or later
* License URI: https://www.gnu.org/licenses/gpl-2.0.html
* Text Domain: pdev
* Domain Path: /public/lang
*/
function commit_product_summary() {
$commit_hash = 'eb1d6e113014a1c6fbfc0c4b8e7420b29d98d131';
$commit_url = site_url( '/wp-json/kokkieh/commit/' . $commit_hash );
$commit_request = wp_remote_get( $commit_url );
if ( is_wp_error( $commit_request ) ) {
return false;
}
$commit_body = wp_remote_retrieve_body( $commit_request );
$commit_data = json_decode( $commit_body );
$block_content = '<h3>Commit hash</h3>' .
'<a href="' . $commit_data->html_url . '">' . substr($commit_data->sha, 0, 7) . '</a>' .
'<h3>Commit Author</h3>' .
'<a href="' . $commit_data->author->html_url . '">' . $commit_data->commit->author->name . '</a>' .
'<h3>Commit message</h3>' .
$commit_data->commit->message;
if ( ! empty( $commit_data ) ) {
return $block_content;
}
}
// shows commit data on WooCommerce products
add_action( 'woocommerce_single_product_summary', 'commit_product_summary' );
function kokkieh_gh_commit_register_block() {
// automatically load dependencies and version
$asset_file = include( plugin_dir_path( __FILE__ ) . 'build/index.asset.php');
wp_register_script(
'kokkieh-github-commit-block',
plugins_url( 'build/index.js', __FILE__ ),
$asset_file['dependencies'],
$asset_file['version']
);
register_block_type( 'kokkieh/github-commit-block', array(
'api_version' => 2,
'editor_script' => 'kokkieh-github-commit-block',
'render_callback' => 'commit_product_summary',
) );
}
add_action( 'init', 'kokkieh_gh_commit_register_block' );
require_once __DIR__ . '/include/class-rest-api.php';
new KokkieH_REST_API();
require_once __DIR__ . '/include/class-admin.php';
new KokkieH_Admin();