| 1 |
<?php |
| 2 |
if ( ! defined( 'ABSPATH' ) ) { exit; } |
| 3 |
|
| 4 |
class VBB_Admin_Menu { |
| 5 |
private $post_type = 'vbb'; |
| 6 |
|
| 7 |
public function __construct() { |
| 8 |
add_action( 'admin_menu', [ $this, 'adminMenu' ] ); |
| 9 |
add_action( 'admin_enqueue_scripts', [ $this, 'adminEnqueueScripts' ] ); |
| 10 |
add_action( 'init', [ $this, 'onInit' ] ); |
| 11 |
add_action( 'wp_ajax_get_license_status', [ $this, 'getLicenseStatus' ] ); |
| 12 |
add_action( 'wp_ajax_activate_freemius_license', [ $this, 'activateFreemiusLicense' ] ); |
| 13 |
add_action( 'wp_ajax_deactivate_freemius_license', [ $this, 'deactivateFreemiusLicense' ] ); |
| 14 |
add_action( 'wp_ajax_vbbSaveUninstallOption', [ $this, 'vbbSaveUninstallOption' ] ); |
| 15 |
add_shortcode( 'vbb', [ $this, 'onAddShortcode' ] ); |
| 16 |
add_filter( 'manage_vbb_posts_columns', [ $this, 'managePostsColumns' ], 10 ); |
| 17 |
add_action( 'manage_vbb_posts_custom_column', [ $this, 'managePostsCustomColumns' ], 10, 2 ); |
| 18 |
add_filter( 'use_block_editor_for_post', [ $this, 'useBlockEditorForPost' ], 999, 2 ); |
| 19 |
} |
| 20 |
|
| 21 |
private function getFreemiusInstance() { |
| 22 |
if ( ! function_exists( 'bvbb_fs' ) ) { |
| 23 |
return null; |
| 24 |
} |
| 25 |
|
| 26 |
$fs = bvbb_fs(); |
| 27 |
return is_object( $fs ) ? $fs : null; |
| 28 |
} |
| 29 |
|
| 30 |
private function getLicenseData( $fs ) { |
| 31 |
$license_key = ''; |
| 32 |
if ( method_exists( $fs, '_get_license' ) ) { |
| 33 |
$license = $fs->_get_license(); |
| 34 |
if ( is_object( $license ) && ! empty( $license->secret_key ) ) { |
| 35 |
$license_key = (string) $license->secret_key; |
| 36 |
} |
| 37 |
} |
| 38 |
|
| 39 |
$is_activated = method_exists( $fs, 'can_use_premium_code' ) ? (bool) $fs->can_use_premium_code() : false; |
| 40 |
|
| 41 |
return [ |
| 42 |
'is_activated' => $is_activated, |
| 43 |
'license_key' => $license_key, |
| 44 |
]; |
| 45 |
} |
| 46 |
|
| 47 |
public function getLicenseStatus() { |
| 48 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 49 |
wp_send_json_error( [ 'message' => __( 'Unauthorized request.', 'video-background' ) ], 403 ); |
| 50 |
} |
| 51 |
|
| 52 |
check_ajax_referer( 'vbb_license_nonce', 'nonce' ); |
| 53 |
|
| 54 |
$fs = $this->getFreemiusInstance(); |
| 55 |
if ( ! $fs ) { |
| 56 |
wp_send_json_success( [ 'is_activated' => false, 'license_key' => '' ] ); |
| 57 |
} |
| 58 |
|
| 59 |
wp_send_json_success( $this->getLicenseData( $fs ) ); |
| 60 |
} |
| 61 |
|
| 62 |
public function activateFreemiusLicense() { |
| 63 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 64 |
wp_send_json_error( [ 'message' => __( 'Unauthorized request.', 'video-background' ) ], 403 ); |
| 65 |
} |
| 66 |
|
| 67 |
check_ajax_referer( 'vbb_license_nonce', 'nonce' ); |
| 68 |
|
| 69 |
$license_key = isset( $_POST['license_key'] ) ? sanitize_text_field( wp_unslash( $_POST['license_key'] ) ) : ''; |
| 70 |
$product_id = isset( $_POST['product_id'] ) ? absint( $_POST['product_id'] ) : 0; |
| 71 |
|
| 72 |
if ( empty( $license_key ) ) { |
| 73 |
wp_send_json_error( [ 'message' => __( 'Please enter a license key.', 'video-background' ) ] ); |
| 74 |
} |
| 75 |
|
| 76 |
$fs = $this->getFreemiusInstance(); |
| 77 |
if ( ! $fs || ! method_exists( $fs, 'activate_migrated_license' ) ) { |
| 78 |
wp_send_json_error( [ 'message' => __( 'License activation is not available.', 'video-background' ) ] ); |
| 79 |
} |
| 80 |
|
| 81 |
$result = $fs->activate_migrated_license( $license_key, null, $product_id ?: null ); |
| 82 |
if ( empty( $result['success'] ) ) { |
| 83 |
$message = ! empty( $result['error'] ) ? $result['error'] : __( 'Activation failed.', 'video-background' ); |
| 84 |
wp_send_json_error( [ 'message' => $message ] ); |
| 85 |
} |
| 86 |
|
| 87 |
wp_send_json_success( $this->getLicenseData( $fs ) ); |
| 88 |
} |
| 89 |
|
| 90 |
public function deactivateFreemiusLicense() { |
| 91 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 92 |
wp_send_json_error( [ 'message' => __( 'Unauthorized request.', 'video-background' ) ], 403 ); |
| 93 |
} |
| 94 |
|
| 95 |
check_ajax_referer( 'vbb_license_nonce', 'nonce' ); |
| 96 |
|
| 97 |
$fs = $this->getFreemiusInstance(); |
| 98 |
if ( ! $fs ) { |
| 99 |
wp_send_json_error( [ 'message' => __( 'License deactivation is not available.', 'video-background' ) ] ); |
| 100 |
} |
| 101 |
|
| 102 |
if ( ! method_exists( $fs, '_deactivate_license' ) ) { |
| 103 |
wp_send_json_error( [ 'message' => __( 'License deactivation is not supported.', 'video-background' ) ] ); |
| 104 |
} |
| 105 |
|
| 106 |
$deactivate = \Closure::bind( function() { |
| 107 |
$this->_deactivate_license( false ); |
| 108 |
}, $fs, get_class( $fs ) ); |
| 109 |
|
| 110 |
$deactivate(); |
| 111 |
|
| 112 |
wp_send_json_success( [ 'is_activated' => false, 'license_key' => '' ] ); |
| 113 |
} |
| 114 |
|
| 115 |
public function adminMenu() { |
| 116 |
$cap = 'manage_options'; |
| 117 |
$slug = 'video-background-block-dashboard'; |
| 118 |
|
| 119 |
add_menu_page( |
| 120 |
__( 'Video Background Block', 'video-background' ), |
| 121 |
__( 'Video Background', 'video-background' ), |
| 122 |
$cap, |
| 123 |
$slug, |
| 124 |
[ $this, 'renderDashboardPage' ], |
| 125 |
'dashicons-format-video', |
| 126 |
26 |
| 127 |
); |
| 128 |
|
| 129 |
add_submenu_page( |
| 130 |
$slug, |
| 131 |
__( 'Help and Demos', 'video-background' ), |
| 132 |
__( 'Help and Demos', 'video-background' ), |
| 133 |
$cap, |
| 134 |
$slug, |
| 135 |
[ $this, 'renderDashboardPage' ] |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
public function renderDashboardPage() { ?> |
| 140 |
<div |
| 141 |
id="video-background-block-dashboard" |
| 142 |
data-info="<?php echo esc_attr( wp_json_encode( [ |
| 143 |
'version' => defined( 'VBB_VERSION' ) ? VBB_VERSION : '1.0.0', |
| 144 |
'licenseActiveNonce' => wp_create_nonce('vbbLicenseActive'), |
| 145 |
'adminUrl' => admin_url(), |
| 146 |
'deleteDataOnUninstall' => (bool) get_option( 'vbbDeleteDataOnUninstall', false ), |
| 147 |
'uninstallNonce' => wp_create_nonce( 'vbb_save_uninstall_option' ), |
| 148 |
|
| 149 |
] ) ); ?>" |
| 150 |
></div> |
| 151 |
<?php } |
| 152 |
|
| 153 |
// Persist the dashboard "delete data on uninstall" toggle. |
| 154 |
// Contract matches bpl-tools/Admin/Settings: reads $_POST['nonce'] and $_POST['enabled']. |
| 155 |
public function vbbSaveUninstallOption() { |
| 156 |
$nonce = sanitize_text_field( wp_unslash( $_POST['nonce'] ?? '' ) ); |
| 157 |
|
| 158 |
if ( ! wp_verify_nonce( $nonce, 'vbb_save_uninstall_option' ) ) { |
| 159 |
wp_send_json_error( [ 'message' => __( 'Invalid security token.', 'video-background' ) ], 403 ); |
| 160 |
} |
| 161 |
|
| 162 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 163 |
wp_send_json_error( [ 'message' => __( 'You do not have permission to perform this action.', 'video-background' ) ], 403 ); |
| 164 |
} |
| 165 |
|
| 166 |
$raw_enabled = isset( $_POST['enabled'] ) ? sanitize_text_field( wp_unslash( $_POST['enabled'] ) ) : ''; |
| 167 |
$enabled = ( 'true' === $raw_enabled || '1' === $raw_enabled ); |
| 168 |
|
| 169 |
update_option( 'vbbDeleteDataOnUninstall', $enabled ); |
| 170 |
|
| 171 |
wp_send_json_success( [ |
| 172 |
'enabled' => $enabled, |
| 173 |
'message' => $enabled |
| 174 |
? __( 'Data deletion enabled.', 'video-background' ) |
| 175 |
: __( 'Data will be preserved on uninstall.', 'video-background' ), |
| 176 |
] ); |
| 177 |
} |
| 178 |
|
| 179 |
public function onInit() { |
| 180 |
register_post_type( $this->post_type, [ |
| 181 |
'labels' => [ |
| 182 |
'name' => __( 'Shortcodes', 'video-background' ), |
| 183 |
'singular_name' => __( 'Shortcode', 'video-background' ), |
| 184 |
'add_new' => __( 'Add New', 'video-background' ), |
| 185 |
'add_new_item' => __( 'Add New Shortcode', 'video-background' ), |
| 186 |
'edit_item' => __( 'Edit Shortcode', 'video-background' ), |
| 187 |
'new_item' => __( 'New Shortcode', 'video-background' ), |
| 188 |
'view_item' => __( 'View Shortcode', 'video-background' ), |
| 189 |
'search_items' => __( 'Search Shortcodes', 'video-background' ), |
| 190 |
'not_found' => __( 'No Shortcodes found.', 'video-background' ), |
| 191 |
], |
| 192 |
'public' => false, |
| 193 |
'show_ui' => true, |
| 194 |
'show_in_rest' => true, |
| 195 |
'publicly_queryable' => false, |
| 196 |
'exclude_from_search' => true, |
| 197 |
'has_archive' => false, |
| 198 |
'hierarchical' => false, |
| 199 |
'show_in_menu' => 'video-background-block-dashboard', |
| 200 |
'menu_position' => 14, |
| 201 |
'capability_type' => 'page', |
| 202 |
'supports' => [ 'title', 'editor' ], |
| 203 |
'template' => [ [ 'vbb/video-bg' ] ], |
| 204 |
'template_lock' => 'all', |
| 205 |
] ); |
| 206 |
} |
| 207 |
|
| 208 |
public function onAddShortcode( $atts ) { |
| 209 |
$atts = shortcode_atts( [ 'id' => 0 ], $atts, 'vbb' ); |
| 210 |
$post_id = absint( $atts['id'] ); |
| 211 |
if ( ! $post_id ) { |
| 212 |
return ''; |
| 213 |
} |
| 214 |
$post = get_post( $post_id ); |
| 215 |
if ( ! $post || $post->post_type !== $this->post_type ) { |
| 216 |
return ''; |
| 217 |
} |
| 218 |
if ( post_password_required( $post ) ) { |
| 219 |
return get_the_password_form( $post ); |
| 220 |
} |
| 221 |
$status = get_post_status( $post ); |
| 222 |
if ( 'publish' === $status ) { |
| 223 |
return $this->displayContent( $post ); |
| 224 |
} |
| 225 |
if ( 'private' === $status ) { |
| 226 |
return current_user_can( 'read_post', $post_id ) ? $this->displayContent( $post ) : ''; |
| 227 |
} |
| 228 |
if ( in_array( $status, [ 'draft', 'pending', 'future' ], true ) ) { |
| 229 |
return current_user_can( 'edit_post', $post_id ) ? $this->displayContent( $post ) : ''; |
| 230 |
} |
| 231 |
return ''; |
| 232 |
} |
| 233 |
|
| 234 |
private function displayContent( $post ) { |
| 235 |
if ( empty( $post->post_content ) ) { |
| 236 |
return ''; |
| 237 |
} |
| 238 |
$old_post = $GLOBALS['post'] ?? null; |
| 239 |
$GLOBALS['post'] = $post; |
| 240 |
setup_postdata( $post ); |
| 241 |
$content = apply_filters( 'the_content', $post->post_content ); |
| 242 |
wp_reset_postdata(); |
| 243 |
$GLOBALS['post'] = $old_post; |
| 244 |
return $content; |
| 245 |
} |
| 246 |
|
| 247 |
public function managePostsColumns( $defaults ) { |
| 248 |
unset( $defaults['date'] ); |
| 249 |
$defaults['shortcode'] = __( 'Shortcode', 'video-background' ); |
| 250 |
$defaults['date'] = __( 'Date', 'video-background' ); |
| 251 |
return $defaults; |
| 252 |
} |
| 253 |
|
| 254 |
public function managePostsCustomColumns( $column_name, $post_ID ) { |
| 255 |
if ( 'shortcode' === $column_name ) { |
| 256 |
$sc = sprintf( '[vbb id="%d"]', (int) $post_ID ); |
| 257 |
echo '<div class="bPlAdminShortcode" id="bPlAdminShortcode-' . esc_attr( $post_ID ) . '"> |
| 258 |
<input readonly value="' . esc_attr( $sc ) . '" onclick="copyBPlAdminShortcode(\'' . esc_attr( $post_ID ) . '\')"> |
| 259 |
<span class="tooltip">' . esc_html__( 'Copy To Clipboard', 'video-background' ) . '</span> |
| 260 |
</div>'; |
| 261 |
} |
| 262 |
} |
| 263 |
|
| 264 |
public function useBlockEditorForPost( $use, $post ) { |
| 265 |
if ( is_object( $post ) && isset( $post->post_type ) && $this->post_type === $post->post_type ) { |
| 266 |
return true; |
| 267 |
} |
| 268 |
return $use; |
| 269 |
} |
| 270 |
|
| 271 |
public function adminEnqueueScripts( $hook ) { |
| 272 |
if ( false !== strpos( $hook, 'video-background-block-dashboard' ) ) { |
| 273 |
if ( file_exists( VBB_DIR_PATH . 'build/admin-dashboard.asset.php' ) ) { |
| 274 |
$asset = include VBB_DIR_PATH . 'build/admin-dashboard.asset.php'; |
| 275 |
$deps = array_unique( array_merge( $asset['dependencies'], [ 'wp-util' ] ) ); |
| 276 |
wp_enqueue_style( 'vbb-admin-dashboard', VBB_DIR_URL . 'build/admin-dashboard.css', [], VBB_VERSION ); |
| 277 |
wp_enqueue_script( 'vbb-admin-dashboard', VBB_DIR_URL . 'build/admin-dashboard.js', $deps, $asset['version'], true ); |
| 278 |
wp_localize_script( 'vbb-admin-dashboard', 'apbAdmin', [ |
| 279 |
'nonce' => wp_create_nonce( 'vbb_license_nonce' ), |
| 280 |
] ); |
| 281 |
wp_set_script_translations( 'vbb-admin-dashboard', 'video-background', VBB_DIR_PATH . 'languages' ); |
| 282 |
} |
| 283 |
return; |
| 284 |
} |
| 285 |
|
| 286 |
$screen = function_exists( 'get_current_screen' ) ? get_current_screen() : null; |
| 287 |
if ( ! $screen || $screen->post_type !== $this->post_type ) { |
| 288 |
return; |
| 289 |
} |
| 290 |
|
| 291 |
if ( in_array( $hook, [ 'edit.php', 'post.php', 'post-new.php' ], true ) ) { |
| 292 |
$asset = file_exists( VBB_DIR_PATH . 'build/admin-post.asset.php' ) |
| 293 |
? include VBB_DIR_PATH . 'build/admin-post.asset.php' |
| 294 |
: [ 'dependencies' => [], 'version' => VBB_VERSION ]; |
| 295 |
wp_enqueue_style( 'vbb-admin-post', VBB_DIR_URL . 'build/admin-post.css', [], VBB_VERSION ); |
| 296 |
wp_enqueue_script( 'vbb-admin-post', VBB_DIR_URL . 'build/admin-post.js', $asset['dependencies'], $asset['version'], true ); |
| 297 |
wp_set_script_translations( 'vbb-admin-post', 'video-background', VBB_DIR_PATH . 'languages' ); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
new VBB_Admin_Menu(); |
| 303 |
|