| 1 |
<?php |
| 2 |
/** |
| 3 |
* Plugin Name: Simple Like Page Plugin |
| 4 |
* Plugin URI: https://topdevs.net/simple-like-page-plugin/ |
| 5 |
* Description: A lightweight, privacy-friendly way to embed Facebook Page feeds on WordPress with performance and consent in mind. |
| 6 |
* Version: 2.0.0 |
| 7 |
* Requires at least: 5.8 |
| 8 |
* Requires PHP: 7.2 |
| 9 |
* Author: topdevs.net |
| 10 |
* Author URI: https://topdevs.net |
| 11 |
* License: GPL v2 or later |
| 12 |
* License URI: https://www.gnu.org/licenses/gpl-2.0.html |
| 13 |
* Text Domain: simple-like-page-plugin |
| 14 |
* |
| 15 |
* This plugin is not affiliated with, endorsed by, or sponsored by Meta Platforms, Inc. |
| 16 |
* All trademarks belong to their respective owners. |
| 17 |
*/ |
| 18 |
|
| 19 |
define( "SFP_VERSION", '2.0' ); |
| 20 |
|
| 21 |
/** |
| 22 |
* Main SF Plugin Class |
| 23 |
* |
| 24 |
* Contains the main functions for SF and stores variables |
| 25 |
* |
| 26 |
* @since SF Plugin 1.2 |
| 27 |
* @author Ilya K. |
| 28 |
*/ |
| 29 |
|
| 30 |
// Check if class already exist |
| 31 |
if ( !class_exists( 'SFPlugin' ) ) { |
| 32 |
|
| 33 |
// Main plugin class |
| 34 |
class SFPlugin { |
| 35 |
|
| 36 |
public $pluginPath; |
| 37 |
public $pluginUrl; |
| 38 |
public $pluginName; |
| 39 |
public $optionName; |
| 40 |
public $frontendAssetsEnqueued = false; |
| 41 |
|
| 42 |
public $facebookLocalesUrl = "http://www.facebook.com/translations/FacebookLocales.xml"; |
| 43 |
public $locales; |
| 44 |
|
| 45 |
/** |
| 46 |
* SF Plugin Constructor |
| 47 |
* |
| 48 |
* Gets things started |
| 49 |
*/ |
| 50 |
public function __construct( ) { |
| 51 |
$this->pluginPath = plugin_dir_path(__FILE__); |
| 52 |
$this->pluginUrl = plugin_dir_url(__FILE__); |
| 53 |
|
| 54 |
$this->optionName = "simple_facebook_plugin_options"; |
| 55 |
|
| 56 |
$this->locales = $this->parseLocales($this->facebookLocalesUrl); |
| 57 |
|
| 58 |
$this->loadFiles(); |
| 59 |
$this->addActions(); |
| 60 |
$this->addShortcodes(); |
| 61 |
} |
| 62 |
|
| 63 |
/** |
| 64 |
* Load all the required files. |
| 65 |
*/ |
| 66 |
protected function loadFiles() { |
| 67 |
// Include social plugins files |
| 68 |
require_once( $this->pluginPath . 'lib/sfp-page-plugin.php' ); |
| 69 |
|
| 70 |
// Allow addons load files |
| 71 |
do_action('sfp_load_files'); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Add all the required actions. |
| 76 |
*/ |
| 77 |
protected function addActions() { |
| 78 |
|
| 79 |
add_action( 'admin_menu', array( $this, 'pluginMenu') ); |
| 80 |
add_action( 'admin_notices', array( $this, 'adminNotice') ); |
| 81 |
add_action( 'widgets_init', array( $this, 'addWidgets') ); |
| 82 |
//add_action( 'wp_footer', array( $this, 'addJavaScriptSDK') ); |
| 83 |
add_action( 'admin_init', array( $this, 'saveOptions' ) ); |
| 84 |
add_action( 'admin_init', array( $this, 'ignoreNotices' ) ); |
| 85 |
add_action( 'admin_init', array( $this, 'registerSettings' ) ); |
| 86 |
add_action( 'admin_enqueue_scripts', array( $this, 'enqueueScriptsAdmin') ); |
| 87 |
add_action( 'init', array( $this, 'registerBlock' ) ); |
| 88 |
|
| 89 |
// Add settings links on Plugins page |
| 90 |
$plugin = plugin_basename( __FILE__ ); |
| 91 |
add_filter( 'plugin_row_meta', array( $this, 'pluginRowMeta' ), 10, 2 ); |
| 92 |
|
| 93 |
// Allow addons add actions |
| 94 |
do_action( 'sfp_add_actions', $this ); |
| 95 |
} |
| 96 |
|
| 97 |
/** |
| 98 |
* Register all widgets |
| 99 |
*/ |
| 100 |
public function addWidgets() { |
| 101 |
|
| 102 |
register_widget('SFPPagePluginWidget'); |
| 103 |
|
| 104 |
// Allow addons add widgets |
| 105 |
do_action('sfp_add_widgets'); |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* Register all shortcodes |
| 110 |
*/ |
| 111 |
public function addShortcodes() { |
| 112 |
|
| 113 |
add_shortcode('sfp-page-plugin', 'sfp_page_plugin_shortcode'); |
| 114 |
|
| 115 |
// Allow addons add shortcodes |
| 116 |
do_action('sfp_add_shortcodes'); |
| 117 |
} |
| 118 |
|
| 119 |
/** |
| 120 |
* Get remote XML file by URL |
| 121 |
* |
| 122 |
* @param string $url |
| 123 |
* @return array |
| 124 |
* @since 1.3 |
| 125 |
*/ |
| 126 |
public function parseLocales ( $url = "" ) { |
| 127 |
|
| 128 |
if ( file_exists( $url ) && function_exists( "simplexml_load_file" ) ) { |
| 129 |
|
| 130 |
$locales = array(); |
| 131 |
$xml = simplexml_load_file( $url ); |
| 132 |
|
| 133 |
foreach ( $xml as $key => $locale ) { |
| 134 |
|
| 135 |
$name = (array) $locale->englishName; |
| 136 |
$name = $name[0]; |
| 137 |
|
| 138 |
$code = (array) $locale->codes->code->standard->representation; |
| 139 |
$code = $code[0]; |
| 140 |
|
| 141 |
$locales[$code] = $name; |
| 142 |
}; |
| 143 |
} |
| 144 |
else |
| 145 |
$locales = array( |
| 146 |
"af_ZA"=> "Afrikaans", |
| 147 |
"ar_AR"=> "Arabic", |
| 148 |
"az_AZ"=> "Azerbaijani", |
| 149 |
"be_BY"=> "Belarusian", |
| 150 |
"bg_BG"=> "Bulgarian", |
| 151 |
"bn_IN"=> "Bengali", |
| 152 |
"bs_BA"=> "Bosnian", |
| 153 |
"ca_ES"=> "Catalan", |
| 154 |
"cs_CZ"=> "Czech", |
| 155 |
"cy_GB"=> "Welsh", |
| 156 |
"da_DK"=> "Danish", |
| 157 |
"de_DE"=> "German", |
| 158 |
"el_GR"=> "Greek", |
| 159 |
"en_GB"=> "English (UK)", |
| 160 |
"en_PI"=> "English (Pirate)", |
| 161 |
"en_UD"=> "English (Upside Down)", |
| 162 |
"en_US"=> "English (US)", |
| 163 |
"eo_EO"=> "Esperanto", |
| 164 |
"es_ES"=> "Spanish (Spain)", |
| 165 |
"es_LA"=> "Spanish", |
| 166 |
"et_EE"=> "Estonian", |
| 167 |
"eu_ES"=> "Basque", |
| 168 |
"fa_IR"=> "Persian", |
| 169 |
"fb_LT"=> "Leet Speak", |
| 170 |
"fi_FI"=> "Finnish", |
| 171 |
"fo_FO"=> "Faroese", |
| 172 |
"fr_CA"=> "French (Canada)", |
| 173 |
"fr_FR"=> "French (France)", |
| 174 |
"fy_NL"=> "Frisian", |
| 175 |
"ga_IE"=> "Irish", |
| 176 |
"gl_ES"=> "Galician", |
| 177 |
"he_IL"=> "Hebrew", |
| 178 |
"hi_IN"=> "Hindi", |
| 179 |
"hr_HR"=> "Croatian", |
| 180 |
"hu_HU"=> "Hungarian", |
| 181 |
"hy_AM"=> "Armenian", |
| 182 |
"id_ID"=> "Indonesian", |
| 183 |
"is_IS"=> "Icelandic", |
| 184 |
"it_IT"=> "Italian", |
| 185 |
"ja_JP"=> "Japanese", |
| 186 |
"ka_GE"=> "Georgian", |
| 187 |
"km_KH"=> "Khmer", |
| 188 |
"ko_KR"=> "Korean", |
| 189 |
"ku_TR"=> "Kurdish", |
| 190 |
"la_VA"=> "Latin", |
| 191 |
"lt_LT"=> "Lithuanian", |
| 192 |
"lv_LV"=> "Latvian", |
| 193 |
"mk_MK"=> "Macedonian", |
| 194 |
"ml_IN"=> "Malayalam", |
| 195 |
"ms_MY"=> "Malay", |
| 196 |
"nb_NO"=> "Norwegian (bokmal)", |
| 197 |
"ne_NP"=> "Nepali", |
| 198 |
"nl_NL"=> "Dutch", |
| 199 |
"nn_NO"=> "Norwegian (nynorsk)", |
| 200 |
"pa_IN"=> "Punjabi", |
| 201 |
"pl_PL"=> "Polish", |
| 202 |
"ps_AF"=> "Pashto", |
| 203 |
"pt_BR"=> "Portuguese (Brazil)", |
| 204 |
"pt_PT"=> "Portuguese (Portugal)", |
| 205 |
"ro_RO"=> "Romanian", |
| 206 |
"ru_RU"=> "Russian", |
| 207 |
"sk_SK"=> "Slovak", |
| 208 |
"sl_SI"=> "Slovenian", |
| 209 |
"sq_AL"=> "Albanian", |
| 210 |
"sr_RS"=> "Serbian", |
| 211 |
"sv_SE"=> "Swedish", |
| 212 |
"sw_KE"=> "Swahili", |
| 213 |
"ta_IN"=> "Tamil", |
| 214 |
"te_IN"=> "Telugu", |
| 215 |
"th_TH"=> "Thai", |
| 216 |
"tl_PH"=> "Filipino", |
| 217 |
"tr_TR"=> "Turkish", |
| 218 |
"uk_UA"=> "Ukrainian", |
| 219 |
"vi_VN"=> "Vietnamese", |
| 220 |
"zh_CN"=> "Simplified Chinese (China)", |
| 221 |
"zh_HK"=> "Traditional Chinese (Hong Kong)", |
| 222 |
"zh_TW"=> "Traditional Chinese (Taiwan)" |
| 223 |
); |
| 224 |
|
| 225 |
return $locales; |
| 226 |
} |
| 227 |
|
| 228 |
/** |
| 229 |
* Load styles for dashboard |
| 230 |
* |
| 231 |
* @since 1.3.1 |
| 232 |
*/ |
| 233 |
|
| 234 |
static function enqueueScriptsAdmin() { |
| 235 |
|
| 236 |
// add custom css |
| 237 |
wp_register_style( 'sfp-admin-style', plugin_dir_url(__FILE__) . '/lib/css/sfp-admin-style.css' ); |
| 238 |
wp_enqueue_style( 'sfp-admin-style' ); |
| 239 |
|
| 240 |
wp_enqueue_style( 'wp-color-picker' ); |
| 241 |
wp_enqueue_script( 'wp-color-picker' ); |
| 242 |
wp_add_inline_script( |
| 243 |
'wp-color-picker', |
| 244 |
'jQuery(function($){function initSfpColors(context){$(context).find(".sfp-color-field").wpColorPicker();}initSfpColors(document);$(document).on("widget-added widget-updated",function(e,widget){initSfpColors(widget);});});' |
| 245 |
); |
| 246 |
} |
| 247 |
|
| 248 |
/** |
| 249 |
* Load Facebook JavaScript SDK |
| 250 |
* |
| 251 |
* @since 1.3 |
| 252 |
*/ |
| 253 |
|
| 254 |
public function addJavaScriptSDK() { |
| 255 |
$this->enqueueFrontendAssets(); |
| 256 |
} |
| 257 |
|
| 258 |
/** |
| 259 |
* Add Dashboard > Plugins Menu Page |
| 260 |
* |
| 261 |
* @since 1.3 |
| 262 |
*/ |
| 263 |
|
| 264 |
public function pluginMenu() { |
| 265 |
|
| 266 |
add_options_page( 'Simple Like Page Plugin', 'Simple Like Page Plugin', 'manage_options', 'sfp_plugin', array( $this, "pluginMenuView" ) ); |
| 267 |
} |
| 268 |
|
| 269 |
/** |
| 270 |
* Show Menu Page View |
| 271 |
* |
| 272 |
* @since 1.3 |
| 273 |
*/ |
| 274 |
|
| 275 |
public function pluginMenuView() { |
| 276 |
|
| 277 |
$options = $this->getPluginOptions(); |
| 278 |
|
| 279 |
// include Like Box view |
| 280 |
include( $this->pluginPath . 'views/view-menu.php' ); |
| 281 |
|
| 282 |
} |
| 283 |
|
| 284 |
/** |
| 285 |
* Show admin notice |
| 286 |
* |
| 287 |
* @since 1.3 |
| 288 |
*/ |
| 289 |
|
| 290 |
public function adminNotice() { |
| 291 |
|
| 292 |
// don't show any notices for now |
| 293 |
return; |
| 294 |
|
| 295 |
global $current_user; |
| 296 |
$user_id = $current_user->ID; |
| 297 |
|
| 298 |
/* Check that the user hasn't already clicked to ignore the message */ |
| 299 |
if ( ! get_user_meta( $user_id, 'sfp_ignore_notice_4') ) { |
| 300 |
|
| 301 |
echo '<div class="updated"><p>'; |
| 302 |
|
| 303 |
printf( __('Thanks for using our <strong>Simple Like Page Plugin</strong>! We have some other great WordPress plugins <a href="http://codecanyon.net/user/topdevs/portfolio?ref=topdevs">View Portfolio</a> | <a href="%1$s">Hide this</a>'), '?sfp_ignore_4=0'); |
| 304 |
|
| 305 |
echo "</p></div>"; |
| 306 |
|
| 307 |
} |
| 308 |
} |
| 309 |
|
| 310 |
public function ignoreNotices() { |
| 311 |
|
| 312 |
global $current_user; |
| 313 |
$user_id = $current_user->ID; |
| 314 |
|
| 315 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 316 |
return; |
| 317 |
} |
| 318 |
|
| 319 |
/* If user clicks to ignore the notice, add that to their user meta */ |
| 320 |
if ( isset( $_GET['sfp_ignore_4'] ) && '0' == $_GET['sfp_ignore_4'] ) { |
| 321 |
add_user_meta( $user_id, 'sfp_ignore_notice_4', 'true', true); |
| 322 |
} |
| 323 |
} |
| 324 |
|
| 325 |
/** |
| 326 |
* Add status link on plugins page |
| 327 |
* |
| 328 |
* @since 1.3 |
| 329 |
*/ |
| 330 |
|
| 331 |
public function pluginRowMeta( $links, $file ) { |
| 332 |
|
| 333 |
if ( $file !== plugin_basename( __FILE__ ) ) { |
| 334 |
return $links; |
| 335 |
} |
| 336 |
|
| 337 |
$settings_link = '<a href="' . menu_page_url( "sfp_plugin", false ) . '">Settings</a>'; |
| 338 |
|
| 339 |
$inserted = false; |
| 340 |
$updated_links = array(); |
| 341 |
foreach ( $links as $link ) { |
| 342 |
$updated_links[] = $link; |
| 343 |
if ( stripos( $link, 'Visit plugin site' ) !== false ) { |
| 344 |
$updated_links[] = $settings_link; |
| 345 |
$inserted = true; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
if ( ! $inserted ) { |
| 350 |
$updated_links[] = $settings_link; |
| 351 |
} |
| 352 |
|
| 353 |
return $updated_links; |
| 354 |
} |
| 355 |
|
| 356 |
/** |
| 357 |
* Get plugin options |
| 358 |
* |
| 359 |
* @since 1.3 |
| 360 |
*/ |
| 361 |
|
| 362 |
public function getPluginOptions() { |
| 363 |
|
| 364 |
$defaults = array( |
| 365 |
'url' => 'https://www.facebook.com/WordPress/', |
| 366 |
'locale' => "en_US", |
| 367 |
'click_to_load' => 0, |
| 368 |
'lazy_load' => 1, |
| 369 |
'placeholder_text' => 'Click to load Facebook content', |
| 370 |
'placeholder_bg_color' => '#e7f3ff', |
| 371 |
'placeholder_text_color' => '#1877f2' |
| 372 |
); |
| 373 |
|
| 374 |
$defaults = apply_filters( "sfp_default_options", $defaults ); |
| 375 |
|
| 376 |
$options = get_option( $this->optionName, array() ); |
| 377 |
$options = wp_parse_args( $options, $defaults ); |
| 378 |
|
| 379 |
return $options; |
| 380 |
} |
| 381 |
|
| 382 |
/** |
| 383 |
* Save plugin options |
| 384 |
* |
| 385 |
* @since 1.3 |
| 386 |
*/ |
| 387 |
|
| 388 |
public function savePluginOptions( $options = array() ) { |
| 389 |
|
| 390 |
update_option( $this->optionName, $options ); |
| 391 |
} |
| 392 |
|
| 393 |
/** |
| 394 |
* Trigger when settings page form submitted |
| 395 |
* |
| 396 |
* @since 1.3 |
| 397 |
*/ |
| 398 |
|
| 399 |
public function saveOptions() { |
| 400 |
|
| 401 |
//delete_option( $this->optionName ); |
| 402 |
|
| 403 |
// If submit button pressed |
| 404 |
if ( isset( $_POST['sfp_options_saved'] ) ) { |
| 405 |
if ( ! current_user_can( 'manage_options' ) ) { |
| 406 |
return; |
| 407 |
} |
| 408 |
|
| 409 |
$options = $this->getPluginOptions(); |
| 410 |
|
| 411 |
if ( isset( $_POST['locale'] ) && !empty( $_POST['locale'] ) ) { |
| 412 |
|
| 413 |
$options['locale'] = $_POST['locale']; |
| 414 |
} |
| 415 |
|
| 416 |
if ( isset( $_POST['url'] ) && ! empty( $_POST['url'] ) ) { |
| 417 |
$options['url'] = esc_url_raw( $_POST['url'] ); |
| 418 |
} |
| 419 |
|
| 420 |
if ( isset( $_POST['click_to_load'] ) ) { |
| 421 |
$options['click_to_load'] = (int) (bool) $_POST['click_to_load']; |
| 422 |
} |
| 423 |
|
| 424 |
if ( isset( $_POST['lazy_load'] ) ) { |
| 425 |
$options['lazy_load'] = (int) (bool) $_POST['lazy_load']; |
| 426 |
} |
| 427 |
|
| 428 |
if ( isset( $_POST['placeholder_text'] ) ) { |
| 429 |
$options['placeholder_text'] = sanitize_text_field( $_POST['placeholder_text'] ); |
| 430 |
} |
| 431 |
|
| 432 |
if ( isset( $_POST['placeholder_bg_color'] ) ) { |
| 433 |
$options['placeholder_bg_color'] = sanitize_hex_color( $_POST['placeholder_bg_color'] ); |
| 434 |
} |
| 435 |
|
| 436 |
if ( isset( $_POST['placeholder_text_color'] ) ) { |
| 437 |
$options['placeholder_text_color'] = sanitize_hex_color( $_POST['placeholder_text_color'] ); |
| 438 |
} |
| 439 |
|
| 440 |
$this->savePluginOptions( $options ); |
| 441 |
} |
| 442 |
} |
| 443 |
|
| 444 |
public function registerSettings() { |
| 445 |
|
| 446 |
register_setting( |
| 447 |
'sfp_options', |
| 448 |
$this->optionName, |
| 449 |
array( $this, 'sanitizeOptions' ) |
| 450 |
); |
| 451 |
|
| 452 |
add_settings_section( |
| 453 |
'sfp_performance_privacy', |
| 454 |
'Performance & Privacy', |
| 455 |
array( $this, 'renderPerformancePrivacySection' ), |
| 456 |
'sfp_plugin' |
| 457 |
); |
| 458 |
|
| 459 |
add_settings_field( |
| 460 |
'sfp_click_to_load', |
| 461 |
'Enable click-to-load', |
| 462 |
array( $this, 'renderClickToLoadField' ), |
| 463 |
'sfp_plugin', |
| 464 |
'sfp_performance_privacy' |
| 465 |
); |
| 466 |
|
| 467 |
add_settings_field( |
| 468 |
'sfp_lazy_load', |
| 469 |
'Enable lazy loading', |
| 470 |
array( $this, 'renderLazyLoadField' ), |
| 471 |
'sfp_plugin', |
| 472 |
'sfp_performance_privacy' |
| 473 |
); |
| 474 |
|
| 475 |
add_settings_field( |
| 476 |
'sfp_placeholder_text', |
| 477 |
'Placeholder text', |
| 478 |
array( $this, 'renderPlaceholderTextField' ), |
| 479 |
'sfp_plugin', |
| 480 |
'sfp_performance_privacy' |
| 481 |
); |
| 482 |
|
| 483 |
add_settings_field( |
| 484 |
'sfp_placeholder_bg_color', |
| 485 |
'Placeholder background color', |
| 486 |
array( $this, 'renderPlaceholderBgField' ), |
| 487 |
'sfp_plugin', |
| 488 |
'sfp_performance_privacy' |
| 489 |
); |
| 490 |
|
| 491 |
add_settings_field( |
| 492 |
'sfp_placeholder_text_color', |
| 493 |
'Placeholder text color', |
| 494 |
array( $this, 'renderPlaceholderTextColorField' ), |
| 495 |
'sfp_plugin', |
| 496 |
'sfp_performance_privacy' |
| 497 |
); |
| 498 |
|
| 499 |
add_settings_section( |
| 500 |
'sfp_locale', |
| 501 |
'Localization', |
| 502 |
array( $this, 'renderLocaleSection' ), |
| 503 |
'sfp_plugin' |
| 504 |
); |
| 505 |
|
| 506 |
add_settings_field( |
| 507 |
'sfp_url_field', |
| 508 |
'Default Facebook Page URL', |
| 509 |
array( $this, 'renderUrlField' ), |
| 510 |
'sfp_plugin', |
| 511 |
'sfp_locale' |
| 512 |
); |
| 513 |
|
| 514 |
add_settings_field( |
| 515 |
'sfp_locale_field', |
| 516 |
'Language', |
| 517 |
array( $this, 'renderLocaleField' ), |
| 518 |
'sfp_plugin', |
| 519 |
'sfp_locale' |
| 520 |
); |
| 521 |
} |
| 522 |
|
| 523 |
public function sanitizeOptions( $options ) { |
| 524 |
|
| 525 |
$defaults = $this->getPluginOptions(); |
| 526 |
|
| 527 |
$clean = array(); |
| 528 |
$clean['url'] = isset( $options['url'] ) ? esc_url_raw( $options['url'] ) : $defaults['url']; |
| 529 |
$clean['locale'] = isset( $options['locale'] ) ? sanitize_text_field( $options['locale'] ) : $defaults['locale']; |
| 530 |
$clean['click_to_load'] = ! empty( $options['click_to_load'] ) ? 1 : 0; |
| 531 |
$clean['lazy_load'] = ! empty( $options['lazy_load'] ) ? 1 : 0; |
| 532 |
$clean['placeholder_text'] = isset( $options['placeholder_text'] ) ? sanitize_text_field( $options['placeholder_text'] ) : $defaults['placeholder_text']; |
| 533 |
$clean['placeholder_bg_color'] = isset( $options['placeholder_bg_color'] ) ? sanitize_hex_color( $options['placeholder_bg_color'] ) : $defaults['placeholder_bg_color']; |
| 534 |
$clean['placeholder_text_color'] = isset( $options['placeholder_text_color'] ) ? sanitize_hex_color( $options['placeholder_text_color'] ) : $defaults['placeholder_text_color']; |
| 535 |
|
| 536 |
return $clean; |
| 537 |
} |
| 538 |
|
| 539 |
public function renderPerformancePrivacySection() { |
| 540 |
echo '<p>Delays Facebook loading until interaction or when the embed is in view to reduce unnecessary third-party requests.</p>'; |
| 541 |
} |
| 542 |
|
| 543 |
public function renderLocaleSection() { |
| 544 |
echo '<p>Select the Facebook SDK locale used when loading embeds.</p>'; |
| 545 |
} |
| 546 |
|
| 547 |
public function renderUrlField() { |
| 548 |
$options = $this->getPluginOptions(); |
| 549 |
?> |
| 550 |
<input type="url" class="regular-text" name="<?php echo esc_attr( $this->optionName ); ?>[url]" value="<?php echo esc_attr( $options['url'] ); ?>" /> |
| 551 |
<p class="description">Used when a widget, shortcode, or block does not specify a URL.</p> |
| 552 |
<?php |
| 553 |
} |
| 554 |
|
| 555 |
public function renderClickToLoadField() { |
| 556 |
$options = $this->getPluginOptions(); |
| 557 |
?> |
| 558 |
<label> |
| 559 |
<input type="checkbox" name="<?php echo esc_attr( $this->optionName ); ?>[click_to_load]" value="1" <?php checked( 1, (int) $options['click_to_load'] ); ?> /> |
| 560 |
<span>Requires a click before Facebook loads.</span> |
| 561 |
</label> |
| 562 |
<?php |
| 563 |
} |
| 564 |
|
| 565 |
public function renderLazyLoadField() { |
| 566 |
$options = $this->getPluginOptions(); |
| 567 |
?> |
| 568 |
<label> |
| 569 |
<input type="checkbox" name="<?php echo esc_attr( $this->optionName ); ?>[lazy_load]" value="1" <?php checked( 1, (int) $options['lazy_load'] ); ?> /> |
| 570 |
<span>Loads the embed only when it enters the viewport.</span> |
| 571 |
</label> |
| 572 |
<?php |
| 573 |
} |
| 574 |
|
| 575 |
public function renderPlaceholderTextField() { |
| 576 |
$options = $this->getPluginOptions(); |
| 577 |
?> |
| 578 |
<input type="text" class="regular-text" name="<?php echo esc_attr( $this->optionName ); ?>[placeholder_text]" value="<?php echo esc_attr( $options['placeholder_text'] ); ?>" /> |
| 579 |
<p class="description">Displayed before the embed loads.</p> |
| 580 |
<?php |
| 581 |
} |
| 582 |
|
| 583 |
public function renderPlaceholderBgField() { |
| 584 |
$options = $this->getPluginOptions(); |
| 585 |
?> |
| 586 |
<input type="text" class="sfp-color-field" name="<?php echo esc_attr( $this->optionName ); ?>[placeholder_bg_color]" value="<?php echo esc_attr( $options['placeholder_bg_color'] ); ?>" data-default-color="#e7f3ff" /> |
| 587 |
<p class="description">Background color for the placeholder.</p> |
| 588 |
<?php |
| 589 |
} |
| 590 |
|
| 591 |
public function renderPlaceholderTextColorField() { |
| 592 |
$options = $this->getPluginOptions(); |
| 593 |
?> |
| 594 |
<input type="text" class="sfp-color-field" name="<?php echo esc_attr( $this->optionName ); ?>[placeholder_text_color]" value="<?php echo esc_attr( $options['placeholder_text_color'] ); ?>" data-default-color="#1877f2" /> |
| 595 |
<p class="description">Text color for the placeholder.</p> |
| 596 |
<?php |
| 597 |
} |
| 598 |
|
| 599 |
public function renderLocaleField() { |
| 600 |
$options = $this->getPluginOptions(); |
| 601 |
?> |
| 602 |
<select name="<?php echo esc_attr( $this->optionName ); ?>[locale]"> |
| 603 |
<?php foreach ( $this->locales as $code => $name ) : ?> |
| 604 |
<option <?php selected( ( $options['locale'] == $code ) ? 1 : 0 ); ?> value="<?php echo esc_attr( $code ); ?>" ><?php echo esc_html( $name ); ?></option> |
| 605 |
<?php endforeach; ?> |
| 606 |
</select> |
| 607 |
<?php |
| 608 |
} |
| 609 |
|
| 610 |
public function enqueueFrontendAssets() { |
| 611 |
|
| 612 |
if ( $this->frontendAssetsEnqueued ) { |
| 613 |
return; |
| 614 |
} |
| 615 |
|
| 616 |
$this->frontendAssetsEnqueued = true; |
| 617 |
|
| 618 |
$options = $this->getPluginOptions(); |
| 619 |
$should_load = apply_filters( 'sfp_should_load', true ); |
| 620 |
$has_consent = apply_filters( 'sfp_has_consent', true ); |
| 621 |
|
| 622 |
// Hooks for future Pro add-on consent logic. |
| 623 |
do_action( 'sfp_before_load', $should_load, $has_consent ); |
| 624 |
|
| 625 |
wp_enqueue_style( |
| 626 |
'sfp-frontend-style', |
| 627 |
$this->pluginUrl . 'assets/css/simple-facebook-plugin.css', |
| 628 |
array(), |
| 629 |
SFP_VERSION |
| 630 |
); |
| 631 |
|
| 632 |
wp_enqueue_script( |
| 633 |
'sfp-frontend-script', |
| 634 |
$this->pluginUrl . 'assets/js/simple-facebook-plugin.js', |
| 635 |
array(), |
| 636 |
SFP_VERSION, |
| 637 |
true |
| 638 |
); |
| 639 |
|
| 640 |
$settings = array( |
| 641 |
'locale' => isset( $options['locale'] ) ? $options['locale'] : 'en_US', |
| 642 |
'clickToLoadEnabled' => (bool) $options['click_to_load'], |
| 643 |
'lazyLoadEnabled' => (bool) $options['lazy_load'], |
| 644 |
'placeholderText' => isset( $options['placeholder_text'] ) ? $options['placeholder_text'] : 'Click to load Facebook content', |
| 645 |
'shouldLoad' => (bool) $should_load, |
| 646 |
'hasConsent' => (bool) $has_consent, |
| 647 |
); |
| 648 |
|
| 649 |
$settings_json = wp_json_encode( $settings ); |
| 650 |
|
| 651 |
wp_add_inline_script( |
| 652 |
'sfp-frontend-script', |
| 653 |
'window.sfpSettings = window.sfpSettings || {}; window.sfpSettings = Object.assign(window.sfpSettings, ' . $settings_json . ');', |
| 654 |
'before' |
| 655 |
); |
| 656 |
|
| 657 |
// Hook for future Pro add-on to react after loader setup. |
| 658 |
do_action( 'sfp_after_load', $should_load, $has_consent ); |
| 659 |
} |
| 660 |
|
| 661 |
public function registerBlock() { |
| 662 |
|
| 663 |
if ( ! function_exists( 'register_block_type' ) ) { |
| 664 |
return; |
| 665 |
} |
| 666 |
|
| 667 |
$defaults = function_exists( 'sfp_get_page_plugin_defaults' ) |
| 668 |
? sfp_get_page_plugin_defaults() |
| 669 |
: array(); |
| 670 |
|
| 671 |
$url_default = isset( $defaults['url'] ) ? $defaults['url'] : ''; |
| 672 |
$width_default = isset( $defaults['width'] ) ? $defaults['width'] : ''; |
| 673 |
$height_default = isset( $defaults['height'] ) ? $defaults['height'] : ''; |
| 674 |
$hide_cover_default = isset( $defaults['hide_cover'] ) ? (bool) $defaults['hide_cover'] : false; |
| 675 |
$show_facepile_default = isset( $defaults['show_facepile'] ) ? (bool) $defaults['show_facepile'] : true; |
| 676 |
$small_header_default = isset( $defaults['small_header'] ) ? (bool) $defaults['small_header'] : false; |
| 677 |
$timeline_default = isset( $defaults['timeline'] ) ? (bool) $defaults['timeline'] : false; |
| 678 |
$events_default = isset( $defaults['events'] ) ? (bool) $defaults['events'] : false; |
| 679 |
$messages_default = isset( $defaults['messages'] ) ? (bool) $defaults['messages'] : false; |
| 680 |
$locale_default = isset( $defaults['locale'] ) ? $defaults['locale'] : 'en_US'; |
| 681 |
$placeholder_default = isset( $defaults['placeholder_text'] ) ? $defaults['placeholder_text'] : ''; |
| 682 |
$placeholder_bg_default = isset( $defaults['placeholder_bg_color'] ) ? $defaults['placeholder_bg_color'] : ''; |
| 683 |
$placeholder_text_color_default = isset( $defaults['placeholder_text_color'] ) ? $defaults['placeholder_text_color'] : ''; |
| 684 |
|
| 685 |
wp_register_script( |
| 686 |
'sfp-block-editor', |
| 687 |
$this->pluginUrl . 'assets/js/sfp-block.js', |
| 688 |
array( 'wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n' ), |
| 689 |
SFP_VERSION, |
| 690 |
true |
| 691 |
); |
| 692 |
|
| 693 |
$block_defaults = array( |
| 694 |
'url' => $url_default, |
| 695 |
'width' => $width_default, |
| 696 |
'height' => $height_default, |
| 697 |
'hideCover' => $hide_cover_default, |
| 698 |
'showFacepile' => $show_facepile_default, |
| 699 |
'smallHeader' => $small_header_default, |
| 700 |
'timeline' => $timeline_default, |
| 701 |
'events' => $events_default, |
| 702 |
'messages' => $messages_default, |
| 703 |
'locale' => $locale_default, |
| 704 |
'placeholderText' => $placeholder_default, |
| 705 |
'placeholderBgColor' => $placeholder_bg_default, |
| 706 |
'placeholderTextColor' => $placeholder_text_color_default, |
| 707 |
); |
| 708 |
|
| 709 |
wp_add_inline_script( |
| 710 |
'sfp-block-editor', |
| 711 |
'window.sfpBlockDefaults = ' . wp_json_encode( $block_defaults ) . ';', |
| 712 |
'before' |
| 713 |
); |
| 714 |
|
| 715 |
register_block_type( 'simple-facebook-plugin/page', array( |
| 716 |
'editor_script' => 'sfp-block-editor', |
| 717 |
'render_callback' => array( $this, 'renderBlock' ), |
| 718 |
'attributes' => array( |
| 719 |
'url' => array( 'type' => 'string', 'default' => $url_default ), |
| 720 |
'width' => array( 'type' => 'string', 'default' => $width_default ), |
| 721 |
'height' => array( 'type' => 'string', 'default' => $height_default ), |
| 722 |
'hideCover' => array( 'type' => 'boolean', 'default' => $hide_cover_default ), |
| 723 |
'showFacepile' => array( 'type' => 'boolean', 'default' => $show_facepile_default ), |
| 724 |
'smallHeader' => array( 'type' => 'boolean', 'default' => $small_header_default ), |
| 725 |
'timeline' => array( 'type' => 'boolean', 'default' => $timeline_default ), |
| 726 |
'events' => array( 'type' => 'boolean', 'default' => $events_default ), |
| 727 |
'messages' => array( 'type' => 'boolean', 'default' => $messages_default ), |
| 728 |
'locale' => array( 'type' => 'string', 'default' => $locale_default ), |
| 729 |
'clickToLoad' => array( 'type' => 'boolean' ), |
| 730 |
'placeholderText' => array( 'type' => 'string', 'default' => $placeholder_default ), |
| 731 |
'placeholderBgColor' => array( 'type' => 'string', 'default' => $placeholder_bg_default ), |
| 732 |
'placeholderTextColor' => array( 'type' => 'string', 'default' => $placeholder_text_color_default ), |
| 733 |
), |
| 734 |
) ); |
| 735 |
} |
| 736 |
|
| 737 |
public function renderBlock( $attributes ) { |
| 738 |
|
| 739 |
$instance = array( |
| 740 |
'url' => isset( $attributes['url'] ) ? $attributes['url'] : '', |
| 741 |
'width' => isset( $attributes['width'] ) ? $attributes['width'] : '', |
| 742 |
'height' => isset( $attributes['height'] ) ? $attributes['height'] : '', |
| 743 |
'hide_cover' => isset( $attributes['hideCover'] ) ? (bool) $attributes['hideCover'] : false, |
| 744 |
'show_facepile' => isset( $attributes['showFacepile'] ) ? (bool) $attributes['showFacepile'] : true, |
| 745 |
'small_header' => isset( $attributes['smallHeader'] ) ? (bool) $attributes['smallHeader'] : false, |
| 746 |
'timeline' => isset( $attributes['timeline'] ) ? (bool) $attributes['timeline'] : false, |
| 747 |
'events' => isset( $attributes['events'] ) ? (bool) $attributes['events'] : false, |
| 748 |
'messages' => isset( $attributes['messages'] ) ? (bool) $attributes['messages'] : false, |
| 749 |
'locale' => isset( $attributes['locale'] ) ? $attributes['locale'] : 'en_US', |
| 750 |
); |
| 751 |
|
| 752 |
if ( array_key_exists( 'clickToLoad', $attributes ) ) { |
| 753 |
$instance['click_to_load'] = (bool) $attributes['clickToLoad']; |
| 754 |
} |
| 755 |
|
| 756 |
if ( ! empty( $attributes['placeholderText'] ) ) { |
| 757 |
$instance['placeholder_text'] = $attributes['placeholderText']; |
| 758 |
} |
| 759 |
|
| 760 |
if ( ! empty( $attributes['placeholderBgColor'] ) ) { |
| 761 |
$instance['placeholder_bg_color'] = $attributes['placeholderBgColor']; |
| 762 |
} |
| 763 |
|
| 764 |
if ( ! empty( $attributes['placeholderTextColor'] ) ) { |
| 765 |
$instance['placeholder_text_color'] = $attributes['placeholderTextColor']; |
| 766 |
} |
| 767 |
|
| 768 |
if ( function_exists( 'sfp_render_page_plugin_html' ) ) { |
| 769 |
return sfp_render_page_plugin_html( $instance ); |
| 770 |
} |
| 771 |
|
| 772 |
return ''; |
| 773 |
} |
| 774 |
|
| 775 |
} // end SFPlugin class |
| 776 |
|
| 777 |
} // end if !class_exists |
| 778 |
|
| 779 |
// Create new SFPlugin instance |
| 780 |
$GLOBALS["sfplugin"] = new SFPlugin(); |
| 781 |
|
| 782 |
?> |
| 783 |
|