Skip to content

Commit 0f1abd5

Browse files
authored
Merge branch 'master' into fix/multiple-custom-fields
2 parents c63eb36 + bbd57a5 commit 0f1abd5

File tree

2 files changed

+562
-118
lines changed

2 files changed

+562
-118
lines changed

modules/custom-status/custom-status.php

Lines changed: 78 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,8 @@ function init() {
9898
// These seven-ish methods are hacks for fixing bugs in WordPress core
9999
add_action( 'admin_init', array( $this, 'check_timestamp_on_publish' ) );
100100
add_filter( 'wp_insert_post_data', array( $this, 'fix_custom_status_timestamp' ), 10, 2 );
101-
add_action( 'wp_insert_post', array( $this, 'fix_post_name' ), 10, 2 );
101+
add_filter( 'wp_insert_post_data', array( $this, 'maybe_keep_post_name_empty' ), 10, 2 );
102+
add_filter( 'pre_wp_unique_post_slug', array( $this, 'fix_unique_post_slug' ), 10, 6 );
102103
add_filter( 'preview_post_link', array( $this, 'fix_preview_link_part_one' ) );
103104
add_filter( 'post_link', array( $this, 'fix_preview_link_part_two' ), 10, 3 );
104105
add_filter( 'page_link', array( $this, 'fix_preview_link_part_two' ), 10, 3 );
@@ -1367,41 +1368,66 @@ function fix_custom_status_timestamp( $data, $postarr ) {
13671368
}
13681369

13691370
/**
1370-
* Another hack! hack! hack! until core better supports custom statuses`
1371+
* A new hack! hack! hack! until core better supports custom statuses`
13711372
*
1372-
* @since 0.7.4
1373+
* @since 0.9.4
13731374
*
1374-
* Keep the post_name value empty for posts with custom statuses
1375-
* Unless they've set it customly
1375+
* If the post_name is set, set it, otherwise keep it empty
1376+
*
13761377
* @see https://github.com/Automattic/Edit-Flow/issues/123
13771378
* @see http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/post.php#L2530
13781379
* @see http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/post.php#L2646
13791380
*/
1380-
public function fix_post_name( $post_id, $post ) {
1381-
global $pagenow;
1381+
public function maybe_keep_post_name_empty( $data, $postarr ) {
1382+
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
13821383

1383-
/*
1384-
* Filters the $post object that will be modified
1385-
*
1386-
* @param $post WP_Post Post object being processed.
1387-
*/
1388-
$post = apply_filters( 'ef_fix_post_name_post', $post );
1384+
// Ignore if it's not a post status and post type we support
1385+
if ( ! in_array( $data['post_status'], $status_slugs )
1386+
|| ! in_array( $data['post_type'], $this->get_post_types_for_module( $this->module ) ) ) {
1387+
return $data;
1388+
}
13891389

1390-
// Only modify if we're using a pre-publish status on a supported custom post type
1390+
// If the post_name was intentionally set, set the post_name
1391+
if ( ! empty( $postarr['post_name'] ) ) {
1392+
$data['post_name'] = $postarr['post_name'];
1393+
return $data;
1394+
}
1395+
1396+
// Otherwise, keep the post_name empty
1397+
$data['post_name'] = '';
1398+
1399+
return $data;
1400+
}
1401+
1402+
/**
1403+
* A new hack! hack! hack! until core better supports custom statuses`
1404+
*
1405+
* @since 0.9.4
1406+
*
1407+
* `wp_unique_post_slug` is used to set the `post_name`. When a custom status is used, WordPress will try
1408+
* really hard to set `post_name`, and we leverage `wp_unique_post_slug` to prevent it being set
1409+
*
1410+
* @see: https://github.com/WordPress/WordPress/blob/396647666faebb109d9cd4aada7bb0c7d0fb8aca/wp-includes/post.php#L3932
1411+
*/
1412+
public function fix_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
13911413
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
1392-
if ( 'post.php' != $pagenow
1393-
|| ! in_array( $post->post_status, $status_slugs )
1394-
|| ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) )
1395-
return;
13961414

1397-
// The slug has been set by the meta box
1398-
if ( ! empty( $_POST['post_name'] ) )
1399-
return;
1415+
if ( ! in_array( $post_status, $status_slugs )
1416+
|| ! in_array( $post_type, $this->get_post_types_for_module( $this->module ) ) ) {
1417+
return null;
1418+
}
14001419

1401-
global $wpdb;
1420+
$post = get_post( $post_ID );
1421+
1422+
if ( empty( $post ) ) {
1423+
return null;
1424+
}
14021425

1403-
$wpdb->update( $wpdb->posts, array( 'post_name' => '' ), array( 'ID' => $post_id ) );
1404-
clean_post_cache( $post_id );
1426+
if ( $post->post_name ) {
1427+
return $slug;
1428+
}
1429+
1430+
return '';
14051431
}
14061432

14071433

@@ -1509,49 +1535,30 @@ public function fix_preview_link_part_three( $preview_link, $query_args ) {
15091535
* @return string $link Direct link to complete the action
15101536
*/
15111537
public function fix_get_sample_permalink( $permalink, $post_id, $title, $name, $post ) {
1512-
//Should we be doing anything at all?
1513-
if( !in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) )
1514-
return $permalink;
15151538

1516-
//Is this published?
1517-
if( in_array( $post->post_status, $this->published_statuses ) )
1518-
return $permalink;
1539+
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
15191540

1520-
//Are we overriding the permalink? Don't do anything
1521-
if( isset( $_POST['action'] ) && $_POST['action'] == 'sample-permalink' )
1541+
if ( ! in_array( $post->post_status, $status_slugs )
1542+
|| ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) ) {
15221543
return $permalink;
1544+
}
15231545

1524-
list( $permalink, $post_name ) = $permalink;
1525-
1526-
$post_name = $post->post_name ? $post->post_name : sanitize_title( $post->post_title );
1527-
$post->post_name = $post_name;
1528-
1529-
$ptype = get_post_type_object( $post->post_type );
1530-
1531-
if ( $ptype->hierarchical ) {
1532-
$post->filter = 'sample';
1546+
remove_filter( 'get_sample_permalink', array( $this, 'fix_get_sample_permalink' ), 10, 5 );
15331547

1534-
$uri = get_page_uri( $post->ID ) . $post_name;
1548+
$new_name = ! is_null( $name ) ? $name : $post->post_name;
1549+
$new_title = ! is_null( $title ) ? $title : $post->post_title;
15351550

1536-
if ( $uri ) {
1537-
$uri = untrailingslashit($uri);
1538-
$uri = strrev( stristr( strrev( $uri ), '/' ) );
1539-
$uri = untrailingslashit($uri);
1540-
}
1551+
$post = get_post( $post_id );
1552+
$status_before = $post->post_status;
1553+
$post->post_status = 'draft';
15411554

1542-
/** This filter is documented in wp-admin/edit-tag-form.php */
1543-
$uri = apply_filters( 'editable_slug', $uri, $post );
1555+
$permalink = get_sample_permalink( $post, $title, sanitize_title( $new_name ? $new_name : $new_title, $post->ID ) );
15441556

1545-
if ( !empty($uri) ) {
1546-
$uri .= '/';
1547-
}
1557+
$post->post_status = $status_before;
15481558

1549-
$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
1550-
}
1551-
1552-
unset($post->post_name);
1559+
add_filter( 'get_sample_permalink', array( $this, 'fix_get_sample_permalink' ), 10, 5 );
15531560

1554-
return array( $permalink, $post_name );
1561+
return $permalink;
15551562
}
15561563

15571564
/**
@@ -1566,75 +1573,28 @@ public function fix_get_sample_permalink( $permalink, $post_id, $title, $name, $
15661573
*
15671574
* @since 0.8.2
15681575
*
1569-
* @param string $return Sample permalink HTML markup
1570-
* @param int $post_id Post ID
1571-
* @param string $new_title New sample permalink title
1572-
* @param string $new_slug New sample permalink kslug
1573-
* @param WP_Post $post Post object
1576+
* @param string $return Sample permalink HTML markup.
1577+
* @param int $post_id Post ID.
1578+
* @param string $new_title New sample permalink title.
1579+
* @param string $new_slug New sample permalink slug.
1580+
* @param WP_Post $post Post object.
15741581
*/
1575-
function fix_get_sample_permalink_html( $return, $post_id, $new_title, $new_slug, $post ) {
1582+
public function fix_get_sample_permalink_html( $permalink, $post_id, $new_title, $new_slug, $post ) {
15761583
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
15771584

1578-
list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
1579-
1580-
$view_link = false;
1581-
$preview_target = '';
1582-
1583-
if ( current_user_can( 'read_post', $post_id ) ) {
1584-
if ( in_array( $post->post_status, $status_slugs ) ) {
1585-
$view_link = $this->get_preview_link( $post );
1586-
$preview_target = " target='wp-preview-{$post->ID}'";
1587-
} else {
1588-
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
1589-
$view_link = get_permalink( $post );
1590-
} else {
1591-
// Allow non-published (private, future) to be viewed at a pretty permalink.
1592-
$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
1593-
}
1594-
}
1585+
if ( ! in_array( $post->post_status, $status_slugs )
1586+
|| ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) ) {
1587+
return $permalink;
15951588
}
15961589

1597-
// Permalinks without a post/page name placeholder don't have anything to edit
1598-
if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
1599-
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1600-
1601-
if ( false !== $view_link ) {
1602-
$display_link = urldecode( $view_link );
1603-
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a>\n";
1604-
} else {
1605-
$return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
1606-
}
1607-
1608-
// Encourage a pretty permalink setting
1609-
if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
1610-
$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
1611-
}
1612-
} else {
1613-
if ( function_exists( 'mb_strlen' ) ) {
1614-
if ( mb_strlen( $post_name ) > 34 ) {
1615-
$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
1616-
} else {
1617-
$post_name_abridged = $post_name;
1618-
}
1619-
} else {
1620-
if ( strlen( $post_name ) > 34 ) {
1621-
$post_name_abridged = substr( $post_name, 0, 16 ) . '&hellip;' . substr( $post_name, -16 );
1622-
} else {
1623-
$post_name_abridged = $post_name;
1624-
}
1625-
}
1590+
remove_filter( 'get_sample_permalink_html', array( $this, 'fix_get_sample_permalink_html' ), 10, 5 );
16261591

1627-
$post_name_html = '<span id="editable-post-name">' . $post_name_abridged . '</span>';
1628-
$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) );
1592+
$post->post_status = 'draft';
1593+
$sample_permalink_html = get_sample_permalink_html( $post, $new_title, $new_slug );
16291594

1630-
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1631-
$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
1632-
$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
1633-
$return .= '<span id="edit-slug-buttons"><button type="button" class="edit-slug button button-small hide-if-no-js" aria-label="' . __( 'Edit permalink' ) . '">' . __( 'Edit' ) . "</button></span>\n";
1634-
$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
1635-
}
1595+
add_filter( 'get_sample_permalink_html', array( $this, 'fix_get_sample_permalink_html' ), 10, 5 );
16361596

1637-
return $return;
1597+
return $sample_permalink_html;
16381598
}
16391599

16401600

0 commit comments

Comments
 (0)