Skip to content

Commit d978b29

Browse files
committed
Ensure post_name is set/unset given context of request
1 parent 2507889 commit d978b29

File tree

2 files changed

+548
-118
lines changed

2 files changed

+548
-118
lines changed

modules/custom-status/custom-status.php

Lines changed: 64 additions & 118 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,8 @@ function init() {
112112
// These seven-ish methods are hacks for fixing bugs in WordPress core
113113
add_action( 'admin_init', array( $this, 'check_timestamp_on_publish' ) );
114114
add_filter( 'wp_insert_post_data', array( $this, 'fix_custom_status_timestamp' ), 10, 2 );
115-
add_action( 'wp_insert_post', array( $this, 'fix_post_name' ), 10, 2 );
115+
add_filter( 'wp_insert_post_data', array( $this, 'maybe_keep_post_name_empty' ), 10, 2 );
116+
add_filter( 'pre_wp_unique_post_slug', array( $this, 'fix_unique_post_slug' ), 10, 6 );
116117
add_filter( 'preview_post_link', array( $this, 'fix_preview_link_part_one' ) );
117118
add_filter( 'post_link', array( $this, 'fix_preview_link_part_two' ), 10, 3 );
118119
add_filter( 'page_link', array( $this, 'fix_preview_link_part_two' ), 10, 3 );
@@ -1397,41 +1398,52 @@ function fix_custom_status_timestamp( $data, $postarr ) {
13971398
}
13981399

13991400
/**
1400-
* Another hack! hack! hack! until core better supports custom statuses`
1401+
* A new hack! hack! hack! until core better supports custom statuses`
14011402
*
1402-
* @since 0.7.4
1403+
* @since 0.9.3
14031404
*
1404-
* Keep the post_name value empty for posts with custom statuses
1405-
* Unless they've set it customly
1405+
* If the slug should stay empty, mark it
14061406
* @see https://github.com/Automattic/Edit-Flow/issues/123
14071407
* @see http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/post.php#L2530
14081408
* @see http://core.trac.wordpress.org/browser/tags/3.4.2/wp-includes/post.php#L2646
14091409
*/
1410-
public function fix_post_name( $post_id, $post ) {
1411-
global $pagenow;
1410+
public function maybe_keep_post_name_empty( $data, $postarr ) {
1411+
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
14121412

1413-
/*
1414-
* Filters the $post object that will be modified
1415-
*
1416-
* @param $post WP_Post Post object being processed.
1417-
*/
1418-
$post = apply_filters( 'ef_fix_post_name_post', $post );
1413+
if ( ! in_array( $data['post_status'], $status_slugs )
1414+
|| ! in_array( $data['post_type'], $this->get_post_types_for_module( $this->module ) ) ) {
1415+
return $data;
1416+
}
14191417

1420-
// Only modify if we're using a pre-publish status on a supported custom post type
1418+
if ( ! empty( $postarr['post_name'] ) ) {
1419+
$data['post_name'] = $postarr['post_name'];
1420+
return $data;
1421+
}
1422+
1423+
$data['post_name'] = '';
1424+
1425+
return $data;
1426+
}
1427+
1428+
public function fix_unique_post_slug( $override_slug, $slug, $post_ID, $post_status, $post_type, $post_parent ) {
14211429
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
1422-
if ( 'post.php' != $pagenow
1423-
|| ! in_array( $post->post_status, $status_slugs )
1424-
|| ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) )
1425-
return;
14261430

1427-
// The slug has been set by the meta box
1428-
if ( ! empty( $_POST['post_name'] ) )
1429-
return;
1431+
if ( ! in_array( $post_status, $status_slugs )
1432+
|| ! in_array( $post_type, $this->get_post_types_for_module( $this->module ) ) ) {
1433+
return null;
1434+
}
14301435

1431-
global $wpdb;
1436+
$post = get_post( $post_ID );
1437+
1438+
if ( empty( $post ) ) {
1439+
return null;
1440+
}
1441+
1442+
if ( $post->post_name ) {
1443+
return $slug;
1444+
}
14321445

1433-
$wpdb->update( $wpdb->posts, array( 'post_name' => '' ), array( 'ID' => $post_id ) );
1434-
clean_post_cache( $post_id );
1446+
return '';
14351447
}
14361448

14371449

@@ -1539,49 +1551,30 @@ public function fix_preview_link_part_three( $preview_link, $query_args ) {
15391551
* @return string $link Direct link to complete the action
15401552
*/
15411553
public function fix_get_sample_permalink( $permalink, $post_id, $title, $name, $post ) {
1542-
//Should we be doing anything at all?
1543-
if( !in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) )
1544-
return $permalink;
15451554

1546-
//Is this published?
1547-
if( in_array( $post->post_status, $this->published_statuses ) )
1548-
return $permalink;
1555+
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
15491556

1550-
//Are we overriding the permalink? Don't do anything
1551-
if( isset( $_POST['action'] ) && $_POST['action'] == 'sample-permalink' )
1557+
if ( ! in_array( $post->post_status, $status_slugs )
1558+
|| ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) ) {
15521559
return $permalink;
1560+
}
15531561

1554-
list( $permalink, $post_name ) = $permalink;
1555-
1556-
$post_name = $post->post_name ? $post->post_name : sanitize_title( $post->post_title );
1557-
$post->post_name = $post_name;
1558-
1559-
$ptype = get_post_type_object( $post->post_type );
1560-
1561-
if ( $ptype->hierarchical ) {
1562-
$post->filter = 'sample';
1563-
1564-
$uri = get_page_uri( $post->ID ) . $post_name;
1562+
remove_filter( 'get_sample_permalink', array( $this, 'fix_get_sample_permalink' ), 10, 5 );
15651563

1566-
if ( $uri ) {
1567-
$uri = untrailingslashit($uri);
1568-
$uri = strrev( stristr( strrev( $uri ), '/' ) );
1569-
$uri = untrailingslashit($uri);
1570-
}
1564+
$new_name = ! is_null( $name ) ? $name : $post->post_name;
1565+
$new_title = ! is_null( $title ) ? $title : $post->post_title;
15711566

1572-
/** This filter is documented in wp-admin/edit-tag-form.php */
1573-
$uri = apply_filters( 'editable_slug', $uri, $post );
1567+
$post = get_post( $post_id );
1568+
$status_before = $post->post_status;
1569+
$post->post_status = 'draft';
15741570

1575-
if ( !empty($uri) ) {
1576-
$uri .= '/';
1577-
}
1571+
$permalink = get_sample_permalink( $post, $title, sanitize_title( $new_name ? $new_name : $new_title, $post->ID ) );
15781572

1579-
$permalink = str_replace('%pagename%', "{$uri}%pagename%", $permalink);
1580-
}
1573+
$post->post_status = $status_before;
15811574

1582-
unset($post->post_name);
1575+
add_filter( 'get_sample_permalink', array( $this, 'fix_get_sample_permalink' ), 10, 5 );
15831576

1584-
return array( $permalink, $post_name );
1577+
return $permalink;
15851578
}
15861579

15871580
/**
@@ -1596,75 +1589,28 @@ public function fix_get_sample_permalink( $permalink, $post_id, $title, $name, $
15961589
*
15971590
* @since 0.8.2
15981591
*
1599-
* @param string $return Sample permalink HTML markup
1600-
* @param int $post_id Post ID
1601-
* @param string $new_title New sample permalink title
1602-
* @param string $new_slug New sample permalink kslug
1603-
* @param WP_Post $post Post object
1592+
* @param string $return Sample permalink HTML markup.
1593+
* @param int $post_id Post ID.
1594+
* @param string $new_title New sample permalink title.
1595+
* @param string $new_slug New sample permalink slug.
1596+
* @param WP_Post $post Post object.
16041597
*/
1605-
function fix_get_sample_permalink_html( $return, $post_id, $new_title, $new_slug, $post ) {
1598+
public function fix_get_sample_permalink_html( $permalink, $post_id, $new_title, $new_slug, $post ) {
16061599
$status_slugs = wp_list_pluck( $this->get_custom_statuses(), 'slug' );
16071600

1608-
list($permalink, $post_name) = get_sample_permalink($post->ID, $new_title, $new_slug);
1609-
1610-
$view_link = false;
1611-
$preview_target = '';
1612-
1613-
if ( current_user_can( 'read_post', $post_id ) ) {
1614-
if ( in_array( $post->post_status, $status_slugs ) ) {
1615-
$view_link = $this->get_preview_link( $post );
1616-
$preview_target = " target='wp-preview-{$post->ID}'";
1617-
} else {
1618-
if ( 'publish' === $post->post_status || 'attachment' === $post->post_type ) {
1619-
$view_link = get_permalink( $post );
1620-
} else {
1621-
// Allow non-published (private, future) to be viewed at a pretty permalink.
1622-
$view_link = str_replace( array( '%pagename%', '%postname%' ), $post->post_name, $permalink );
1623-
}
1624-
}
1601+
if ( ! in_array( $post->post_status, $status_slugs )
1602+
|| ! in_array( $post->post_type, $this->get_post_types_for_module( $this->module ) ) ) {
1603+
return $permalink;
16251604
}
16261605

1627-
// Permalinks without a post/page name placeholder don't have anything to edit
1628-
if ( false === strpos( $permalink, '%postname%' ) && false === strpos( $permalink, '%pagename%' ) ) {
1629-
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1606+
remove_filter( 'get_sample_permalink_html', array( $this, 'fix_get_sample_permalink_html' ), 10, 5 );
16301607

1631-
if ( false !== $view_link ) {
1632-
$display_link = urldecode( $view_link );
1633-
$return .= '<a id="sample-permalink" href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a>\n";
1634-
} else {
1635-
$return .= '<span id="sample-permalink">' . $permalink . "</span>\n";
1636-
}
1608+
$post->post_status = 'draft';
1609+
$sample_permalink_html = get_sample_permalink_html( $post, $new_title, $new_slug );
16371610

1638-
// Encourage a pretty permalink setting
1639-
if ( '' == get_option( 'permalink_structure' ) && current_user_can( 'manage_options' ) && !( 'page' == get_option('show_on_front') && $id == get_option('page_on_front') ) ) {
1640-
$return .= '<span id="change-permalinks"><a href="options-permalink.php" class="button button-small" target="_blank">' . __('Change Permalinks') . "</a></span>\n";
1641-
}
1642-
} else {
1643-
if ( function_exists( 'mb_strlen' ) ) {
1644-
if ( mb_strlen( $post_name ) > 34 ) {
1645-
$post_name_abridged = mb_substr( $post_name, 0, 16 ) . '&hellip;' . mb_substr( $post_name, -16 );
1646-
} else {
1647-
$post_name_abridged = $post_name;
1648-
}
1649-
} else {
1650-
if ( strlen( $post_name ) > 34 ) {
1651-
$post_name_abridged = substr( $post_name, 0, 16 ) . '&hellip;' . substr( $post_name, -16 );
1652-
} else {
1653-
$post_name_abridged = $post_name;
1654-
}
1655-
}
1656-
1657-
$post_name_html = '<span id="editable-post-name">' . $post_name_abridged . '</span>';
1658-
$display_link = str_replace( array( '%pagename%', '%postname%' ), $post_name_html, urldecode( $permalink ) );
1659-
1660-
$return = '<strong>' . __( 'Permalink:' ) . "</strong>\n";
1661-
$return .= '<span id="sample-permalink"><a href="' . esc_url( $view_link ) . '"' . $preview_target . '>' . $display_link . "</a></span>\n";
1662-
$return .= '&lrm;'; // Fix bi-directional text display defect in RTL languages.
1663-
$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";
1664-
$return .= '<span id="editable-post-name-full">' . $post_name . "</span>\n";
1665-
}
1611+
add_filter( 'get_sample_permalink_html', array( $this, 'fix_get_sample_permalink_html' ), 10, 5 );
16661612

1667-
return $return;
1613+
return $sample_permalink_html;
16681614
}
16691615

16701616

0 commit comments

Comments
 (0)