| 1 |
<?php |
| 2 |
|
| 3 |
/** |
| 4 |
* Plugin Name: Quick Download Button |
| 5 |
* Plugin URI: https://github.com/kusimo/quick-download-button |
| 6 |
* Description: Create download buttons with countdown timers and file links. Upgrade to unlock email capture, analytics, and secure downloads. |
| 7 |
* Version: 1.4.0 |
| 8 |
* Author: sidocode |
| 9 |
* Author URI: https://www.sidocode.com |
| 10 |
* License: GPL-2.0+ |
| 11 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 12 |
* Contributors: sidocode, kusimo |
| 13 |
* Requires at least: 5.8 |
| 14 |
* Requires PHP: 7.4 |
| 15 |
* Tested up to: 6.9 |
| 16 |
* Text Domain: quick-download-button |
| 17 |
* |
| 18 |
* @package quick-download-button |
| 19 |
*/ |
| 20 |
|
| 21 |
defined( 'ABSPATH' ) || exit; |
| 22 |
|
| 23 |
// Define globals properties |
| 24 |
define( 'QDBN__PLUGIN_DIR', plugin_dir_path( __FILE__ ) ); |
| 25 |
define( 'QDBN__PLUGIN_URI', plugin_dir_url( __FILE__ ) ); |
| 26 |
define( 'QDBN__VERSION', '1.4.0' ); |
| 27 |
define( 'QDBN__DB_VERSION', '1.5' ); // bump when schema changes |
| 28 |
|
| 29 |
/** |
| 30 |
* Load translations for the plugin from the /languages/ folder. |
| 31 |
* |
| 32 |
* @link https://developer.wordpress.org/reference/functions/load_plugin_textdomain/ |
| 33 |
*/ |
| 34 |
|
| 35 |
add_action( 'init', 'qdbu_quick_download_button_load_textdomain' ); |
| 36 |
|
| 37 |
/** |
| 38 |
* function qdbu_quick_download_button_load_textdomain |
| 39 |
* |
| 40 |
* @return void |
| 41 |
*/ |
| 42 |
function qdbu_quick_download_button_load_textdomain() { |
| 43 |
load_plugin_textdomain( 'quick-download-button', false, basename( __DIR__ ) . '/languages' ); |
| 44 |
} |
| 45 |
|
| 46 |
/** |
| 47 |
* Settings Page @todo |
| 48 |
* We need to add a settings page to provide UI for user to generate shortcode. |
| 49 |
*/ |
| 50 |
/* |
| 51 |
require_once plugin_dir_path( __FILE__ ) . '/class/settings.class.php'; |
| 52 |
|
| 53 |
add_filter( 'plugin_action_links_' . plugin_basename( __FILE__ ), 'quick_download_button_page_settings_link' ); |
| 54 |
function quick_download_button_page_settings_link( $links ) { |
| 55 |
$links[] = '<a href="' . |
| 56 |
admin_url( 'options-general.php?page=quick-download-button-dashboard' ) . |
| 57 |
'">' . __( 'Settings' ) . '</a>'; |
| 58 |
return $links; |
| 59 |
} |
| 60 |
*/ |
| 61 |
|
| 62 |
/** |
| 63 |
* Registers all block assets so that they can be enqueued through the Block Editor in |
| 64 |
* the corresponding context. |
| 65 |
* |
| 66 |
* @link https://wordpress.org/gutenberg/handbook/designers-developers/developers/block-api/block-registration/ |
| 67 |
*/ |
| 68 |
add_action( 'init', 'qdbu_quick_download_button_register_blocks' ); |
| 69 |
|
| 70 |
/** |
| 71 |
* function qdbu_quick_download_button_register_blocks |
| 72 |
* |
| 73 |
* @return void |
| 74 |
*/ |
| 75 |
function qdbu_quick_download_button_register_blocks() { |
| 76 |
//If Block Editor is not active, bail. |
| 77 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 78 |
return; |
| 79 |
} |
| 80 |
|
| 81 |
/** |
| 82 |
* Use asset file to automatically set the dependency list for enqueuing the script |
| 83 |
*/ |
| 84 |
$quick_download_button_asset_file = include plugin_dir_path( __FILE__ ) . 'build/index.asset.php'; |
| 85 |
|
| 86 |
//Register the block editor script |
| 87 |
wp_register_script( |
| 88 |
'quick-download-button-editor-script', // label |
| 89 |
plugins_url( 'build/index.js', __FILE__ ), // script file |
| 90 |
$quick_download_button_asset_file['dependencies'], // dependencies |
| 91 |
filemtime( plugin_dir_path( __FILE__ ) . 'build/index.js' ) // set version as file last modified time |
| 92 |
); |
| 93 |
|
| 94 |
//Localise script - download page ID |
| 95 |
/** |
| 96 |
* Filter: qdb_editor_localize_data |
| 97 |
* |
| 98 |
* Modify the data passed to the Gutenberg block editor script. |
| 99 |
* Used by Pro: inject pro block attributes, available product list, etc. |
| 100 |
* |
| 101 |
* @param array $data Localized data for the editor script. |
| 102 |
*/ |
| 103 |
$editor_localize_data = apply_filters( |
| 104 |
'qdb_editor_localize_data', |
| 105 |
array( |
| 106 |
'download_page_id' => (int) get_option( 'qdbu_quick_download_button_page_id' ), |
| 107 |
'qdbn_user_roles' => qdbn_get_user_rolls(), |
| 108 |
) |
| 109 |
); |
| 110 |
wp_localize_script( 'quick-download-button-editor-script', 'qdbu_data', $editor_localize_data ); |
| 111 |
|
| 112 |
//Register blocks script and styles |
| 113 |
register_block_type( |
| 114 |
'quick-download-button/download-button', |
| 115 |
array( |
| 116 |
'editor_script' => 'quick-download-button-editor-script', // Calls registered script above |
| 117 |
'editor_style' => 'quick-download-button-editor-styles', // Calls registered stylesheet above |
| 118 |
'style' => 'quick-download-button-front-end-styles', |
| 119 |
) |
| 120 |
); |
| 121 |
|
| 122 |
register_block_type( |
| 123 |
'quick-download-button/button-row', |
| 124 |
array( |
| 125 |
'editor_script' => 'quick-download-button-editor-script', |
| 126 |
'editor_style' => 'quick-download-button-editor-styles', |
| 127 |
'style' => 'quick-download-button-front-end-styles', |
| 128 |
) |
| 129 |
); |
| 130 |
|
| 131 |
if ( function_exists( 'wp_set_script_translations' ) ) { |
| 132 |
/** |
| 133 |
* Adds internationalization support. |
| 134 |
* |
| 135 |
* @link https://wordpress.org/gutenberg/handbook/designers-developers/developers/internationalization/ |
| 136 |
* @link https://make.wordpress.org/core/2018/11/09/new-javascript-i18n-support-in-wordpress/ |
| 137 |
*/ |
| 138 |
wp_set_script_translations( 'quick-download-button-editor-script', 'quick-download-button', plugin_dir_path( __FILE__ ) . '/languages' ); |
| 139 |
} |
| 140 |
} |
| 141 |
|
| 142 |
/** |
| 143 |
* function qdbu_enqueue_block_editor_assets |
| 144 |
* |
| 145 |
* @return void |
| 146 |
*/ |
| 147 |
function qdbu_enqueue_block_editor_assets() { |
| 148 |
// Enqueue editor-specific styles |
| 149 |
wp_enqueue_style( |
| 150 |
'quick-download-button-editor-styles', // Handle for editor styles |
| 151 |
plugins_url('css/editor.css', __FILE__), // Editor CSS file |
| 152 |
array('wp-edit-blocks'), // Dependencies |
| 153 |
filemtime(plugin_dir_path(__FILE__) . 'css/editor.css') // Version: file last modified time |
| 154 |
); |
| 155 |
|
| 156 |
// Enqueue frontend styles as well |
| 157 |
wp_enqueue_style( |
| 158 |
'quick-download-button-front-end-styles', |
| 159 |
plugins_url('css/minified/style.min.css', __FILE__), |
| 160 |
array(), |
| 161 |
filemtime(plugin_dir_path(__FILE__) . 'css/style.css') |
| 162 |
); |
| 163 |
} |
| 164 |
|
| 165 |
add_action('enqueue_block_editor_assets', 'qdbu_enqueue_block_editor_assets'); |
| 166 |
|
| 167 |
/** |
| 168 |
* function has_quick_download_button |
| 169 |
* |
| 170 |
* @param mixed $post_id |
| 171 |
* |
| 172 |
* @return bool |
| 173 |
*/ |
| 174 |
function has_quick_download_button($post_id) { |
| 175 |
$post = get_post($post_id); |
| 176 |
$content = $post ? $post->post_content : ''; |
| 177 |
|
| 178 |
// Check for shortcode |
| 179 |
if (has_shortcode($content, 'quick_download_button')) { |
| 180 |
return true; |
| 181 |
} |
| 182 |
|
| 183 |
// Check for Gutenberg block |
| 184 |
if (has_block('quick-download-button/download-button', $post_id)) { |
| 185 |
return true; |
| 186 |
} |
| 187 |
|
| 188 |
return false; |
| 189 |
} |
| 190 |
|
| 191 |
/** |
| 192 |
* function qdbu_enqueue_download_button_styles |
| 193 |
* |
| 194 |
* Front end CSS |
| 195 |
* |
| 196 |
* @return void |
| 197 |
*/ |
| 198 |
function qdbu_enqueue_download_button_styles() { |
| 199 |
global $post; |
| 200 |
|
| 201 |
if (is_a($post, 'WP_Post') && has_quick_download_button($post->ID)) { |
| 202 |
wp_enqueue_style( |
| 203 |
'quick-download-button-front-end-styles', // label |
| 204 |
plugins_url('css/minified/style.min.css', __FILE__), // CSS file |
| 205 |
array(), // dependencies |
| 206 |
filemtime(plugin_dir_path(__FILE__) . 'css/style.css') // set version as file last modified time |
| 207 |
); |
| 208 |
} |
| 209 |
} |
| 210 |
add_action('wp_enqueue_scripts', 'qdbu_enqueue_download_button_styles'); |
| 211 |
|
| 212 |
/** |
| 213 |
* function qdbu_button_front_end_script |
| 214 |
* |
| 215 |
* Front end Script |
| 216 |
* |
| 217 |
* @return void |
| 218 |
*/ |
| 219 |
function qdbu_button_front_end_script() { |
| 220 |
global $post; |
| 221 |
if (is_a($post, 'WP_Post') && has_quick_download_button($post->ID)) { |
| 222 |
wp_enqueue_script( |
| 223 |
'quick-download-button-frontend-script', |
| 224 |
plugins_url( 'frontend/minified/frontend.js', __FILE__ ), |
| 225 |
array(), |
| 226 |
filemtime( plugin_dir_path( __FILE__ ) . 'frontend/frontend.js' ), |
| 227 |
true |
| 228 |
); |
| 229 |
|
| 230 |
/** |
| 231 |
* Filter: qdb_localize_script_data |
| 232 |
* |
| 233 |
* Modify the data passed to the frontend download button script. |
| 234 |
* Used by Pro: inject email gate config, download limit info, etc. |
| 235 |
* |
| 236 |
* @param array $data Localized data for the frontend script. |
| 237 |
*/ |
| 238 |
$frontend_localize_data = apply_filters( |
| 239 |
'qdb_localize_script_data', |
| 240 |
array( |
| 241 |
'ajaxurl' => admin_url( 'admin-ajax.php' ), |
| 242 |
'security' => wp_create_nonce( 'qdbutton_nonce_action' ), |
| 243 |
'redirecturl' => qdbu_default_url(), |
| 244 |
'qdbn_user_roles' => qdbn_get_current_user_roles(), |
| 245 |
) |
| 246 |
); |
| 247 |
wp_localize_script( 'quick-download-button-frontend-script', 'quick_download_object', $frontend_localize_data ); |
| 248 |
} |
| 249 |
|
| 250 |
} |
| 251 |
add_action( 'wp_enqueue_scripts', 'qdbu_button_front_end_script' ); |
| 252 |
|
| 253 |
|
| 254 |
/** |
| 255 |
* Shortcode |
| 256 |
*/ |
| 257 |
|
| 258 |
require_once 'class/shortcode.class.php'; |
| 259 |
$downloadShortcode = new QDBU_QuickDownloadShortCode( __FILE__ ); |
| 260 |
$downloadShortcode = new QDBU_QuickDownloadShortCode(); |
| 261 |
|
| 262 |
|
| 263 |
/** |
| 264 |
* Set default download page to 'quick-download-button/' |
| 265 |
* Incase user change the page slug, we use download page by ID |
| 266 |
* Download page name can be renamed before using the plugin. Rename just once and before using |
| 267 |
* */ |
| 268 |
if ( ! function_exists( 'qdbu_default_url' ) ) { |
| 269 |
|
| 270 |
/** |
| 271 |
* function qdbu_default_url |
| 272 |
* |
| 273 |
* @return mixed |
| 274 |
*/ |
| 275 |
function qdbu_default_url() { |
| 276 |
|
| 277 |
$quick_download_button_default_page = site_url() . '/quick-download-button/'; |
| 278 |
|
| 279 |
if ( false !== get_option( 'qdbu_quick_download_button_page_id' ) ) { |
| 280 |
$quick_download_button_default_page = get_page_link( get_option( 'qdbu_quick_download_button_page_id' ) ); |
| 281 |
} |
| 282 |
return $quick_download_button_default_page; |
| 283 |
} |
| 284 |
} |
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
add_filter( 'template_include', 'qdbu_download_button_plugin_templates' ); |
| 289 |
|
| 290 |
/** |
| 291 |
* function qdbu_download_button_plugin_templates |
| 292 |
* |
| 293 |
* Get Custom Template for the download page |
| 294 |
* |
| 295 |
* @param mixed $template |
| 296 |
* |
| 297 |
* @return mixed |
| 298 |
*/ |
| 299 |
function qdbu_download_button_plugin_templates( $template ) { |
| 300 |
|
| 301 |
$quick_download_button_pid = get_option( 'qdbu_quick_download_button_page_id' ); |
| 302 |
|
| 303 |
//Let's check we are on the right page and template file exists |
| 304 |
if ( is_page( 'quick-download-button' ) || is_page( $quick_download_button_pid ) && file_exists( plugin_dir_path( __DIR__ ) . 'quick-download-button/templates/qdb-page-template.php' ) ) { |
| 305 |
|
| 306 |
$template = dirname( __FILE__ ) . '/templates/qdb-page-template.php'; |
| 307 |
} |
| 308 |
return $template; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
// register custom meta tag field |
| 313 |
|
| 314 |
/** |
| 315 |
* function qdbu_register_post_meta |
| 316 |
* |
| 317 |
* Feature development - to store the download url in the cutom field. Do not uncomment qdbu_register_post_meta. |
| 318 |
* |
| 319 |
* @return mixed |
| 320 |
*/ |
| 321 |
function qdbu_register_post_meta() { |
| 322 |
register_post_meta( |
| 323 |
'post', |
| 324 |
'_qdbu_download_url', |
| 325 |
array( |
| 326 |
'show_in_rest' => false, |
| 327 |
'single' => true, |
| 328 |
'type' => 'string', |
| 329 |
'auth_callback' => function() { |
| 330 |
return current_user_can( 'edit_posts' ); |
| 331 |
}, |
| 332 |
) |
| 333 |
); |
| 334 |
} |
| 335 |
|
| 336 |
//add_action( 'init', 'qdbu_register_post_meta' ); |
| 337 |
|
| 338 |
/** |
| 339 |
* Create download page after plugin activation |
| 340 |
*/ |
| 341 |
|
| 342 |
require_once 'class/create.downloadpage.class.php'; |
| 343 |
|
| 344 |
$create_download_page = new QDBU_CreateDownloadPage(); |
| 345 |
|
| 346 |
register_activation_hook( __FILE__, array( $create_download_page, 'activation_logic' ) ); |
| 347 |
|
| 348 |
// ── Feature module activation / deactivation ────────────────────────────── |
| 349 |
|
| 350 |
register_activation_hook( __FILE__, 'qdbn_activate_features' ); |
| 351 |
|
| 352 |
/** |
| 353 |
* function qdbn_activate_features |
| 354 |
* |
| 355 |
* @return void |
| 356 |
*/ |
| 357 |
function qdbn_activate_features() { |
| 358 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-settings.php'; |
| 359 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-analytics.php'; |
| 360 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-email-gate.php'; |
| 361 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-expiring-links.php'; |
| 362 |
QDBP_Analytics::create_table(); |
| 363 |
QDBP_Email_Gate::create_table(); |
| 364 |
QDBP_Expiring_Links::create_table(); |
| 365 |
QDBP_Analytics::backfill_counts(); |
| 366 |
QDBP_Analytics::schedule_cleanup(); |
| 367 |
update_option( 'qdbp_db_version', QDBN__DB_VERSION ); |
| 368 |
} |
| 369 |
|
| 370 |
register_deactivation_hook( __FILE__, 'qdbn_deactivate_features' ); |
| 371 |
|
| 372 |
/** |
| 373 |
* function qdbn_deactivate_features |
| 374 |
* |
| 375 |
* @return void |
| 376 |
*/ |
| 377 |
function qdbn_deactivate_features() { |
| 378 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-settings.php'; |
| 379 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-analytics.php'; |
| 380 |
QDBP_Analytics::unschedule_cleanup(); |
| 381 |
} |
| 382 |
|
| 383 |
// ── Load feature modules ─────────────────────────────────────────────────── |
| 384 |
|
| 385 |
add_action( 'plugins_loaded', 'qdbn_load_features' ); |
| 386 |
|
| 387 |
/** |
| 388 |
* function qdbn_load_features |
| 389 |
* |
| 390 |
* @return void |
| 391 |
*/ |
| 392 |
function qdbn_load_features() { |
| 393 |
require_once QDBN__PLUGIN_DIR . 'includes/class-qdbp-loader.php'; |
| 394 |
QDBP_Loader::init(); |
| 395 |
} |
| 396 |
|
| 397 |
|
| 398 |
|