Skip to content

Instantly share code, notes, and snippets.

@atwellpub
Last active July 29, 2019 23:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save atwellpub/e77d450ce6ca498ee3e6e9589e3bad90 to your computer and use it in GitHub Desktop.
Save atwellpub/e77d450ce6ca498ee3e6e9589e3bad90 to your computer and use it in GitHub Desktop.
An attempt to integrate Landing Pages by Inbound Now with the Cornerstone Page Builder. Landing Pages stores field variation data in separate meta keys, usually in the following format: `key-name` for variation A, `key-name-1' for variation B, `key-name-2` for C, etc. Notice how A does not have an appended suffix. The challenge for these page bu…
<?php
/*
Plugin Name: Inbound Extension - Cornerstone Builder
Plugin URI: http://www.inboundnow.com/
Description: Adds landing pages support to the cornerstone builder
Version: 1.0.8
Author: Inbound Now
Contributors: Hudson Atwell
Author URI: http://www.inboundnow.com/
*/
if (!class_exists('Inbound_CornerStone_Builder')) {
class Inbound_CornerStone_Builder {
static $map;
static $settings;
/**
* Initialize class
*/
public function __construct() {
self::define_constants();
self::load_hooks();
}
/**
* Define constants
*/
public static function define_constants() {
define('INBOUND_CORNERSTONE_BUILDER_CURRENT_VERSION', '1.0.8');
define('INBOUND_CORNERSTONE_BUILDER_LABEL', __('Cornerstone Builder Integration', 'inbound-pro'));
define('INBOUND_CORNERSTONE_BUILDER_SLUG', 'inbound-cornerstone-builder');
define('INBOUND_CORNERSTONE_BUILDER_FILE', __FILE__);
define('INBOUND_CORNERSTONE_BUILDER_REMOTE_ITEM_NAME', 'cornerstone-page-builder-integration');
define('INBOUND_CORNERSTONE_BUILDER_PATH', realpath(dirname(__FILE__)) . '/');
$upload_dir = wp_upload_dir();
$url = (!strstr(INBOUND_CORNERSTONE_BUILDER_PATH, 'plugins')) ? $upload_dir['baseurl'] . '/inbound-pro/extensions/' . plugin_basename(basename(__DIR__)) . '/' : WP_PLUGIN_URL . '/' . plugin_basename(dirname(__FILE__)) . '/';
define('INBOUND_CORNERSTONE_BUILDER_URLPATH', $url);
define('LANDING_PAGES_WPAUTOP', false);
}
/**
* Load Hooks & Filters
*/
public static function load_hooks() {
add_filter( 'the_content' , array( __CLASS__ , 'readd_cornerstone_container' ) , 100 , 1 );
add_action( 'cornerstone_after_save_content', array( __CLASS__ , 'lp_cornerstone_save_content' ), 1000 , 4 );
add_filter( 'get_post_metadata', array( __CLASS__ , 'lp_cornerstone_load_content' ) , 100, 4 );
add_action( 'admin_init' , array( __CLASS__ , 'lp_cornerstone_open_variation') , 1 );
add_action( 'cornerstone_before_boot_app' , array( __CLASS__ , 'lp_cornerstone_add_variation_id_to_url' ) , 10 );
add_action( 'cornerstone_before_load_preview' , array( __CLASS__ , 'load_correct_variation' ) , 10 );
add_action( 'cs_before_preview_frame' , array( __CLASS__ , 'load_correct_variation' ) , 10 );
}
/**
* loads the correct variation into the preview page
*/
public static function load_correct_variation() {
global $current_variation_id;
$current_variation_id = $_COOKIE['cornerstone_loaded_variation'];
//I had to pause building this feature.
}
/**
*
*/
public static function lp_cornerstone_open_variation() {
if (!isset($_GET['post']) ) {
return;
}
$post_type = get_post_type($_GET['post']);
if ($post_type != 'landing-page') {
return;
}
$vid = (isset($_GET['lp-variation-id'])) ? $_GET['lp-variation-id'] : 0;
setcookie('cornerstone_loaded_variation', $vid , time() + 3600, "/");
}
/**
* @param $meta_value
* @param $post_id
* @param $meta_key
* @param $single
* @return mixed
*/
public function lp_cornerstone_load_content($meta_value, $post_id, $meta_key, $single){
if ( '_cornerstone_data' != $meta_key ) {
return $meta_value;
}
$post = get_post($post_id);
if ($post->post_type!='landing-page') {
return $meta_value;
}
remove_filter( 'get_post_metadata', array( __CLASS__ , 'lp_cornerstone_load_content') , 100 , 4);
if (isset($_REQUEST['cs_preview_state'])) {
$variation_id = ( isset($_COOKIE['cornerstone_loaded_variation']) && $_COOKIE['cornerstone_loaded_variation'] ) ? $_COOKIE['cornerstone_loaded_variation'] : 0;
} else {
$variation_id = Landing_Pages_Variations::get_current_variation_id();
}
$meta_value = get_post_meta( $post_id, '_cornerstone_data' , TRUE );
$cornerstone_variation_data = get_post_meta( $post_id, '_cornerstone_data-'.$variation_id , TRUE );
if ($cornerstone_variation_data) {
//return $meta_value;
return $cornerstone_variation_data;
} else {
return $meta_value;
}
}
function lp_cornerstone_save_content( $post_id ) {
$post = get_post($post_id);
if ( $post->post_type !='landing-page' || wp_is_post_revision( $post_id ) ) {
return;
}
if (defined('DOING_AUTOSAVE') && DOING_AUTOSAVE ) {
return;
}
$variation_id = ( isset($_COOKIE['cornerstone_loaded_variation']) && $_COOKIE['cornerstone_loaded_variation'] ) ? $_COOKIE['cornerstone_loaded_variation'] : 0;
$post_content = $post->post_content;
$cornerstone_data = get_post_meta( $post_id , '_cornerstone_data' ,true);
if ( $variation_id > 0 ) {
$content_key = 'content' . '-' . $variation_id;
} else {
$content_key = 'content';
}
update_post_meta( $post_id , $content_key , $post_content );
update_post_meta( $post_id , '_cornerstone_data-' .$variation_id , addslashes($cornerstone_data) );
}
/**
*
*/
public static function lp_cornerstone_add_variation_id_to_url() {
global $current_variation_id;
$variation_id = (INT) $_COOKIE['cornerstone_loaded_variation'];
$current_variation_id = $variation_id;
return;
?>
<script>
var current_url = window.location.href.replace('/lp-variation-id=(.?*)/' , '');
if (window.location.href.indexOf('/vid-<?php echo $current_variation_id; ?>') === -1 ) {
var split = window.location.href.split('#');
var pageUrl = split[0] + 'vid-<?php echo $current_variation_id; ?>/#' + split[1];
setTimeout(function() {
window.history.pushState('', '', pageUrl);
} , 400);
}
</script>
<?php
}
/**
* Add containers to content to support preview mode
* @param $content
* @return string
*/
public static function readd_cornerstone_container($content) {
global $post;
if ($post->post_type != 'landing-page') {
return $content;
}
if (!isset($_REQUEST['cs_preview_state'])) {
return $content;
}
$variation_id = (INT) $_COOKIE['cornerstone_loaded_variation'];
$notice = '<blockquote class="notice">You are editing landing page variation '.$variation_id.'</i>'.'<br>'.' <a href="'.get_edit_post_link($post->ID).'">Return to landing page edit screen</a></blockquote>';
$content = '<div id="cs-content" class="cs-content"><div data-cs-zone="cs_content"></div></div>';
return $content . $notice;
}
}
new Inbound_CornerStone_Builder();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment