Skip to content

Commit 741d215

Browse files
committed
Merge branch 'feature/add-shortcode-column'
2 parents 73a2e12 + dcca444 commit 741d215

File tree

11 files changed

+7781
-13
lines changed

11 files changed

+7781
-13
lines changed

build/dlx-pw-preview.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-i18n', 'wp-plugins'), 'version' => 'e8c6101578cbc0285708');
1+
<?php return array('dependencies' => array('react', 'wp-i18n', 'wp-plugins'), 'version' => 'c30059576042eea38880');

build/dlx-pw-preview.js

Lines changed: 178 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,178 @@
1-
(()=>{"use strict";const e=window.React,t=window.wp.i18n;(0,window.wp.plugins.registerPlugin)("dlx-pattern-wrangler-preview-button",{render:function(){return(0,e.useEffect)((function(){var e=document.querySelector(".edit-post-header");if(null!==e){var r=e.querySelector(".edit-post-header__settings");if(null!==r){var n=document.createElement("a");n.className="dlx-button-preview components-button has-icon",n.ariaLabel=(0,t.__)("Preview","dlx-pattern-wrangler"),n.href=dlxPatternWranglerPreview.previewUrl,n.target="_blank",n.rel="noopener noreferrer";var a=document.createElement("svg");a.className="dlx-pattern-wrangler-preview-icon",a.innerHTML='<svg width="16" height="16" style="display: inline-block; margin-right: 8px;"><use xlink:href="#dlx-pattern-wrangler-preview-icon" /></svg>',n.prepend(a),r.prepend(n)}}}),[]),React.createElement(React.Fragment,null,React.createElement("svg",{height:"0",width:"0",xmlns:"http://www.w3.org/2000/svg",style:{display:"none"},"aria-hidden":"true"},React.createElement("symbol",{id:"dlx-pattern-wrangler-preview-icon",width:"16",height:"16",viewBox:"0 0 512 512"},React.createElement("path",{fill:"currentColor",d:"M304 24c0 13.3 10.7 24 24 24h102.1L207 271c-9.4 9.4-9.4 24.6 0 33.9s24.6 9.4 33.9 0l223-223V184c0 13.3 10.7 24 24 24s24-10.7 24-24V24c0-13.3-10.7-24-24-24H328c-13.3 0-24 10.7-24 24zM72 32C32.2 32 0 64.2 0 104v336c0 39.8 32.2 72 72 72h336c39.8 0 72-32.2 72-72V312c0-13.3-10.7-24-24-24s-24 10.7-24 24v128c0 13.3-10.7 24-24 24H72c-13.3 0-24-10.7-24-24V104c0-13.3 10.7-24 24-24h128c13.3 0 24-10.7 24-24s-10.7-24-24-24H72z"}))))}})})();
2-
//# sourceMappingURL=dlx-pw-preview.js.map
1+
/******/ (() => { // webpackBootstrap
2+
/******/ "use strict";
3+
/******/ var __webpack_modules__ = ({
4+
5+
/***/ "react":
6+
/*!************************!*\
7+
!*** external "React" ***!
8+
\************************/
9+
/***/ ((module) => {
10+
11+
module.exports = window["React"];
12+
13+
/***/ }),
14+
15+
/***/ "@wordpress/i18n":
16+
/*!******************************!*\
17+
!*** external ["wp","i18n"] ***!
18+
\******************************/
19+
/***/ ((module) => {
20+
21+
module.exports = window["wp"]["i18n"];
22+
23+
/***/ }),
24+
25+
/***/ "@wordpress/plugins":
26+
/*!*********************************!*\
27+
!*** external ["wp","plugins"] ***!
28+
\*********************************/
29+
/***/ ((module) => {
30+
31+
module.exports = window["wp"]["plugins"];
32+
33+
/***/ })
34+
35+
/******/ });
36+
/************************************************************************/
37+
/******/ // The module cache
38+
/******/ var __webpack_module_cache__ = {};
39+
/******/
40+
/******/ // The require function
41+
/******/ function __webpack_require__(moduleId) {
42+
/******/ // Check if module is in cache
43+
/******/ var cachedModule = __webpack_module_cache__[moduleId];
44+
/******/ if (cachedModule !== undefined) {
45+
/******/ return cachedModule.exports;
46+
/******/ }
47+
/******/ // Create a new module (and put it into the cache)
48+
/******/ var module = __webpack_module_cache__[moduleId] = {
49+
/******/ // no module.id needed
50+
/******/ // no module.loaded needed
51+
/******/ exports: {}
52+
/******/ };
53+
/******/
54+
/******/ // Execute the module function
55+
/******/ __webpack_modules__[moduleId](module, module.exports, __webpack_require__);
56+
/******/
57+
/******/ // Return the exports of the module
58+
/******/ return module.exports;
59+
/******/ }
60+
/******/
61+
/************************************************************************/
62+
/******/ /* webpack/runtime/compat get default export */
63+
/******/ (() => {
64+
/******/ // getDefaultExport function for compatibility with non-harmony modules
65+
/******/ __webpack_require__.n = (module) => {
66+
/******/ var getter = module && module.__esModule ?
67+
/******/ () => (module['default']) :
68+
/******/ () => (module);
69+
/******/ __webpack_require__.d(getter, { a: getter });
70+
/******/ return getter;
71+
/******/ };
72+
/******/ })();
73+
/******/
74+
/******/ /* webpack/runtime/define property getters */
75+
/******/ (() => {
76+
/******/ // define getter functions for harmony exports
77+
/******/ __webpack_require__.d = (exports, definition) => {
78+
/******/ for(var key in definition) {
79+
/******/ if(__webpack_require__.o(definition, key) && !__webpack_require__.o(exports, key)) {
80+
/******/ Object.defineProperty(exports, key, { enumerable: true, get: definition[key] });
81+
/******/ }
82+
/******/ }
83+
/******/ };
84+
/******/ })();
85+
/******/
86+
/******/ /* webpack/runtime/hasOwnProperty shorthand */
87+
/******/ (() => {
88+
/******/ __webpack_require__.o = (obj, prop) => (Object.prototype.hasOwnProperty.call(obj, prop))
89+
/******/ })();
90+
/******/
91+
/******/ /* webpack/runtime/make namespace object */
92+
/******/ (() => {
93+
/******/ // define __esModule on exports
94+
/******/ __webpack_require__.r = (exports) => {
95+
/******/ if(typeof Symbol !== 'undefined' && Symbol.toStringTag) {
96+
/******/ Object.defineProperty(exports, Symbol.toStringTag, { value: 'Module' });
97+
/******/ }
98+
/******/ Object.defineProperty(exports, '__esModule', { value: true });
99+
/******/ };
100+
/******/ })();
101+
/******/
102+
/************************************************************************/
103+
var __webpack_exports__ = {};
104+
// This entry need to be wrapped in an IIFE because it need to be isolated against other modules in the chunk.
105+
(() => {
106+
/*!**************************************************!*\
107+
!*** ./src/js/blocks/plugins/pattern-preview.js ***!
108+
\**************************************************/
109+
__webpack_require__.r(__webpack_exports__);
110+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(/*! react */ "react");
111+
/* harmony import */ var react__WEBPACK_IMPORTED_MODULE_0___default = /*#__PURE__*/__webpack_require__.n(react__WEBPACK_IMPORTED_MODULE_0__);
112+
/* harmony import */ var _wordpress_i18n__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(/*! @wordpress/i18n */ "@wordpress/i18n");
113+
/* harmony import */ var _wordpress_i18n__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_wordpress_i18n__WEBPACK_IMPORTED_MODULE_1__);
114+
/* harmony import */ var _wordpress_plugins__WEBPACK_IMPORTED_MODULE_2__ = __webpack_require__(/*! @wordpress/plugins */ "@wordpress/plugins");
115+
/* harmony import */ var _wordpress_plugins__WEBPACK_IMPORTED_MODULE_2___default = /*#__PURE__*/__webpack_require__.n(_wordpress_plugins__WEBPACK_IMPORTED_MODULE_2__);
116+
117+
118+
119+
120+
/**
121+
* Render a Preview Button.
122+
*
123+
* @return {Object} The rendered component.
124+
*/
125+
var PatternPreviewButton = function PatternPreviewButton() {
126+
(0,react__WEBPACK_IMPORTED_MODULE_0__.useEffect)(function () {
127+
var headerToolbar = document.querySelector('.edit-post-header');
128+
if (null === headerToolbar) {
129+
return;
130+
}
131+
132+
// Get the left toolbar and add to it.
133+
var settingsToolbar = headerToolbar.querySelector('.edit-post-header__settings');
134+
if (null === settingsToolbar) {
135+
return;
136+
}
137+
138+
// Create the button.
139+
var button = document.createElement('a');
140+
button.className = 'dlx-button-preview components-button has-icon';
141+
button.ariaLabel = (0,_wordpress_i18n__WEBPACK_IMPORTED_MODULE_1__.__)('Preview', 'dlx-pattern-wrangler');
142+
button.href = dlxPatternWranglerPreview.previewUrl;
143+
button.target = '_blank';
144+
button.rel = 'noopener noreferrer';
145+
146+
// Add icon.
147+
var icon = document.createElement('svg');
148+
icon.className = 'dlx-pattern-wrangler-preview-icon';
149+
icon.innerHTML = '<svg width="16" height="16" style="display: inline-block; margin-right: 8px;"><use xlink:href="#dlx-pattern-wrangler-preview-icon" /></svg>';
150+
button.prepend(icon);
151+
// Add the button to the toolbar as the first child.
152+
settingsToolbar.prepend(button);
153+
}, []);
154+
return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("svg", {
155+
height: "0",
156+
width: "0",
157+
xmlns: "http://www.w3.org/2000/svg",
158+
style: {
159+
display: 'none'
160+
},
161+
"aria-hidden": "true"
162+
}, /*#__PURE__*/React.createElement("symbol", {
163+
id: "dlx-pattern-wrangler-preview-icon",
164+
width: "16",
165+
height: "16",
166+
viewBox: "0 0 512 512"
167+
}, /*#__PURE__*/React.createElement("path", {
168+
fill: "currentColor",
169+
d: "M304 24c0 13.3 10.7 24 24 24h102.1L207 271c-9.4 9.4-9.4 24.6 0 33.9s24.6 9.4 33.9 0l223-223V184c0 13.3 10.7 24 24 24s24-10.7 24-24V24c0-13.3-10.7-24-24-24H328c-13.3 0-24 10.7-24 24zM72 32C32.2 32 0 64.2 0 104v336c0 39.8 32.2 72 72 72h336c39.8 0 72-32.2 72-72V312c0-13.3-10.7-24-24-24s-24 10.7-24 24v128c0 13.3-10.7 24-24 24H72c-13.3 0-24-10.7-24-24V104c0-13.3 10.7-24 24-24h128c13.3 0 24-10.7 24-24s-10.7-24-24-24H72z"
170+
}))));
171+
};
172+
(0,_wordpress_plugins__WEBPACK_IMPORTED_MODULE_2__.registerPlugin)('dlx-pattern-wrangler-preview-button', {
173+
render: PatternPreviewButton
174+
});
175+
})();
176+
177+
/******/ })()
178+
;

build/dlx-pw-preview.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

build/index.asset.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-i18n'), 'version' => 'e902d3c84f955b703431');
1+
<?php return array('dependencies' => array('react', 'wp-block-editor', 'wp-blocks', 'wp-components', 'wp-compose', 'wp-data', 'wp-i18n'), 'version' => '97feab35bf36d9fe3aa4');

build/index.js

Lines changed: 7418 additions & 2 deletions
Large diffs are not rendered by default.

build/index.js.map

Lines changed: 0 additions & 1 deletion
This file was deleted.

php/Functions.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,26 @@ public static function get_kses_allowed_html( $svg = true ) {
329329
'class' => array(),
330330
);
331331
$allowed_tags['a']['class'] = array();
332+
$allowed_tags['input'] = array(
333+
'type' => array(),
334+
'name' => array(),
335+
'value' => array(),
336+
'class' => array(),
337+
'readonly' => array(),
338+
);
339+
$allowed_tags['button'] = array(
340+
'type' => array(),
341+
'name' => array(),
342+
'value' => array(),
343+
'class' => array(),
344+
'title' => array(),
345+
);
346+
$allowed_tags['div'] = array(
347+
'class' => array(),
348+
);
349+
$allowed_tags['span'] = array(
350+
'class' => array(),
351+
);
332352

333353
if ( ! $svg ) {
334354
return $allowed_tags;

php/Patterns.php

Lines changed: 80 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,9 @@ class Patterns {
1818
public function run() {
1919
$options = Options::get_options();
2020

21+
// Enqueue scripts for the wp_block post_type.
22+
add_action( 'admin_enqueue_scripts', array( $this, 'add_post_type_enqueue_scripts' ) );
23+
2124
// Deregister any disabled pattern categories.
2225
add_action( 'init', array( $this, 'maybe_deregister_pattern_categories' ), 999 );
2326

@@ -48,11 +51,11 @@ public function run() {
4851
// Change post type label for featured image.
4952
add_filter( 'post_type_labels_wp_block', array( $this, 'change_featured_image_label' ) );
5053

51-
// Add post type column for featured image.
52-
add_filter( 'manage_wp_block_posts_columns', array( $this, 'add_featured_image_column' ) );
54+
// Add post type column for shortcode.
55+
add_filter( 'manage_wp_block_posts_columns', array( $this, 'add_post_type_columns' ) );
5356

5457
// Add a featured image to the wp_block post type column.
55-
add_action( 'manage_wp_block_posts_custom_column', array( $this, 'add_featured_image_column_content' ), 10, 2 );
58+
add_action( 'manage_wp_block_posts_custom_column', array( $this, 'add_post_type_columns_content' ), 10, 2 );
5659

5760
// Add bulk action for making patterns a draft.
5861
add_filter( 'bulk_actions-edit-wp_block', array( $this, 'add_draft_bulk_actions' ) );
@@ -153,10 +156,36 @@ public function enable_menus_ui() {
153156
* @param string $column_name Column name.
154157
* @param int $post_id Post ID.
155158
*/
156-
public function add_featured_image_column_content( $column_name, $post_id ) {
159+
public function add_post_type_columns_content( $column_name, $post_id ) {
157160
if ( 'featured_image' === $column_name ) {
158161
$thumbnail = get_the_post_thumbnail( $post_id, array( 125, 125 ) );
159162
echo wp_kses_post( $thumbnail );
163+
return;
164+
}
165+
if ( 'shortcode' === $column_name ) {
166+
$post = get_post( $post_id );
167+
if ( 'publish' === $post->post_status ) {
168+
169+
$shortcode = '<div class="dlxpw-copy-shortcode-container">';
170+
171+
// Generate the shortcode for the readonly input.
172+
$shortcode .= sprintf(
173+
'<input type="text" value="%s" readonly>',
174+
esc_attr( sprintf( '[block slug="%s"]', $post->post_name ) )
175+
);
176+
177+
// Generate the shortcode for the button with dashicon.
178+
$shortcode .= sprintf(
179+
'<button type="button" aria-label="%1$s" class="dlxpw-copy-shortcode dlx-copy-shortcode-hidden" title="%1$s">%2$s</button>',
180+
esc_attr__( 'Copy shortcode', 'dlx-pattern-wrangler' ),
181+
'<span class="dashicons dashicons-clipboard"></span>'
182+
);
183+
184+
$shortcode .= '</div>';
185+
186+
// Output the combined shortcode with allowed HTML tags.
187+
echo wp_kses( $shortcode, Functions::get_kses_allowed_html( false ) );
188+
}
160189
}
161190
}
162191

@@ -167,9 +196,12 @@ public function add_featured_image_column_content( $column_name, $post_id ) {
167196
*
168197
* @return array Updated columns.
169198
*/
170-
public function add_featured_image_column( $columns ) {
199+
public function add_post_type_columns( $columns ) {
171200
// Add featured image to 2nd column.
172201
$columns = array_slice( $columns, 0, 2, true ) + array( 'featured_image' => __( 'Pattern Preview', 'dlx-pattern-wrangler' ) ) + array_slice( $columns, 1, count( $columns ) - 1, true );
202+
203+
// Add shortcode to 3rd column.
204+
$columns = array_slice( $columns, 0, 3, true ) + array( 'shortcode' => __( 'Shortcode', 'dlx-pattern-wrangler' ) ) + array_slice( $columns, 2, count( $columns ) - 1, true );
173205
return $columns;
174206
}
175207

@@ -453,4 +485,47 @@ public function maybe_deregister_pattern_categories() {
453485
}
454486
}
455487
}
488+
489+
/**
490+
* Enqueue scripts for the wp_block post type.
491+
*
492+
* @param string $hook The current post type page.
493+
*/
494+
public function add_post_type_enqueue_scripts( $hook ) {
495+
$post = get_post();
496+
// Only proceed if we are on a valid page and the current post is not null.
497+
if ( 'edit.php' !== $hook || ! $post ) {
498+
return;
499+
}
500+
501+
// Check if the current post type is wp_block.
502+
if ( 'wp_block' === $post->post_type ) {
503+
// Load asset file to get dependencies and version.
504+
$asset_file = Functions::get_plugin_dir( 'dist/dlx-pw-admin.asset.php' );
505+
506+
// Check if asset file exists and is readable.
507+
if ( file_exists( $asset_file ) && is_readable( $asset_file ) ) {
508+
// Retrieve dependencies and version from asset file.
509+
$deps = require $asset_file;
510+
511+
// Enqueue script with retrieved dependencies and version.
512+
wp_enqueue_script(
513+
'dlx-pw-post-utilities',
514+
Functions::get_plugin_url( 'dist/dlx-pw-post-utilities.js' ),
515+
$deps['dependencies'],
516+
$deps['version'],
517+
true
518+
);
519+
}
520+
521+
// Enqueue admin styles.
522+
wp_enqueue_style(
523+
'dlx-pw-admin-css',
524+
Functions::get_plugin_url( 'dist/dlx-pw-admin-utils-css.css' ),
525+
array(),
526+
Functions::get_plugin_version(),
527+
'all'
528+
);
529+
}
530+
}
456531
}

0 commit comments

Comments
 (0)