Skip to content

Commit

Permalink
Merge pull request #22 from badabingbreda/full-autoloader
Browse files Browse the repository at this point in the history
  • Loading branch information
zackpyle authored Aug 25, 2023
2 parents 22d7c94 + 05f6299 commit 49650e5
Show file tree
Hide file tree
Showing 13 changed files with 807 additions and 681 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,13 @@ The last and what I think is the **best** feature is you also have the ability t

## Changelog

*v1.0.3 - 8/24/23*
- Split up the plugin in functional classes and made it namespaced
- An autoloader takes care of loading/include_once/require_once the files
- Load admin script on fl-builder-settings page
- Fixed fatal error on plugins page (issue #19) - Updater fixed with wp_tempnam and unlink method
- Fixed reorder class bug (issue #21) - Couldn't reorder classes on new groups before hitting save

*v1.0.2 - 8/21/23*
- Fix error when opening other settings panels
- Moved all functions to BBClassDropdown
Expand Down
225 changes: 36 additions & 189 deletions bb-class-dropdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,200 +10,47 @@
* Plugin Name: Beaver Builder Class Dropdown
* Plugin URI: https://github.com/zackpyle/BBClassDropdown
* Description: BB Class Dropdown adds user defined CSS classes to dropdown below the Beaver Builder class input in the Advanced tab
* Version: 1.0.2
* Version: 1.0.3
* Author: PYLE/DIGITAL
* Author URI: https://github.com/zackpyle
* Text Domain: BBClassDropdown
* License: GPL-3.0+
* License URI: https://www.gnu.org/licenses/gpl-3.0.txt
*/


define( 'BBCLASSDROPDOWN_VERSION', '1.0.2' );
define( 'BBCLASSDROPDOWN_DIR', plugin_dir_path( __FILE__ ) );
define( 'BBCLASSDROPDOWN_FILE', __FILE__ );
define( 'BBCLASSDROPDOWN_URL', plugins_url( '/', __FILE__ ) );

// Include Updater files
require_once(BBCLASSDROPDOWN_DIR . 'includes/updater/Autoloader.php');
require_once(BBCLASSDROPDOWN_DIR . 'includes/updater/GithubUpdater.php');
require_once(BBCLASSDROPDOWN_DIR . 'includes/updater/Init.php');

// Include plugin files
require_once( BBCLASSDROPDOWN_DIR . 'includes/bb-class-dropdown-functions.php');
require_once( BBCLASSDROPDOWN_DIR . 'includes/bb-class-dropdown-admin.php');

register_activation_hook( BBCLASSDROPDOWN_FILE, 'beaver_builder_class_dropdown_plugin_activate' );

// Initialize updater
new BBClassDropdown\Includes\Updater\Init();

add_action( 'init', 'maybe_load_scripts' );
add_action( 'init', 'clear_bb_class_options' );

/**
* maybe_load_scripts
*
* @return void
*/
function maybe_load_scripts() {

// no need to load this if BB isn't even available
if ( !class_exists( 'FLBuilderModel' ) ) return;

add_action( 'admin_enqueue_scripts', 'beaver_builder_class_dropdown_enqueue_scripts' );
add_action( 'wp_enqueue_scripts', 'bb_class_frontend_scripts' );
add_action( 'wp_enqueue_scripts', 'bb_class_frontend_select2' );

// Enqueue jQuery UI for admin area
add_action('admin_enqueue_scripts', 'enqueue_jquery_ui_sortable');

// Add admin settings tab to Beaver Builder
add_action('fl_builder_admin_settings_nav_items', 'bb_class_dropdown_menu_item');
add_action('fl_builder_admin_settings_render_forms', 'bb_class_dropdown_add_settings_form');
add_action('fl_builder_admin_settings_save', 'bb_class_dropdown_admin_settings_save');
}


/**
* beaver_builder_class_dropdown_enqueue_scripts
*
* Enqueue scripts for dashboard
*
* @return void
*/
function beaver_builder_class_dropdown_enqueue_scripts() {

// no need to load this if BB isn't available
if ( !class_exists( 'FLBuilderModel' ) ) return;

wp_enqueue_script(
'bb-class-dropdown-scripts',
BBCLASSDROPDOWN_URL . 'includes/js/bb-class-dropdown-admin-scripts.js',
array( 'jquery' ),
BBCLASSDROPDOWN_VERSION,
true
);
}

/**
* bb_class_frontend_scripts
*
* Enqueue scripts when editing BB layout/page
*
* @return void
*/
function bb_class_frontend_scripts() {

// no need to load this if BB isn't available or not in builder
if ( !class_exists( 'FLBuilderModel' ) || !\FLBuilderModel::is_builder_active() ) return;

// Custom JS
wp_enqueue_script(
'bb-class-dropdown-frontend-script',
BBCLASSDROPDOWN_URL . 'includes/js/bb-class-dropdown-frontend-script.js',
array( 'jquery' ), BBCLASSDROPDOWN_VERSION, true
);
// get options for localization
$options = get_option( 'beaver_builder_class_dropdown_options' , [] );
wp_localize_script(
'bb-class-dropdown-frontend-script',
'BBClassOptions' ,
array( "options" => $options )
);
}

/**
* bb_class_frontend_select2
*
* @return void
*/
function bb_class_frontend_select2() {

// no need to load this if BB isn't available or not in builder
if ( !class_exists( 'FLBuilderModel' ) || !\FLBuilderModel::is_builder_active() ) return;

// return early if select2 isn't loaded
if ( !apply_filters( 'fl_select2_enabled' , true ) ) return;

// Custom JS
wp_enqueue_script(
'bb-class-dropdown-select2',
BBCLASSDROPDOWN_URL . 'includes/js/bb-class-dropdown-select2.js',
array( 'jquery', 'select2' ),
BBCLASSDROPDOWN_VERSION,
true
);
}

function enqueue_jquery_ui_sortable() {
// Enqueue jQuery UI core
wp_enqueue_script('jquery-ui-core');

// Enqueue jQuery UI sortable module
wp_enqueue_script('jquery-ui-sortable');
}

/**
* beaver_builder_class_dropdown_activate
*
* @return void
*/
function beaver_builder_class_dropdown_plugin_activate() {
// Check if Beaver Builder is active
if ( ! class_exists( 'FLBuilder' ) ) {
deactivate_plugins( plugin_basename( __FILE__ ) );
wp_die( esc_html__( 'Sorry, but this plugin requires Beaver Builder.', 'BBClassDropdown' ) );

use BBClassDropdown\Autoloader;
use BBClassDropdown\Init;

if ( defined( 'ABSPATH' ) && ! defined( 'BBCLASSDROPDOWN_VERION' ) ) {
register_activation_hook( __FILE__, 'BBCLASSDROPDOWN_check_php_version' );

/**
* Display notice for old PHP version.
*/
function BBCLASSDROPDOWN_check_php_version() {
if ( version_compare( phpversion(), '7.4', '<' ) ) {
die( esc_html__( 'BB Class Dropdown requires PHP version 7.4+. Please contact your host to upgrade.', 'BBClassDropdown' ) );
}
}

// Flush rewrite rules
//flush_rewrite_rules();
}


/**
* clear_bb_class_options
*
* Clear all existing classes by navigating to /?clear_bb_class_options=1
*
* @return void
*/
function clear_bb_class_options() {
if (isset($_GET['clear_bb_class_options'])) {
delete_option('beaver_builder_class_dropdown_options');
add_action('admin_notices', 'bb_class_options_reset_notice');
}
}
function bb_class_options_reset_notice() {
echo '<div class="notice notice-success is-dismissible updated"><p>' . esc_html__('Classes reset!', 'BBClassDropdown') . '</p></div>';
}

/**
* bb_class_dropdown_menu_item
*
* Add new tab in the bb settings menu
*
* @param mixed $navitems
* @return void
*/
function bb_class_dropdown_menu_item($navitems)
{
$navitems['class-dropdown'] = array(
'title'=> 'Predefined Classes',
'show' => true,
'priority' => 695
);
return $navitems;
}

/**
* bb_class_dropdown_add_settings_form
*
* output for our BB settings tab
*
* @return void
*/
function bb_class_dropdown_add_settings_form(){
// function found in includes/bb-class-dropdown-functions.php
beaver_builder_class_dropdown_settings_page_html();

define( 'BBCLASSDROPDOWN_VERSION' , '1.0.3' );
define( 'BBCLASSDROPDOWN_DIR' , plugin_dir_path( __FILE__ ) );
define( 'BBCLASSDROPDOWN_BASE' , plugin_basename( __FILE__ ) );
define( 'BBCLASSDROPDOWN_FILE' , __FILE__ );
define( 'BBCLASSDROPDOWN_URL' , plugins_url( '/', __FILE__ ) );

define( 'CHECK_BBCLASSDROPDOWN_PLUGIN_FILE', __FILE__ );

}

if ( ! class_exists( 'BBClassDropdown\Init' ) ) {

/**
* The file where the Autoloader class is defined.
*/
require_once 'includes/classes/Autoloader.php';
spl_autoload_register( array( new Autoloader(), 'autoload' ) );

$plugin_var = new Init();

}
Loading

0 comments on commit 49650e5

Please sign in to comment.