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

50558 Fix Shortcode Recognition for Detail Pages in WP Bakery Builder #864

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
34 changes: 33 additions & 1 deletion plugin/Controller/DetailViewPostSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,13 @@ public function onSavePost($postId) {
$viewContainedAddressDetailCustomField = true;
}
}
$containsDetailViewInWPBakeryBuilder = false;

if (($viewContained) || ($viewContainedCustomField && $viewContained) || ($viewContainedCustomField && $hasOtherShortcodeInPostContent == false)) {
if (in_array( 'js_composer/js_composer.php', get_option("active_plugins") )) {
$containsDetailViewInWPBakeryBuilder = $this->containsDetailViewInWPBakeryBuilder($postContent, $detailViewName);
}

if (($viewContained) || ($viewContainedCustomField && $viewContained) || ($viewContainedCustomField && $hasOtherShortcodeInPostContent == false) || $containsDetailViewInWPBakeryBuilder) {
if ($postType == 'page') {
$pDetailView->setPageId((int) $postId);
$pDetailView->addToPageIdsHaveDetailShortCode( (int) $postId );
Expand Down Expand Up @@ -585,4 +590,31 @@ private function deletePageShortCode( $listView, $post, $tableName, $column, $pr

}
}

/**
* @param string $postContent
* @param string $detailViewName
* @return bool
*/

private function containsDetailViewInWPBakeryBuilder(string $postContent, string $detailViewName): bool
{
$matches = array();
$regex = get_shortcode_regex(array('vc_raw_html'));
preg_match_all('/' . $regex . '/ism', $postContent, $matches);

if (!array_key_exists(5, $matches)) {
return false;
}

foreach ($matches[5] as $encodedContent) {
$decodedContent = rawurldecode(base64_decode($encodedContent));
if ($this->postContainsViewName($decodedContent, $detailViewName)) {
return true;
}
}

return false;
}
}

42 changes: 42 additions & 0 deletions tests/TestClassDetailViewPostSaveController.php
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,48 @@ public function testShortCodeInMetaKey()
}


/**
*
*/

public function testShortCodeDetailViewInWPBakeryBuilder()
{
$this->run_activate_plugin_for_test('js_composer/js_composer.php');
$this->_pDataDetailView->setPageId( 13 );
$this->_pDataDetailViewHandler->saveDetailView( $this->_pDataDetailView );

$pWPPost = self::factory()->post->create_and_get( [
'post_author' => 1,
'post_content' => '[vc_raw_html css=""]JTVCb29fZXN0YXRlJTIwdmlldyUzRCUyMmRldGFpbCUyMiU1RA==[/vc_raw_html]',
'post_title' => 'Test Post',
'post_type' => 'page',
] );

$this->_pDetailViewPostSaveController->onSavePost( $pWPPost->ID );
$detailViewOptions = get_option( DataDetailViewHandler::DEFAULT_VIEW_OPTION_KEY );
$this->assertEquals( $pWPPost->ID, $detailViewOptions->getPageId() );
}

/**
* @param $plugin
* @return null
*/
private function run_activate_plugin_for_test($plugin) {
$current = get_option('active_plugins');
$plugin = plugin_basename(trim($plugin));

if (!in_array($plugin, $current)) {
$current[] = $plugin;
sort($current);
do_action('activate_plugin', trim($plugin));
update_option('active_plugins', $current);
do_action('activate_' . trim($plugin));
do_action('activated_plugin', trim($plugin));
}

return null;
}

/**
*
*/
Expand Down
Loading