Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds Beaver Builder support. #33

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions assets/admin/js/bb-analysis.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
(function($){
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indentation is a bit off in this file, which makes it harder to read. It looks like most other JS files in this project use tabs.

var ClassicSEOIntegration = function() {
this.hooks()
}

ClassicSEOIntegration.prototype.hooks = function() {
classicSEOApp.registerPlugin( 'bb-seo' )
wp.hooks.addFilter( 'cpseo_content', 'bb-seo', function(content) {
return window.classicSEO.beaverbuilder.pagedata;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like window.classicSEO.beaverbuilder will be undefined if Beaver Builder is not loaded. Maybe this is not actually a problem though, since the script will only be registered if BB is loaded...

Copy link
Contributor

@timbocode timbocode Feb 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, the script won't load if BB is not installed and active.

} )
}

ClassicSEOIntegration.prototype.getContent = function( content ) {
return window.classicSEO.beaverbuilder.pagedata;
}

$( document ).ready( function () {
new ClassicSEOIntegration()
})
})(jQuery);
9 changes: 9 additions & 0 deletions includes/module/class-manager.php
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,15 @@ public function setup_3rd_party( $modules ) {
];
}

if ( class_exists( 'FLBuilder' ) ) {
$modules['beaverbuilder'] = array(
'title' => esc_html__( 'Beaver Builder', 'cpseo' ),
'desc' => esc_html__( 'Read and analyze content in Beaver Builder layouts.', 'cpseo' ),
'class' => 'Classic_SEO\BeaverBuilder\BeaverBuilder',
'icon' => 'dashicons-editor-table',
);
}

return $modules;
}

Expand Down
105 changes: 105 additions & 0 deletions includes/modules/beaverbuilder/class-beaverbuilder.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php
/**
* The BeaverBuilder Module
*
* @since 0.5.4
* @package Classic_SEO
* @subpackage Classic_SEO\modules
*/

namespace Classic_SEO\BeaverBuilder;

use Classic_SEO\Helper;
use Classic_SEO\Admin\Admin_Helper;
use Classic_SEO\Traits\Hooker;
use FLBuilderModel;

defined( 'ABSPATH' ) || exit;

/**
* BeaverBuilder class.
*/
class BeaverBuilder {
use Hooker;

/**
* The Constructor.
*/
public function __construct() {

add_filter( 'cpseo/sitemap/excluded_post_types', array( $this, 'types' ) );

if ( ! Admin_Helper::is_post_edit() ) {
return;
}
if ( ! isset( $_GET['post'] ) ) {
return false;
}

$id = $_GET['post'];

if ( ! get_post_meta( $id, '_fl_builder_enabled', true ) ) {
return false;
}

$this->action( 'cpseo/admin/enqueue_scripts', 'enqueue' );
$this->action( 'admin_enqueue_scripts', 'dequeue_layout_scripts', 10000 );
}

/**
* Enqueue styles and scripts for the metabox.
*/
public function enqueue() {
wp_enqueue_script( 'cpseo-bb-post-analysis', cpseo()->plugin_url() . 'assets/admin/js/bb-analysis.js', array( 'cpseo-post-metabox' ), cpseo()->version, true );
Helper::add_json( 'beaverbuilder', $this->get_config() );
}

/**
* Get Config data
*
* @return array The config data.
*/
private function get_config() {
$config = array(
'pluginName' => 'cpseo-beaverbuilder',
'pagedata' => $this->content_data(),
);

return $this->do_filter( 'beaverbuilder/config', $config );
}

/**
* Get Page Layout HTML
*/
private function content_data() {

$id = $_GET['post'];
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like it needs a permission / nonce check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For when this is revisited: using get_the_ID() might be a better option here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

get_the_ID() is a wp loop function. This is all happening in wp admin on a post/page edit screen.


ob_start();
echo do_shortcode( "[fl_builder_insert_layout id=$id]" );
$data = ob_get_clean();
FLBuilderModel::delete_all_asset_cache( $id );
return str_replace( PHP_EOL, '', $data );
}

/**
* Dequeue any JS enqueued during content_data() function to prevent JS errors.
*/
function dequeue_layout_scripts() {
global $wp_scripts;
foreach ( $wp_scripts->queue as $item ) {
if ( false !== strpos( $item, 'fl-builder-layout' ) ) {
wp_dequeue_script( $item );
}
}
}

/**
* Removes fl-builder-template from sitemap
*/
function types( $post_types ) {
unset( $post_types['fl-builder-template'] );
return $post_types;
}

}
1 change: 1 addition & 0 deletions vendor/composer/autoload_classmap.php
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
'Classic_SEO\\Admin\\Serp_Preview' => $baseDir . '/includes/admin/class-serp-preview.php',
'Classic_SEO\\Admin\\Watcher' => $baseDir . '/includes/admin/watcher/class-watcher.php',
'Classic_SEO\\Admin_Bar_Menu' => $baseDir . '/includes/admin/class-admin-bar-menu.php',
'Classic_SEO\\BeaverBuilder\\BeaverBuilder' => $baseDir . '/includes/modules/beaverbuilder/class-beaverbuilder.php',
'Classic_SEO\\CLI\\Commands' => $baseDir . '/includes/cli/class-commands.php',
'Classic_SEO\\CMB2' => $baseDir . '/includes/class-cmb2.php',
'Classic_SEO\\Common' => $baseDir . '/includes/class-common.php',
Expand Down
1 change: 1 addition & 0 deletions vendor/composer/autoload_static.php
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ class ComposerStaticInitc2822413d75ee1db8fcd50de895c43fa
'Classic_SEO\\Admin\\Serp_Preview' => __DIR__ . '/../..' . '/includes/admin/class-serp-preview.php',
'Classic_SEO\\Admin\\Watcher' => __DIR__ . '/../..' . '/includes/admin/watcher/class-watcher.php',
'Classic_SEO\\Admin_Bar_Menu' => __DIR__ . '/../..' . '/includes/admin/class-admin-bar-menu.php',
'Classic_SEO\\BeaverBuilder\\BeaverBuilder' => __DIR__ . '/../..' . '/includes/modules/beaverbuilder/class-beaverbuilder.php',
'Classic_SEO\\CLI\\Commands' => __DIR__ . '/../..' . '/includes/cli/class-commands.php',
'Classic_SEO\\CMB2' => __DIR__ . '/../..' . '/includes/class-cmb2.php',
'Classic_SEO\\Common' => __DIR__ . '/../..' . '/includes/class-common.php',
Expand Down