-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathblock-save.js
executable file
·95 lines (78 loc) · 2.18 KB
/
block-save.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import _cloneDeep from 'lodash/cloneDeep';
export class SaveBlock {
constructor() {
wp.data.subscribe( this.saveCallback.bind(this) );
this.lastIsSaving = false;
this.lastDataSent = false;
wp.hooks.addFilter( 'blocks.getSaveElement', 'gutes-array', this.getSaveCallback );
}
/**
*
* blocks.getSaveElement callback - store bid
*
* @param elem
* @param blockType
* @param attr
* @returns {*}
*/
getSaveCallback( elem, blockType, attr ) {
if ( ! attr.bid ) {
attr.bid = Math.floor((1 + Math.random()) * 0x10000).toString(16);
}
return elem;
}
/**
* Data Subscribe callback.
* Clones blocks, runs through filter(s), and saves to custom DB table.
*
*/
saveCallback() {
const editor = wp.data.select( 'core/editor' );
const blocks = editor.getBlocks();
let blocks_dup = _cloneDeep( blocks );
const isSaving = editor.isSavingPost();
const postId = editor.getCurrentPostId();
if ( isSaving && ! this.lastIsSaving ) {
blocks_dup = SaveBlock.cleanData( blocks_dup );
// do not save - same data.
if ( blocks_dup === this.lastDataSent ) {
return;
}
this.lastDataSent = blocks_dup;
// Save using gutes-db endpoint.
wp.apiRequest( {
path: '/gutes-db/v1/' + postId,
data: {
post_id: postId,
gutes_data: blocks_dup.length ? JSON.stringify( blocks_dup ) : false
},
method: 'POST',
dataType: 'json',
} ).then(function(res){
console.log( res );
}, function(err) {
console.log( err );
});
}
this.lastIsSaving = isSaving;
}
/**
*
* Clean the data - allows for filters to be added so you can modify the data being saved.
*
* @param blocks
* @returns {Array}
*/
static cleanData( blocks ) {
let newBlocks = [];
for ( let block of blocks ) {
let blockName = block.name.replace( '/', '-' );
newBlocks.push({
uid: block.uid || block.clientId,
name: block.name,
data: wp.hooks.applyFilters( `clean_data_${blockName}`, block.attributes, block.name, block.innerBlocks )
})
}
return newBlocks;
}
}