Skip to content

Commit 4c3b806

Browse files
committed
Merge branch 'develop' into fix/screenshots
2 parents 459d5c3 + dcf8510 commit 4c3b806

29 files changed

Lines changed: 6072 additions & 2655 deletions

.nvmrc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
16
1+
18

assets/js/admin-external-connection.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -324,7 +324,7 @@ function checkConnections() {
324324

325325
if ( response.data.endpoint_suggestion ) {
326326
endpointResult.innerText = `${ __(
327-
'Did you mean: ',
327+
'Did you mean:',
328328
'distributor'
329329
) } `;
330330

@@ -337,7 +337,7 @@ function checkConnections() {
337337
endpointResult.appendChild( suggestion );
338338

339339
speak(
340-
`${ __( 'Did you mean: ', 'distributor' ) } ${
340+
`${ __( 'Did you mean:', 'distributor' ) } ${
341341
response.data.endpoint_suggestion
342342
}`,
343343
'polite'

assets/js/admin-pull.js

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,20 @@ import jQuery from 'jquery';
44
import { addQueryArgs } from '@wordpress/url';
55
import { __ } from '@wordpress/i18n';
66

7-
const { document } = window;
7+
const { document, DISTRIBUTOR } = window;
8+
const { getPullUrl } = DISTRIBUTOR;
9+
10+
/**
11+
* Escape special characters in URL components.
12+
*
13+
* @param {string} str The string to escape.
14+
* @return {string} The escaped string.
15+
*/
16+
const escapeURLComponent = ( str ) => {
17+
return encodeURIComponent( str ).replace( /[!'()*]/g, ( c ) => {
18+
return '%' + c.charCodeAt( 0 ).toString( 16 );
19+
} );
20+
};
821

922
const chooseConnection = document.getElementById( 'pull_connections' );
1023
const choosePostType = document.getElementById( 'pull_post_type' );
@@ -16,11 +29,12 @@ const asDraftCheckboxes = document.querySelectorAll( '[name=dt_as_draft]' );
1629
const pullLinks = document.querySelectorAll( '.distributor_page_pull .pull a' );
1730

1831
jQuery( chooseConnection ).on( 'change', ( event ) => {
19-
document.location =
32+
const pullUrlId =
2033
event.currentTarget.options[
2134
event.currentTarget.selectedIndex
22-
].getAttribute( 'data-pull-url' );
35+
].getAttribute( 'data-pull-url-id' );
2336

37+
document.location = getPullUrl( pullUrlId );
2438
document.body.className += ' ' + 'dt-loading';
2539
} );
2640

@@ -82,12 +96,15 @@ if ( chooseConnection && choosePostType && form ) {
8296
* @return {string} Distribution URL.
8397
*/
8498
const getURL = () => {
85-
const postType =
86-
choosePostType.options[ choosePostType.selectedIndex ].value;
87-
const baseURL =
99+
const postType = escapeURLComponent(
100+
choosePostType.options[ choosePostType.selectedIndex ].value
101+
);
102+
const pullUrlId = escapeURLComponent(
88103
chooseConnection.options[ chooseConnection.selectedIndex ].getAttribute(
89-
'data-pull-url'
90-
);
104+
'data-pull-url-id'
105+
)
106+
);
107+
const baseURL = getPullUrl( pullUrlId );
91108
let status = 'new';
92109

93110
if ( -1 < ` ${ form.className } `.indexOf( ' status-skipped ' ) ) {

assets/js/gutenberg-plugin.js

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ const RenderDistributedFrom = () => {
151151
) }
152152
</span>
153153
<span id="distributed-data">
154-
{ __( 'Updating the ', 'distributor' ) }
154+
{ __( 'Updating the', 'distributor' ) }{ ' ' }
155155
<a href={ dtGutenberg.postUrl }>
156156
{ __( 'Original Content', 'distributor' ) }
157157
</a>
@@ -217,7 +217,7 @@ const RenderDistributedFrom = () => {
217217
) }
218218
</span>
219219
<span id="distributed-data">
220-
{ __( 'This post has been unlinked from the ', 'distributor' ) }
220+
{ __( 'This post has been unlinked from the', 'distributor' ) }{ ' ' }
221221
<a href={ dtGutenberg.postUrl }>
222222
{ __( 'Original Content', 'distributor' ) }
223223
</a>
@@ -239,12 +239,12 @@ const RenderDistributedFrom = () => {
239239
overlayClassName="distributed-modal-overlay"
240240
>
241241
<span id="distributed-data">
242-
{ __( 'Restoring the link to the ', 'distributor' ) }
242+
{ __( 'Restoring the link to the', 'distributor' ) }{ ' ' }
243243
<a href={ dtGutenberg.postUrl }>
244244
{ __( 'Original Content', 'distributor' ) }
245-
</a>
245+
</a>{ ' ' }
246246
{ __(
247-
' will start updating this post automatically from the Original, overwriting current content.',
247+
'will start updating this post automatically from the Original, overwriting current content.',
248248
'distributor'
249249
) }
250250
</span>

docs/auto-distribution.md

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
You can enable the automatic distribution of posts upon publication by togging on the "auto-distribute" feature.
2+
3+
To enable auto-distribution, you can include this code in your site's feature plugin.
4+
5+
```php
6+
add_filter( 'dt_auto_distribution_enabled', '__return_true' );
7+
```
8+
9+
This code must run prior to or on the `plugins_loaded` hook. If running on the `plugins_loaded` hook, it must do so at priority 19 or lower.
10+
11+
With auto-distribution enabled, the following will occur:
12+
13+
* upon publication, posts and pages will be pushed to all internal and external connections,
14+
* the default status for pushing posts is `publish`, and,
15+
* auto distribution is runs via cron jobs to avoid slowing down the publication process for users.
16+
17+
## Filters
18+
19+
The auto-distribution feature comes with a number of filters in additional to the one above.
20+
21+
### Auto-distributed post types
22+
23+
By default only posts and pages are auto-distributed. To enable auto-distribution of custom post
24+
types requires the `auto_distribute_supported_post_types` filter be used.
25+
26+
To add a custom post type to supported post types would require the code:
27+
28+
```php
29+
add_filter( 'auto_distribute_supported_post_types', function( $post_types ) {
30+
$post_types[] = 'my_cpt';
31+
return $post_types;
32+
} );
33+
```
34+
35+
### Default post status.
36+
37+
The `dt_auto_distribution_default_status` filter allows you to filter the default post status for
38+
distribution. To modify the default post status for one post type but not others, you can use the code:
39+
40+
```php
41+
function ad_demo_modify_page_default_status( $default_status, $post ) {
42+
if ( 'page' !== get_post_type() ) {
43+
return $default_status;
44+
}
45+
46+
return 'draft';
47+
}
48+
add_filter( 'dt_auto_distribution_default_status', 'ad_demo_modify_page_default_status', 10, 2 );
49+
```
50+
51+
### Whether to auto-distribute a post
52+
53+
The `dt_auto_distribute_post` filter allows you to filter whether an individual post will be
54+
auto-distributed. The filter accepts a number of arguments providing the context of the post,
55+
see the [filter's docs](./dt_auto_distribute_post.html) for further information.'
56+
57+
This filter is only run for post types that are supported, see above.
58+
59+
To prevent auto-distribution to a certain connection type:
60+
61+
```php
62+
function ad_demo_no_external( $should_distribute, $post, $user_id, $connection_type ) {
63+
if ( $connection_type === 'external' ) {
64+
return false;
65+
}
66+
return $should_distribute;
67+
}
68+
add_filter( 'dt_auto_distribute_post', 'ad_demo_no_external', 10, 4 );
69+
```

docs/stored-id-handling.md

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ This function registers a data piece by providing details about its location and
4040
- `block_name` (`string`): The block name. **(Required)**
4141
- `block_attribute` (`string|array`): The attribute(s) containing the data. **(Required)**
4242

43+
- **`type`** (`string`):
44+
Type of data. Accepted values are `'media'`, `'post'`, or `'term'`. If set, the default callbacks will be used.
45+
**Note:** If this parameter is used in conjunction with custom callbacks, the custom callbacks will be used instead.
46+
4347
- **`pre_distribute_cb`** (`callable`):
4448
A callback function that prepares the data on the source site before distribution. It should return extra data that is required for processing on the target site.
4549

@@ -56,6 +60,8 @@ Below are several examples demonstrating how to use the `distributor_register_da
5660

5761
In this example, we register an image ID stored in post meta under the key `example_image_id`.
5862

63+
#### With Custom Callback Functions
64+
5965
```php
6066
distributor_register_data( 'example_post_meta_data', array(
6167
'location' => 'post_meta',
@@ -105,7 +111,7 @@ distributor_register_data( 'example_post_meta_data', array(
105111
) );
106112
```
107113

108-
#### Explanation
114+
**Explanation**
109115

110116
1. **Pre-Distribution Callback (`pre_distribute_cb`):**
111117
- Retrieves the URL of the image using the stored ID.
@@ -116,10 +122,24 @@ distributor_register_data( 'example_post_meta_data', array(
116122
- If not found, processes (uploads) the media and updates the relevant post meta with the original source data.
117123
- Returns the new image ID for the distributed post.
118124

125+
#### Using the `type` Parameter to Utilize the Default Callback
126+
127+
```php
128+
distributor_register_data( 'example_post_meta_data', array(
129+
'location' => 'post_meta',
130+
'attributes' => array(
131+
'meta_key' => 'example_image_id',
132+
),
133+
'type' => 'media'
134+
) );
135+
```
136+
119137
### 2. Handling Term ID in a Block Attribute
120138

121139
This example demonstrates how to handle a term ID stored in a block attribute.
122140

141+
#### With Custom Callback Functions
142+
123143
```php
124144
distributor_register_data(
125145
'example_block_data',
@@ -177,10 +197,28 @@ distributor_register_data(
177197
);
178198
```
179199

200+
#### Using the `type` Parameter to Utilize the Default Callback
201+
202+
```php
203+
distributor_register_data(
204+
'example_block_data',
205+
array(
206+
'location' => 'post_content',
207+
'attributes' => array(
208+
'block_name' => 'example/block-name',
209+
'block_attribute' => 'id',
210+
),
211+
'type' => 'term'
212+
)
213+
);
214+
```
215+
216+
180217
### 3. Handling Post ID in a Shortcode Attribute
181218

182219
This example shows how to manage a post ID stored within a shortcode attribute.
183220

221+
#### With Custom Callback Functions
184222
```php
185223
// Distributor data registration for the post ID stored in shortcode attribute.
186224
distributor_register_data( 'example_post_data', array(
@@ -251,4 +289,17 @@ distributor_register_data( 'example_post_data', array(
251289
) );
252290
```
253291

292+
#### Using the `type` Parameter to Utilize the Default Callback
293+
```php
294+
// Distributor data registration for the post ID stored in shortcode attribute.
295+
distributor_register_data( 'example_post_data', array(
296+
'location' => 'post_content',
297+
'attributes' => array(
298+
'shortcode' => 'extra_shortcode',
299+
'shortcode_attribute' => 'example_post_id',
300+
),
301+
'type' => 'post'
302+
) );
303+
```
304+
254305
If you have any doubts, encounter any different scenarios to handle, or need further assistance, please feel free to report an issue on our [GitHub repository](https://github.com/10up/distributor) and we would be happy to help.

docs/tutorials.json

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,13 @@
1414
"snippets": {
1515
"title": "Snippets"
1616
},
17-
"demo-with-playground": {
18-
"title": "Demo with Playground"
19-
},
20-
"stored-id-handling": {
21-
"title": "Stored ID Handling"
22-
}
17+
"demo-with-playground": {
18+
"title": "Demo with Playground"
19+
},
20+
"stored-id-handling": {
21+
"title": "Stored ID Handling"
22+
},
23+
"auto-distribution": {
24+
"title": "Enabling Auto Distribution"
25+
}
2326
}

0 commit comments

Comments
 (0)