| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Sliderberg |
| 4 |
* Plugin URI: https://sliderberg.com/ |
| 5 |
* Description: Slider Block For the Block Editor (Gutenberg). Slide Anything With Ease. |
| 6 |
* Version: 1.2.2 |
| 7 |
* Author: DotCamp |
| 8 |
* Author URI: https://dotcamp.com/ |
| 9 |
* License: GPL v2 or later |
| 10 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 11 |
* Text Domain: sliderberg |
| 12 |
* Domain Path: /languages |
| 13 |
*/ |
| 14 |
|
| 15 |
// If this file is called directly, abort. |
| 16 |
if (!defined('WPINC')) { |
| 17 |
die; |
| 18 |
} |
| 19 |
|
| 20 |
// Define plugin constants |
| 21 |
define( 'SLIDERBERG_VERSION', '1.2.2' ); |
| 22 |
define('SLIDERBERG_PLUGIN_DIR', plugin_dir_path(__FILE__)); |
| 23 |
define('SLIDERBERG_PLUGIN_URL', plugin_dir_url(__FILE__)); |
| 24 |
|
| 25 |
if ( ! function_exists( 'sli_fs' ) ) { |
| 26 |
// Create a helper function for easy SDK access. |
| 27 |
function sli_fs() { |
| 28 |
global $sli_fs; |
| 29 |
|
| 30 |
if ( ! isset( $sli_fs ) ) { |
| 31 |
// Include Freemius SDK. |
| 32 |
require_once dirname( __FILE__ ) . '/vendor/freemius/start.php'; |
| 33 |
$sli_fs = fs_dynamic_init( array( |
| 34 |
'id' => '19340', |
| 35 |
'slug' => 'sliderberg', |
| 36 |
'type' => 'plugin', |
| 37 |
'public_key' => 'pk_f6a90542b187793a33ebb75752ce7', // This is a public key, safe to expose |
| 38 |
'is_premium' => false, |
| 39 |
'has_addons' => false, |
| 40 |
'has_paid_plans' => false, |
| 41 |
'menu' => array( |
| 42 |
'slug' => 'sliderberg-welcome', |
| 43 |
'contact' => false, |
| 44 |
), |
| 45 |
) ); |
| 46 |
} |
| 47 |
|
| 48 |
return $sli_fs; |
| 49 |
} |
| 50 |
|
| 51 |
// Init Freemius. |
| 52 |
sli_fs(); |
| 53 |
// Signal that SDK was initiated. |
| 54 |
do_action( 'sli_fs_loaded' ); |
| 55 |
} |
| 56 |
|
| 57 |
// Include security helpers (must load before admin-welcome.php) |
| 58 |
require_once SLIDERBERG_PLUGIN_DIR . 'includes/security.php'; |
| 59 |
|
| 60 |
// Include admin welcome page |
| 61 |
require_once SLIDERBERG_PLUGIN_DIR . 'includes/admin-welcome.php'; |
| 62 |
|
| 63 |
// Include slider and slide renderer |
| 64 |
require_once SLIDERBERG_PLUGIN_DIR . 'includes/slider-renderer.php'; |
| 65 |
require_once SLIDERBERG_PLUGIN_DIR . 'includes/slide-renderer.php'; |
| 66 |
|
| 67 |
// Include review handler |
| 68 |
require_once SLIDERBERG_PLUGIN_DIR . 'includes/class-review-handler.php'; |
| 69 |
|
| 70 |
/** |
| 71 |
* Registers the block using the metadata loaded from the `block.json` file. |
| 72 |
* Behind the scenes, it registers also all assets so they can be enqueued |
| 73 |
* through the block editor in the corresponding context. |
| 74 |
* |
| 75 |
* @see https://developer.wordpress.org/reference/functions/register_block_type/ |
| 76 |
*/ |
| 77 |
function sliderberg_init() { |
| 78 |
|
| 79 |
// Version assets by file modification time to avoid cache issues |
| 80 |
$editor_version = file_exists( SLIDERBERG_PLUGIN_DIR . 'build/index.css' ) ? filemtime( SLIDERBERG_PLUGIN_DIR . 'build/index.css' ) : SLIDERBERG_VERSION; |
| 81 |
$style_version = file_exists( SLIDERBERG_PLUGIN_DIR . 'build/style-index.css' ) ? filemtime( SLIDERBERG_PLUGIN_DIR . 'build/style-index.css' ) : SLIDERBERG_VERSION; |
| 82 |
$editor_js_version = file_exists( SLIDERBERG_PLUGIN_DIR . 'build/index.js' ) ? filemtime( SLIDERBERG_PLUGIN_DIR . 'build/index.js' ) : SLIDERBERG_VERSION; |
| 83 |
|
| 84 |
// Load editor script dependencies from asset file |
| 85 |
$editor_asset_file = SLIDERBERG_PLUGIN_DIR . 'build/index.asset.php'; |
| 86 |
$editor_dependencies = array('wp-blocks', 'wp-element', 'wp-editor'); |
| 87 |
if ( file_exists( $editor_asset_file ) ) { |
| 88 |
$editor_asset = require $editor_asset_file; |
| 89 |
$editor_dependencies = isset( $editor_asset['dependencies'] ) ? $editor_asset['dependencies'] : $editor_dependencies; |
| 90 |
} |
| 91 |
|
| 92 |
// Register editor script FIRST (required by block.json) |
| 93 |
wp_register_script( |
| 94 |
'sliderberg-editor', |
| 95 |
SLIDERBERG_PLUGIN_URL . 'build/index.js', |
| 96 |
$editor_dependencies, |
| 97 |
$editor_js_version, |
| 98 |
true |
| 99 |
); |
| 100 |
|
| 101 |
// Register shared styles first so block.json can load them on the |
| 102 |
// frontend and inside the editor iframe. |
| 103 |
wp_register_style( |
| 104 |
'sliderberg-style', |
| 105 |
SLIDERBERG_PLUGIN_URL . 'build/style-index.css', |
| 106 |
array(), |
| 107 |
$style_version |
| 108 |
); |
| 109 |
|
| 110 |
// Register editor-only styles after the shared styles. |
| 111 |
wp_register_style( |
| 112 |
'sliderberg-editor', |
| 113 |
SLIDERBERG_PLUGIN_URL . 'build/index.css', |
| 114 |
array( 'sliderberg-style' ), |
| 115 |
$editor_version |
| 116 |
); |
| 117 |
|
| 118 |
// The frontend view script is registered and enqueued on demand in |
| 119 |
// sliderberg_enqueue_view_assets(), called by the render callback. |
| 120 |
|
| 121 |
// Register blocks AFTER assets are registered |
| 122 |
// Register slider block with PHP rendering |
| 123 |
sliderberg_register_slider_block(); |
| 124 |
|
| 125 |
// Register slide block with PHP rendering |
| 126 |
sliderberg_register_slide_block(); |
| 127 |
} |
| 128 |
add_action('init', 'sliderberg_init'); |
| 129 |
|
| 130 |
// Initialize review handler |
| 131 |
add_action('init', function() { |
| 132 |
new \SliderBerg\Review_Handler(); |
| 133 |
}); |
| 134 |
|
| 135 |
// Load plugin text domain |
| 136 |
add_action('plugins_loaded', function() { |
| 137 |
load_plugin_textdomain('sliderberg', false, dirname(plugin_basename(__FILE__)) . '/languages'); |
| 138 |
}); |
| 139 |
|
| 140 |
// Enqueue editor assets |
| 141 |
function sliderberg_editor_assets() { |
| 142 |
wp_enqueue_script( 'sliderberg-editor' ); |
| 143 |
|
| 144 |
// Pass pro status and upgrade URL to the editor JS. |
| 145 |
// isPro is true when the pro plugin is active and its class is loaded. |
| 146 |
$is_pro = class_exists( 'SliderbergPro\Pro_Features' ); |
| 147 |
$upgrade_url = 'https://sliderberg.com/pricing/'; |
| 148 |
|
| 149 |
// Scan assets/images/upsell/ and build a feature-key → URL map. |
| 150 |
// Drop any image named by feature key (e.g. cube-effect.gif) there; no rebuild needed. |
| 151 |
$images_dir = plugin_dir_path( __FILE__ ) . 'assets/images/upsell/'; |
| 152 |
$images_url = plugins_url( 'assets/images/upsell/', __FILE__ ); |
| 153 |
$upsell_images = array(); |
| 154 |
|
| 155 |
if ( is_dir( $images_dir ) ) { |
| 156 |
$allowed_ext = array( 'gif', 'png', 'jpg', 'jpeg', 'webp', 'svg' ); |
| 157 |
foreach ( glob( $images_dir . '*' ) as $file ) { |
| 158 |
$ext = strtolower( pathinfo( $file, PATHINFO_EXTENSION ) ); |
| 159 |
if ( in_array( $ext, $allowed_ext, true ) ) { |
| 160 |
$key = pathinfo( $file, PATHINFO_FILENAME ); |
| 161 |
$upsell_images[ $key ] = $images_url . basename( $file ); |
| 162 |
} |
| 163 |
} |
| 164 |
} |
| 165 |
|
| 166 |
wp_localize_script( |
| 167 |
'sliderberg-editor', |
| 168 |
'sliderbergData', |
| 169 |
array( |
| 170 |
'isPro' => $is_pro, |
| 171 |
'upgradeUrl' => esc_url( $upgrade_url ), |
| 172 |
'upsellImages' => $upsell_images, |
| 173 |
) |
| 174 |
); |
| 175 |
} |
| 176 |
add_action( 'enqueue_block_editor_assets', 'sliderberg_editor_assets' ); |
| 177 |
|
| 178 |
/** |
| 179 |
* Register and enqueue the frontend view assets (Swiper CSS + the vanilla |
| 180 |
* Swiper initializer, plus the block's own frontend styles). |
| 181 |
* |
| 182 |
* Called directly from render_sliderberg_slider_block() rather than hooked to |
| 183 |
* wp_enqueue_scripts behind has_block('sliderberg/sliderberg') — has_block() |
| 184 |
* only scans the current post's raw content, so it misses the block when |
| 185 |
* it's rendered from a synced pattern, template part, query loop, or widget. |
| 186 |
* Calling this from the render callback guarantees it runs whenever the |
| 187 |
* block actually renders, wherever it was placed. wp_enqueue_style()/ |
| 188 |
* wp_enqueue_script() are no-ops on repeat calls, so this is safe to call |
| 189 |
* once per slider on pages with multiple sliders. |
| 190 |
*/ |
| 191 |
function sliderberg_enqueue_view_assets() { |
| 192 |
wp_enqueue_style( |
| 193 |
'sliderberg-style', |
| 194 |
SLIDERBERG_PLUGIN_URL . 'build/style-index.css', |
| 195 |
array(), |
| 196 |
filemtime( SLIDERBERG_PLUGIN_DIR . 'build/style-index.css' ) |
| 197 |
); |
| 198 |
|
| 199 |
wp_enqueue_style( |
| 200 |
'sliderberg-view', |
| 201 |
SLIDERBERG_PLUGIN_URL . 'build/view.css', |
| 202 |
array(), |
| 203 |
filemtime( SLIDERBERG_PLUGIN_DIR . 'build/view.css' ) |
| 204 |
); |
| 205 |
|
| 206 |
$view_asset = require SLIDERBERG_PLUGIN_DIR . 'build/view.asset.php'; |
| 207 |
|
| 208 |
wp_enqueue_script( |
| 209 |
'sliderberg-view', |
| 210 |
SLIDERBERG_PLUGIN_URL . 'build/view.js', |
| 211 |
$view_asset['dependencies'], |
| 212 |
$view_asset['version'], |
| 213 |
true |
| 214 |
); |
| 215 |
} |
| 216 |
|