| 1 |
<?php |
| 2 |
|
| 3 |
if (!class_exists('CTRBPlugin')) { |
| 4 |
class CTRBPlugin |
| 5 |
{ |
| 6 |
public function __construct() |
| 7 |
{ |
| 8 |
add_action('plugins_loaded', [$this, 'load_dependencies']); |
| 9 |
add_action('admin_enqueue_scripts', [$this, 'ctrbAdminScripts']); |
| 10 |
add_shortcode('counters-block', [$this, 'ctrbShortcode']); |
| 11 |
add_action('rest_api_init', [$this, 'register_rest_endpoints']); |
| 12 |
} |
| 13 |
|
| 14 |
public function register_rest_endpoints() |
| 15 |
{ |
| 16 |
register_rest_route('counters-block/v1', '/stats', [ |
| 17 |
'methods' => 'GET', |
| 18 |
'callback' => [$this, 'get_stats_callback'], |
| 19 |
'permission_callback' => function () { |
| 20 |
return current_user_can('edit_posts'); |
| 21 |
} |
| 22 |
]); |
| 23 |
} |
| 24 |
|
| 25 |
public function get_stats_callback($request) |
| 26 |
{ |
| 27 |
$type = $request->get_param('type'); |
| 28 |
$stat = $request->get_param('stat'); |
| 29 |
$value = 0; |
| 30 |
|
| 31 |
if ($type === 'wp_stats') { |
| 32 |
switch ($stat) { |
| 33 |
case 'posts': |
| 34 |
$value = ctrbGetWpPostsCount(); |
| 35 |
break; |
| 36 |
case 'pages': |
| 37 |
$value = ctrbGetWpPagesCount(); |
| 38 |
break; |
| 39 |
case 'comments': |
| 40 |
$value = ctrbGetWpCommentsCount(); |
| 41 |
break; |
| 42 |
case 'users': |
| 43 |
$value = ctrbGetWpUsersCount(); |
| 44 |
break; |
| 45 |
} |
| 46 |
} elseif ($type === 'wc_stats' && class_exists('WooCommerce')) { |
| 47 |
switch ($stat) { |
| 48 |
case 'sales': |
| 49 |
$value = ctrbGetWcSales(); |
| 50 |
break; |
| 51 |
case 'orders': |
| 52 |
$value = ctrbGetWcOrdersCount(); |
| 53 |
break; |
| 54 |
case 'products': |
| 55 |
$value = ctrbGetWcProductsCount(); |
| 56 |
break; |
| 57 |
case 'customers': |
| 58 |
$value = ctrbGetWcCustomersCount(); |
| 59 |
break; |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
return new WP_REST_Response(['value' => (float) $value], 200); |
| 64 |
} |
| 65 |
|
| 66 |
public function load_dependencies() |
| 67 |
{ |
| 68 |
require_once CTRB_DIR_PATH . 'includes/functions.php'; |
| 69 |
require_once CTRB_DIR_PATH . 'counters-block.php'; |
| 70 |
require_once CTRB_DIR_PATH . 'includes/class-ctrbAdmin.php'; |
| 71 |
} |
| 72 |
|
| 73 |
public function ctrbAdminScripts($screen): void |
| 74 |
{ |
| 75 |
global $typenow; |
| 76 |
|
| 77 |
|
| 78 |
if ('counters-block' == $typenow) { |
| 79 |
$assets = include CTRB_DIR_PATH . 'build/admin-dashboard.asset.php'; |
| 80 |
wp_enqueue_script('admin-post-js', CTRB_DIR_URL . 'build/admin-post.js', [], CTRB_VERSION, true); |
| 81 |
wp_enqueue_style('admin-post-css', CTRB_DIR_URL . 'build/admin-post.css', [], CTRB_VERSION); |
| 82 |
wp_enqueue_script('ctrb-admin-dashboard-js', CTRB_DIR_URL . 'build/admin-dashboard.js', array_merge($assets['dependencies'], ['wp-util']), CTRB_VERSION, true); |
| 83 |
wp_enqueue_style('ctrb-admin-dashboard-css', CTRB_DIR_URL . 'build/admin-dashboard.css', [], CTRB_VERSION); |
| 84 |
} |
| 85 |
} |
| 86 |
|
| 87 |
|
| 88 |
function ctrbShortcode($atts) |
| 89 |
{ |
| 90 |
$post_id = $atts['id']; |
| 91 |
$post = get_post($post_id); |
| 92 |
|
| 93 |
if (!$post) { |
| 94 |
return ''; |
| 95 |
} |
| 96 |
|
| 97 |
if (post_password_required($post)) { |
| 98 |
return get_the_password_form($post); |
| 99 |
} |
| 100 |
|
| 101 |
switch ($post->post_status) { |
| 102 |
case 'publish': |
| 103 |
return $this->ctrbDisplayContent($post); |
| 104 |
|
| 105 |
case 'private': |
| 106 |
if (current_user_can('read_private_posts')) { |
| 107 |
return $this->ctrbDisplayContent($post); |
| 108 |
} |
| 109 |
return ''; |
| 110 |
|
| 111 |
case 'draft': |
| 112 |
case 'pending': |
| 113 |
case 'future': |
| 114 |
if (current_user_can('edit_post', $post_id)) { |
| 115 |
return $this->ctrbDisplayContent($post); |
| 116 |
} |
| 117 |
return ''; |
| 118 |
|
| 119 |
default: |
| 120 |
return ''; |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
function ctrbDisplayContent($post) |
| 125 |
{ |
| 126 |
$content = $post->post_content; |
| 127 |
if (function_exists('do_blocks')) { |
| 128 |
$content = do_blocks($content); |
| 129 |
} |
| 130 |
if (function_exists('wp_filter_content_tags')) { |
| 131 |
$content = wp_filter_content_tags($content); |
| 132 |
} |
| 133 |
$content = do_shortcode($content); |
| 134 |
return $content; |
| 135 |
} |
| 136 |
} |
| 137 |
} |
| 138 |
|