| 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 |
} |
| 12 |
|
| 13 |
public function load_dependencies() |
| 14 |
{ |
| 15 |
require_once CTRB_DIR_PATH . 'includes/functions.php'; |
| 16 |
require_once CTRB_DIR_PATH . 'counters-block.php'; |
| 17 |
require_once CTRB_DIR_PATH . 'includes/class-ctrbAdmin.php'; |
| 18 |
} |
| 19 |
|
| 20 |
public function ctrbAdminScripts($screen): void |
| 21 |
{ |
| 22 |
global $typenow; |
| 23 |
|
| 24 |
|
| 25 |
if ('counters-block' == $typenow) { |
| 26 |
$assets = include CTRB_DIR_PATH . 'build/admin-dashboard.asset.php'; |
| 27 |
wp_enqueue_script('admin-post-js', CTRB_DIR_URL . 'build/admin-post.js', [], CTRB_VERSION, true); |
| 28 |
wp_enqueue_style('admin-post-css', CTRB_DIR_URL . 'build/admin-post.css', [], CTRB_VERSION); |
| 29 |
wp_enqueue_script('ctrb-admin-dashboard-js', CTRB_DIR_URL . 'build/admin-dashboard.js', array_merge($assets['dependencies'], ['wp-util']), CTRB_VERSION, true); |
| 30 |
wp_enqueue_style('ctrb-admin-dashboard-css', CTRB_DIR_URL . 'build/admin-dashboard.css', [], CTRB_VERSION); |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
function ctrbShortcode($atts) |
| 36 |
{ |
| 37 |
$post_id = $atts['id']; |
| 38 |
$post = get_post($post_id); |
| 39 |
|
| 40 |
if (!$post) { |
| 41 |
return ''; |
| 42 |
} |
| 43 |
|
| 44 |
if (post_password_required($post)) { |
| 45 |
return get_the_password_form($post); |
| 46 |
} |
| 47 |
|
| 48 |
switch ($post->post_status) { |
| 49 |
case 'publish': |
| 50 |
return $this->ctrbDisplayContent($post); |
| 51 |
|
| 52 |
case 'private': |
| 53 |
if (current_user_can('read_private_posts')) { |
| 54 |
return $this->ctrbDisplayContent($post); |
| 55 |
} |
| 56 |
return ''; |
| 57 |
|
| 58 |
case 'draft': |
| 59 |
case 'pending': |
| 60 |
case 'future': |
| 61 |
if (current_user_can('edit_post', $post_id)) { |
| 62 |
return $this->ctrbDisplayContent($post); |
| 63 |
} |
| 64 |
return ''; |
| 65 |
|
| 66 |
default: |
| 67 |
return ''; |
| 68 |
} |
| 69 |
} |
| 70 |
|
| 71 |
function ctrbDisplayContent($post) |
| 72 |
{ |
| 73 |
$content = apply_filters('the_content', $post->post_content); |
| 74 |
return $content; |
| 75 |
} |
| 76 |
} |
| 77 |
} |
| 78 |
|