Created
February 20, 2019 19:02
-
-
Save n7studios/17bc765cdd4828581c896519fdcdebd0 to your computer and use it in GitHub Desktop.
Page Generator Pro: TablePress Example Integration
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Plugin Name: Page Generator Pro: TablePress Example Integration | |
* Plugin URI: http://www.wpzinc.com/plugins/page-generator-pro | |
* Version: 0.0.1 | |
* Author: WP Zinc | |
* Author URI: http://www.wpzinc.com | |
* Description: Example integration of how keywords within a third party Plugin, that uses a shortcode within a Page Generator Pro 1.9.8+ Content Group can be processed. | |
*/ | |
class Page_Generator_Pro_TablePress { | |
/** | |
* Constructor | |
* | |
* @since 0.0.1 | |
*/ | |
public function __construct() { | |
// Register TablePress' shortcode to run their do_shortcode() actions when Pages are generated from Page Generator Pro | |
// @see https://www.wpzinc.com/documentation/page-generator-pro/developers/#page_generator_pro_shortcode_add_shortcodes | |
add_action( 'page_generator_pro_shortcode_add_shortcodes', array( $this, 'add_shortcodes' ), 10, 1 ); | |
// Replace keywords in the generated TablePress HTML from above with Terms | |
add_filter( 'page_generator_pro_generate_post_args', array( $this, 'replace_keywords_with_terms_in_content' ), 10, 2 ); | |
} | |
/** | |
* Register TablePress' shortcode to run their do_shortcode() actions when Pages are generated from Page Generator Pro | |
* | |
* @since 0.0.1 | |
* | |
* @param bool $generating_group Generating Group | |
*/ | |
public function add_shortcodes( $generating_group = false ) { | |
// If we're not generating pages from a group, we do not need to do anything | |
if ( ! $generating_group ) { | |
return; | |
} | |
// Load TablePress Frontend Controller | |
$controller = TablePress::load_controller( 'frontend' ); | |
// Add Shortcodes | |
add_shortcode( TablePress::$shortcode, array( $controller, 'shortcode_table' ) ); | |
add_shortcode( TablePress::$shortcode_info, array( $controller, 'shortcode_table_info' ) ); | |
} | |
/** | |
* Just before a Page is created, re-run replacing keywords with terms. | |
* | |
* This has to be done, because the Generation process will have: | |
* 1. Replaced keywords with terms, | |
* 2. Processed registered shortcodes | |
* | |
* This ensures that e.g. a Page Generator Pro shortcode with an argument that is | |
* a keyword is correctly replaced with a term prior to processing. | |
* | |
* Shortcodes, which in themselves contain keywords not specified as shortcode arguments (but rather contain | |
* keywords within their processed content state), such as TablePress, will need to have those keywords | |
* subsequently replaced with terms prior to the Page being created. | |
* | |
* @since 0.0.1 | |
* | |
* @param array $post_args wp_insert_post() / wp_update_post() compatible arguments | |
* @param string $settings Group Settings | |
* @return array wp_insert_post() / wp_update_post() compatible arguments | |
*/ | |
public function replace_keywords_with_terms_in_content( $post_args, $settings ) { | |
// For performance, build an array comprising of only the Group Settings Fields | |
// that we know need keyword replacements performing on them. | |
$parse = array( | |
'post_content' => $post_args['post_content'], | |
); | |
// Parse the array, replacing keywords with terms | |
$parse = Page_Generator_Pro()->get_class( 'generate' )->replace_keywords( $parse ); | |
// Inject the resuting content into the Post Arguments | |
$post_args['post_content'] = $parse['post_content']; | |
// Return Post Args | |
return $post_args; | |
} | |
} | |
// Initialize class | |
$page_generator_pro_tablepress = new Page_Generator_Pro_TablePress; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment