Skip to content

Commit

Permalink
Bug with WPML and Use directory for default language
Browse files Browse the repository at this point in the history
  • Loading branch information
samiahmedsiddiqui committed Aug 4, 2020
1 parent 01db834 commit 87760ea
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 18 deletions.
41 changes: 36 additions & 5 deletions admin/class-custom-permalinks-posttypes.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private function post_permalinks()
$post_html = '';
$request_uri = '';
$search_permalink = '';
$site_url = site_url();

if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$request_uri = $_SERVER['REQUEST_URI'];
Expand Down Expand Up @@ -136,15 +137,15 @@ private function post_permalinks()
$count_query = "SELECT COUNT(p.ID) AS total_permalinks FROM $wpdb->posts AS p LEFT JOIN $wpdb->postmeta AS pm ON (p.ID = pm.post_id) WHERE pm.meta_key = 'custom_permalink' AND pm.meta_value != '' " . $filter_permalink . "";
$count_posts = $wpdb->get_row( $count_query );

$post_html .= '<form action="' . $home_url . $request_uri . '" method="get">' .
$post_html .= '<form action="' . $site_url . $request_uri . '" method="get">' .
'<p class="search-box">' .
'<input type="hidden" name="page" value="cp-post-permalinks" />' .
$filter_options .
'<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>' .
'<input type="search" id="custom-permalink-search-input" name="s" value="' . $search_value . '">' .
'<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>' .
'</form>' .
'<form action="' . $home_url . $request_uri . '" method="post">' .
'<form action="' . $site_url . $request_uri . '" method="post">' .
'<div class="tablenav top">' .
'<div class="alignleft actions bulkactions">' .
'<label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>' .
Expand Down Expand Up @@ -203,22 +204,52 @@ private function post_permalinks()
'<thead>' . $table_navigation . '</thead>' .
'<tbody>';
if ( 0 != $posts && ! empty( $posts ) ) {
$cp_frontend = new Custom_Permalinks_Frontend();
if ( class_exists( 'SitePress' ) ) {
$wpml_lang_format = apply_filters( 'wpml_setting', 0,
'language_negotiation_type'
);

if ( 1 === intval( $wpml_lang_format ) ) {
$home_url = $site_url;
}
}

foreach ( $posts as $post ) {
$custom_permalink = '/' . $post->meta_value;
$post_type = 'post';
if ( isset( $post->post_type ) ) {
$post_type = $post->post_type;
}

$language_code = apply_filters( 'wpml_element_language_code', null,
array(
'element_id' => $post->ID,
'element_type' => $post_type
)
);

$permalink = $cp_frontend->wpml_permalink_filter( $custom_permalink,
$language_code
);
$permalink = $cp_frontend->remove_double_slash( $permalink );
$perm_text = str_replace( $home_url, '', $permalink );

$post_html .= '<tr valign="top">' .
'<th scope="row" class="check-column">' .
'<input type="checkbox" name="permalink[]" value="' . $post->ID . '" />' .
'</th>' .
'<td>' .
'<strong>' .
'<a class="row-title" href="' . $home_url . '/wp-admin/post.php?action=edit&post=' . $post->ID . '">' .
'<a class="row-title" href="' . $site_url . '/wp-admin/post.php?action=edit&post=' . $post->ID . '">' .
$post->post_title .
'</a>' .
'</strong>' .
'</td>' .
'<td>' . ucwords( $post->post_type ) . '</td>' .
'<td>' .
'<a href="' . $home_url . '/' . $post->meta_value . '" target="_blank" title="' . __( "Visit " . $post->post_title, "custom-permalinks" ) . '">/' .
urldecode( $post->meta_value ) .
'<a href="' . $permalink . '" target="_blank" title="' . __( "Visit " . $post->post_title, "custom-permalinks" ) . '">' .
$perm_text .
'</a>' .
'</td>' .
'</tr>';
Expand Down
37 changes: 33 additions & 4 deletions admin/class-custom-permalinks-taxonomies.php
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ private function taxonomies_permalinks()
$home_url = home_url();
$page_html = '';
$request_uri = '';
$site_url = site_url();

if ( isset( $_SERVER['REQUEST_URI'] ) ) {
$request_uri = $_SERVER['REQUEST_URI'];
Expand Down Expand Up @@ -111,14 +112,14 @@ private function taxonomies_permalinks()
$pager_offset = 20 * ( $_GET['paged'] - 1 );
$page_limit = $pager_offset + 20;
}
$page_html .= '<form action="' . $home_url . $request_uri . '" method="get">' .
$page_html .= '<form action="' . $site_url . $request_uri . '" method="get">' .
'<p class="search-box">' .
'<input type="hidden" name="page" value="cp-category-permalinks" />' .
'<label class="screen-reader-text" for="custom-permalink-search-input">Search Custom Permalink:</label>' .
'<input type="search" id="custom-permalink-search-input" name="s" value="' . $search_value . '">' .
'<input type="submit" id="search-submit" class="button" value="Search Permalink"></p>' .
'</form>' .
'<form action="' . $home_url . $request_uri . '" method="post">' .
'<form action="' . $site_url . $request_uri . '" method="post">' .
'<div class="tablenav top">' .
'<div class="alignleft actions bulkactions">' .
'<label for="bulk-action-selector-top" class="screen-reader-text">Select bulk action</label>' .
Expand Down Expand Up @@ -183,6 +184,17 @@ private function taxonomies_permalinks()
'<tbody>';

if ( $table && is_array( $table ) && 0 < $count_tags ) {
$cp_frontend = new Custom_Permalinks_Frontend();
if ( class_exists( 'SitePress' ) ) {
$wpml_lang_format = apply_filters( 'wpml_setting', 0,
'language_negotiation_type'
);

if ( 1 === intval( $wpml_lang_format ) ) {
$home_url = $site_url;
}
}

uasort( $table, array( $this, 'sort_array' ) );
$loopCount = -1;
foreach ( $table as $permalink => $info ) {
Expand All @@ -200,17 +212,34 @@ private function taxonomies_permalinks()
$type = 'post_tag';
}

$language_code = apply_filters( 'wpml_element_language_code',
null, array(
'element_id' => $info['id'],
'element_type' => $type
)
);

$permalink = $cp_frontend->wpml_permalink_filter( $permalink,
$language_code
);
$permalink = $cp_frontend->remove_double_slash( $permalink );
$perm_text = str_replace( $home_url, '', $permalink );

$term = get_term( $info['id'], $type );
$page_html .= '<tr valign="top">' .
'<th scope="row" class="check-column">' .
'<input type="checkbox" name="permalink[]" value="' . $info['id'] . '" />' .
'</th>' .
'<td><strong>' .
'<a class="row-title" href="' . $home_url . '/wp-admin/edit-tags.php?action=edit&taxonomy=' . $type . '&tag_ID=' . $info['id'] . ' ">' . $term->name . '</a>' .
'<a class="row-title" href="' . $site_url . '/wp-admin/edit-tags.php?action=edit&taxonomy=' . $type . '&tag_ID=' . $info['id'] . ' ">' .
$term->name .
'</a>' .
'</strong></td>' .
'<td>' . ucwords( $info['kind'] ) . '</td>' .
'<td>' .
'<a href="' . $home_url . '/' . $permalink . '" target="_blank" title="' . __( "Visit " . $term->name, "custom-permalinks" ) . '">/' . $permalink . '</a>' .
'<a href="' . $permalink . '" target="_blank" title="' . __( "Visit " . $term->name, "custom-permalinks" ) . '">' .
$perm_text .
'</a>' .
'</td>' .
'</tr>';
}
Expand Down
31 changes: 22 additions & 9 deletions frontend/class-custom-permalinks-frontend.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,14 @@ public function init()
/**
* Replace double slash `//` with single slash `/`.
*
* @since 2.0.0
* @access private
* @since 1.6.0
* @access public
*
* @param string $permalink URL in which `//` needs to be replaced with `/`.
*
* @return string URL with single slash.
*/
private function remove_double_slash( $permalink = '' )
public function remove_double_slash( $permalink = '' )
{
$protocol = '';
if ( 0 === strpos( $permalink, 'http://' )
Expand All @@ -80,15 +80,15 @@ private function remove_double_slash( $permalink = '' )
* Use `wpml_permalink` to add language information to permalinks and
* resolve language switcher issue if found.
*
* @since 2.0.0
* @access private
* @since 1.6.0
* @access public
*
* @param string $permalink Custom Permalink.
* @param string $language_code The language to convert the url into.
*
* @return string permalink with language information.
*/
private function wpml_permalink_filter( $permalink = '', $language_code )
public function wpml_permalink_filter( $permalink = '', $language_code )
{
$custom_permalink = $permalink;
$trailing_permalink = trailingslashit( home_url() ) . $custom_permalink;
Expand Down Expand Up @@ -457,9 +457,10 @@ public function custom_post_link( $permalink, $post )
$permalink = $this->wpml_permalink_filter( $custom_permalink,
$language_code
);
$permalink = $this->remove_double_slash( $permalink );
}

$permalink = $this->remove_double_slash( $permalink );

return $permalink;
}

Expand Down Expand Up @@ -487,9 +488,10 @@ public function custom_page_link( $permalink, $page )
$permalink = $this->wpml_permalink_filter( $custom_permalink,
$language_code
);
$permalink = $this->remove_double_slash( $permalink );
}

$permalink = $this->remove_double_slash( $permalink );

return $permalink;
}

Expand Down Expand Up @@ -529,10 +531,11 @@ public function custom_term_link( $permalink, $term )
$permalink = $this->wpml_permalink_filter( $custom_permalink,
$language_code
);
$permalink = $this->remove_double_slash( $permalink );
}
}

$permalink = $this->remove_double_slash( $permalink );

return $permalink;
}

Expand Down Expand Up @@ -651,6 +654,16 @@ public function custom_trailingslash( $url_string )
$trailingslash_string = $url_string;
$url = parse_url( get_bloginfo( 'url' ) );

if ( class_exists( 'SitePress' ) ) {
$wpml_lang_format = apply_filters( 'wpml_setting', 0,
'language_negotiation_type'
);

if ( 1 === intval( $wpml_lang_format ) ) {
$url = parse_url( get_bloginfo( 'wpurl' ) );
}
}

if ( isset( $url['path'] ) ) {
$request = substr( $url_string, strlen( $url['path'] ) );
} else {
Expand Down

0 comments on commit 87760ea

Please sign in to comment.