| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Button Block |
| 4 |
* Description: Implement multi-functional button |
| 5 |
* Version: 1.2.6 |
| 6 |
* Author: bPlugins |
| 7 |
* Author URI: http://bplugins.com |
| 8 |
* License: GPLv3 |
| 9 |
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt |
| 10 |
* Text Domain: button-block |
| 11 |
* Requires at least: 6.5 |
| 12 |
* Tested up to: 7.1 |
| 13 |
* Requires PHP: 7.4 |
| 14 |
*/ |
| 15 |
|
| 16 |
// ABS PATH |
| 17 |
if ( !defined( 'ABSPATH' ) ) { exit; } |
| 18 |
|
| 19 |
if ( function_exists( 'btnb_fs' ) ) { |
| 20 |
btnb_fs()->set_basename( true, __FILE__ ); |
| 21 |
}else{ |
| 22 |
define( 'BTNB_VERSION', ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ? time() : '1.2.6' ); |
| 23 |
define( 'BTNB_DIR_URL', plugin_dir_url( __FILE__ ) ); |
| 24 |
define( 'BTNB_DIR_PATH', plugin_dir_path( __FILE__ ) ); |
| 25 |
define( 'BTNB_HAS_PRO', false ); |
| 26 |
define( 'BTNB_OPTIONS_KEY', 'btn_options' ); |
| 27 |
|
| 28 |
require_once BTNB_DIR_PATH . 'includes/fs-lite.php'; |
| 29 |
|
| 30 |
require_once BTNB_DIR_PATH . 'includes/admin/CPT.php'; |
| 31 |
require_once BTNB_DIR_PATH . 'includes/admin/AdminMenu.php'; |
| 32 |
require_once BTNB_DIR_PATH . 'includes/admin/SubMenu.php'; |
| 33 |
require_once BTNB_DIR_PATH . 'includes/Options.php'; |
| 34 |
|
| 35 |
if( !class_exists( 'BTNBPlugin' ) ){ |
| 36 |
/** |
| 37 |
* Main plugin class responsible for initialization, enqueuing assets, and registering block types. |
| 38 |
* |
| 39 |
* @package BTN |
| 40 |
*/ |
| 41 |
class BTNBPlugin{ |
| 42 |
/** |
| 43 |
* Constructor. |
| 44 |
* Registers hooks for plugin initialization, asset enqueuing, and AJAX handlers. |
| 45 |
*/ |
| 46 |
function __construct(){ |
| 47 |
add_action( 'init', [$this, 'onInit'] ); |
| 48 |
add_action( 'admin_enqueue_scripts', [$this, 'adminEnqueueScripts'] ); |
| 49 |
add_action( 'enqueue_block_editor_assets', [$this, 'enqueueBlockEditorAssets'] ); |
| 50 |
add_action( 'enqueue_block_assets', [$this, 'enqueueBlockAssets'] ); |
| 51 |
|
| 52 |
add_filter( 'plugin_action_links', [$this, 'pluginActionLinks'], 10, 2 ); |
| 53 |
add_filter( 'default_title', [$this, 'defaultTitle'], 10, 2 ); |
| 54 |
add_filter( 'default_content', [$this, 'defaultContent'], 10, 2 ); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Filters the default post title for new pages created via the dashboard "Start Now" link. |
| 59 |
* |
| 60 |
* @param string $title The default post title. |
| 61 |
* @param \WP_Post $post The post object. |
| 62 |
* @return string The filtered post title. |
| 63 |
*/ |
| 64 |
function defaultTitle( $title, $post ) { |
| 65 |
if ( 'page' === $post->post_type && isset( $_GET['title'] ) ) { |
| 66 |
$nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : ''; |
| 67 |
|
| 68 |
if ( wp_verify_nonce( $nonce, 'btnCreatePage' ) ) { |
| 69 |
return sanitize_text_field( wp_unslash( $_GET['title'] ) ); |
| 70 |
} |
| 71 |
} |
| 72 |
return $title; |
| 73 |
} |
| 74 |
|
| 75 |
/** |
| 76 |
* Filters the default post content for new pages created via the dashboard "Start Now" link. |
| 77 |
* |
| 78 |
* @param string $content The default post content. |
| 79 |
* @param \WP_Post $post The post object. |
| 80 |
* @return string The filtered post content. |
| 81 |
*/ |
| 82 |
function defaultContent( $content, $post ) { |
| 83 |
if ( 'page' === $post->post_type && isset( $_GET['content'] ) ) { |
| 84 |
$nonce = isset( $_GET['nonce'] ) ? sanitize_text_field( wp_unslash( $_GET['nonce'] ) ) : ''; |
| 85 |
|
| 86 |
if ( wp_verify_nonce( $nonce, 'btnCreatePage' ) ) { |
| 87 |
return wp_kses_post( wp_unslash( $_GET['content'] ) ); // phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized |
| 88 |
} |
| 89 |
} |
| 90 |
return $content; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* Adds custom action links to the plugin entry on the Plugins page. |
| 95 |
* |
| 96 |
* @param array $links The existing action links. |
| 97 |
* @param string $file The plugin file path. |
| 98 |
* @return array Modified action links. |
| 99 |
*/ |
| 100 |
function pluginActionLinks( $links, $file ) { |
| 101 |
if( plugin_basename( __FILE__ ) === $file ) { |
| 102 |
$helpDemosLink = admin_url( 'true' !== get_option( 'button_block_option', '' ) ? 'edit.php?post_type=button-block&page=button-block#/welcome' : 'tools.php?page=button-block#/welcome' ); |
| 103 |
|
| 104 |
$links['help-and-demos'] = sprintf( '<a href="%s" style="%s">%s</a>', $helpDemosLink, 'color:#FF7A00;font-weight:bold', __( 'Help & Demos', 'button-block' ) ); |
| 105 |
} |
| 106 |
|
| 107 |
return $links; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* Initializes the plugin by registering the block type. |
| 112 |
* |
| 113 |
* @return void |
| 114 |
*/ |
| 115 |
function onInit() { |
| 116 |
register_block_type( __DIR__ . '/build' ); |
| 117 |
|
| 118 |
// Core registers block.json scripts without a path, so local JSONs need this explicitly |
| 119 |
wp_set_script_translations( 'btn-button-editor-script', 'button-block', BTNB_DIR_PATH . 'languages' ); |
| 120 |
wp_set_script_translations( 'btn-button-view-script', 'button-block', BTNB_DIR_PATH . 'languages' ); |
| 121 |
} |
| 122 |
|
| 123 |
/** |
| 124 |
* Enqueues scripts and styles for the admin dashboard pages. |
| 125 |
* |
| 126 |
* @param string $hook The current admin page hook suffix. |
| 127 |
* @return void |
| 128 |
*/ |
| 129 |
function adminEnqueueScripts( $hook ) { |
| 130 |
if( false !== strpos( $hook, 'button-block' ) ){ |
| 131 |
wp_enqueue_style( 'btn-admin-dashboard', BTNB_DIR_URL . 'build/admin/dashboard.css', [], BTNB_VERSION ); |
| 132 |
|
| 133 |
$asset_file = include BTNB_DIR_PATH . 'build/admin/dashboard.asset.php'; |
| 134 |
wp_enqueue_script( 'btn-admin-dashboard', BTNB_DIR_URL . 'build/admin/dashboard.js', array_merge( $asset_file['dependencies'], [ 'wp-util' ] ), BTNB_VERSION, true ); |
| 135 |
wp_set_script_translations( 'btn-admin-dashboard', 'button-block', BTNB_DIR_PATH . 'languages' ); |
| 136 |
} |
| 137 |
} |
| 138 |
|
| 139 |
/** |
| 140 |
* Enqueues inline scripts for the block editor with premium status and pricing URL. |
| 141 |
* |
| 142 |
* @return void |
| 143 |
*/ |
| 144 |
function enqueueBlockEditorAssets(){ |
| 145 |
// FIXED: Extract URL separately and use wp_json_encode() for safe escaping |
| 146 |
$pricing_page = 'true' !== get_option( 'button_block_option', '' ) |
| 147 |
? 'edit.php?post_type=button-block&page=button-block#/pricing' |
| 148 |
: 'tools.php?page=button-block#/pricing'; |
| 149 |
$pricing_url = admin_url( $pricing_page ); |
| 150 |
|
| 151 |
wp_add_inline_script( 'btn-button-editor-script', |
| 152 |
'const btnpricingurl = ' . wp_json_encode( $pricing_url ) . ';', |
| 153 |
'before' |
| 154 |
); |
| 155 |
} |
| 156 |
|
| 157 |
/** |
| 158 |
* Registers shared block assets (Font Awesome, AOS) for both editor and frontend. |
| 159 |
* |
| 160 |
* @return void |
| 161 |
*/ |
| 162 |
function enqueueBlockAssets(){ |
| 163 |
wp_register_style( 'font-awesome-7', BTNB_DIR_URL . 'public/css/font-awesome.min.css', [], '7.1.0' ); |
| 164 |
wp_register_style( 'aos', BTNB_DIR_URL . 'public/css/aos.css', [], '3.0.0' ); |
| 165 |
wp_register_script( 'aos', BTNB_DIR_URL . 'public/js/aos.js', [], '3.0.0', true ); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* Renders the dashboard container div with plugin info as a data attribute. |
| 170 |
* |
| 171 |
* @return void |
| 172 |
*/ |
| 173 |
static function renderDashboard(){ ?> |
| 174 |
<div |
| 175 |
id='btnDashboard' |
| 176 |
data-info='<?php echo esc_attr( wp_json_encode( [ |
| 177 |
'version' => BTNB_VERSION, |
| 178 |
'adminUrl' => admin_url(), |
| 179 |
'startUrl' => admin_url( 'post-new.php?post_type=page&title=' . rawurlencode( 'Button Block' ) . '&content=' . rawurlencode( '<!-- wp:btn/button /-->' ) . '&nonce=' . wp_create_nonce( 'btnCreatePage' ) ), |
| 180 |
'licenseActiveNonce' => wp_create_nonce( 'bPlLicenseActivation' ), |
| 181 |
'deleteDataOnUninstall' => (bool) \BTNB\Options::getOptions()['delete_data_on_uninstall'], |
| 182 |
'uninstallNonce' => wp_create_nonce( 'btnSaveUninstallOption' ) |
| 183 |
] ) ); ?>' |
| 184 |
></div> |
| 185 |
<?php } |
| 186 |
} |
| 187 |
new BTNBPlugin; |
| 188 |
} |
| 189 |
} |