Skip to content

Commit

Permalink
Re-Usable Block issue fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
anik committed Nov 19, 2019
1 parent 13322de commit f4137ec
Show file tree
Hide file tree
Showing 2 changed files with 160 additions and 5 deletions.
48 changes: 45 additions & 3 deletions assets/reactjs/src/helpers/ParseCss.js
Original file line number Diff line number Diff line change
Expand Up @@ -100,18 +100,60 @@ function isQubelyBlock(blocks){
}


function getData(pId) {
wp.apiFetch({
path: 'qubely/v1/qubely_get_content',
method: 'POST',
data: { postId: pId }
}).then( response => {
if (response.success) {
const innerBlock = innerBlocks(wp.blocks.parse(response.data), true)
if(innerBlock.css){
wp.apiFetch({
path: 'qubely/v1/append_qubely_css',
method: 'POST',
data: { css: innerBlock.css, post_id: select('core/editor').getCurrentPostId() }
}).then( res => {
if (res.success) {
// Save Data
}
})
}
}
})
};


function parseBlock(blocks){
blocks.forEach( block => {
if (block.name.indexOf('core/block')!= -1) {
getData(block.attributes.ref)
}
if (block.innerBlocks && (block.innerBlocks).length > 0) {
parseBlock(block.innerBlocks)
}
})
}


const ParseCss = (setDatabase = true) => {
window.bindCss = true
const { getBlocks } = select('core/block-editor')
const isRemain = isQubelyBlock(getBlocks())
const all_blocks = select('core/block-editor').getBlocks()
const isRemain = isQubelyBlock(all_blocks)
const { getCurrentPostId } = select('core/editor')
let __blocks = { css: '', interaction: {} };
if (typeof window.globalData != 'undefined') {
__blocks.css += CssGenerator(window.globalData.settings, 'pagesettings', '8282882', true)
}
let parseData = innerBlocks(getBlocks(), true)

// Inner Blocks
let parseData = innerBlocks(all_blocks, true)
__blocks.interaction = parseData.interaction
__blocks.css += parseData.css

// reusable Block
parseBlock(all_blocks);

localStorage.setItem('qubelyCSS', __blocks)
if (setDatabase) {
API_fetch(getCurrentPostId(), __blocks, isRemain ).then(data => { })
Expand Down
117 changes: 115 additions & 2 deletions core/QUBELY.php
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,79 @@ public function register_api_hook()
)
)
);
// Get the Content by ID
register_rest_route(
'qubely/v1',
'/qubely_get_content/',
array(
array(
'methods' => 'POST',
'callback' => array($this, 'qubely_get_content'),
'permission_callback' => function () {
return current_user_can('edit_posts');
},
'args' => array()
)
)
);
// Append Qubely CSS
register_rest_route(
'qubely/v1',
'/append_qubely_css/',
array(
array(
'methods' => 'POST',
'callback' => array($this, 'append_qubely_css_callback'),
'permission_callback' => function () {
return current_user_can('edit_posts');
},
'args' => array()
)
)
);

}


public function append_qubely_css_callback($request)
{
try {
global $wp_filesystem;
if (!$wp_filesystem) {
require_once(ABSPATH . 'wp-admin/includes/file.php');
}
$params = $request->get_params();
$css = $params['css'];
$post_id = (int) sanitize_text_field($params['post_id']);
if( $post_id ){
$filename = "qubely-css-{$post_id}.css";
$upload_dir = wp_upload_dir();
$dir = trailingslashit($upload_dir['basedir']) . 'qubely/';
if(file_exists($dir.$filename)) {
$file = fopen($dir.$filename, "a");
fwrite($file, $css);
fclose($file);
}
$get_data = get_post_meta($post_id, '_qubely_css', true);
update_post_meta($post_id, '_qubely_css', $get_data.$css);

wp_send_json_success(['success' => true, 'message' => 'Update done'.$get_data]);
}
} catch (Exception $e) {
wp_send_json_error(['success' => false, 'message' => $e->getMessage()]);
}
}

public function qubely_get_content($request)
{
$params = $request->get_params();
try{
if (isset($params['postId'])) {
return ['success' => true, 'data'=> get_post($params['postId'])->post_content, 'message' => 'Get Data Success!!'];
}
} catch (Exception $e){
return ['success' => false, 'message' => $e->getMessage()];
}
}

/**
Expand All @@ -365,7 +438,6 @@ public function register_api_hook()
*/
public function update_global_option($request)
{

try {
$params = $request->get_params();
if (!isset($params['settings']))
Expand Down Expand Up @@ -564,23 +636,64 @@ public function enqueue_block_css()
// }
}

/**
*
* Return reference id
*
* @since 1.2.5
* @return bool
*/
public function reference_id($parse_blocks) {
$extra_id = array();
if (!empty($parse_blocks)) {
foreach ($parse_blocks as $key => $block) {
if( $block['blockName'] == 'core/block') {
$extra_id[] = $block['attrs']['ref'];
}
if (count($block['innerBlocks']) > 0) {
$extra_id = array_merge( $this->reference_id($block['innerBlocks']), $extra_id );
}
}
}
return $extra_id;
}


/**
* Enqueue block css file
* Check if css path exists and it has current post page
* Then enqueue file
* Then enqueue file
*/
public function enqueue_block_css_file()
{
$post_id = $this->is_qubely_single();
$upload_dir = wp_get_upload_dir();
$upload_css_dir = trailingslashit($upload_dir['basedir']);
$css_path = $upload_css_dir . "qubely/qubely-css-{$post_id}.css";

$content_post = get_post($post_id);
$content = $content_post->post_content;
$parse_blocks = parse_blocks($content);
$css_id = $this->reference_id($parse_blocks);

if (file_exists($css_path)) {
$css_dir_url = trailingslashit($upload_dir['baseurl']);
$css_url = $css_dir_url . "qubely/qubely-css-{$post_id}.css";
if (!$this->is_editor_screen()) {
wp_enqueue_style("qubely-post-{$post_id}", $css_url, false, QUBELY_VERSION);
}
// Reusable Blocks CSS add
if (is_array($css_id)) {
if (!empty($css_id)) {
$css_id = array_unique($css_id);
foreach ($css_id as $value) {
$css = $upload_css_dir . "qubely/qubely-css-{$value}.css";
if (file_exists($upload_css_dir . "qubely/qubely-css-{$value}.css")) {
wp_enqueue_style("qubely-post-{$value}", trailingslashit($upload_dir['baseurl'])."qubely/qubely-css-{$value}.css", false, QUBELY_VERSION);
}
}
}
}
} else {
wp_register_style('qubely-post-data', false);
wp_enqueue_style('qubely-post-data');
Expand Down

0 comments on commit f4137ec

Please sign in to comment.