Skip to content

Commit

Permalink
Merge pull request #1805 from kprajapatii/master
Browse files Browse the repository at this point in the history
GD listings author page not working when author permalink contains custom link - FIXED
  • Loading branch information
Stiofan authored Jul 13, 2021
2 parents d4d7937 + 6965ecd commit df0315e
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 48 deletions.
5 changes: 5 additions & 0 deletions change_log.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
= 2.1.0.18 =
* GD listings author page not working when author permalink contains custom link - FIXED
* Compatibility changes for Jetpack v8.1+ - CHANGED
* Map markers for custom loop filtering current location on single listing page - FIXED

= 2.1.0.17 =
* Complianz | GDPR/CCPA Cookie Consent plugin integration added for GD maps - ADDED
* Show admin notices on GD pages templates for Divi Builder - ADDED
Expand Down
2 changes: 1 addition & 1 deletion includes/admin/class-geodir-admin-post-view.php
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,7 @@ public static function owner_meta_box() {
);
?>
<label class="screen-reader-text" for="post_author_override"><?php _e('User', 'geodirectory'); ?></label>
<select class="geodir-user-search" name="post_author_override" id="post_author_override" data-placeholder="<?php esc_attr_e( 'Search for a user&hellip;', 'geodirectory' ); ?>" data-allow_clear="true"><option value="<?php echo esc_attr( $curent_user_id ); ?>" selected="selected"><?php echo $curent_user_name; ?><option></select>
<select class="geodir-user-search" name="post_author_override" id="post_author_override" data-placeholder="<?php esc_attr_e( 'Search for a user&hellip;', 'geodirectory' ); ?>" data-allow_clear="false"><option value="<?php echo esc_attr( $curent_user_id ); ?>" selected="selected"><?php echo $curent_user_name; ?><option></select>
<?php
}

Expand Down
22 changes: 21 additions & 1 deletion includes/admin/class-geodir-admin-tracker.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ private static function get_tracking_data() {
// Jetpack & GeoDirectory Connect
$data['jetpack_version'] = defined( 'JETPACK__VERSION' ) ? JETPACK__VERSION : 'none';
$data['jetpack_connected'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_active' ) && Jetpack::is_active() ) ? 'yes' : 'no';
$data['jetpack_is_staging'] = ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_staging_site' ) && Jetpack::is_staging_site() ) ? 'yes' : 'no';
$data['jetpack_is_staging'] = self::is_jetpack_staging_site() ? 'yes' : 'no';
//$data['connect_installed'] = class_exists( 'GeoDir_Connect_Loader' ) ? 'yes' : 'no';
//$data['connect_active'] = ( class_exists( 'GeoDir_Connect_Loader' ) && wp_next_scheduled( 'geodir_connect_fetch_service_schemas' ) ) ? 'yes' : 'no';

Expand All @@ -138,6 +138,26 @@ private static function get_tracking_data() {
return apply_filters( 'geodirectory_tracker_data', $data );
}

/**
* Check whether a site is running with Jetpack staging site environment.
*
* @since 2.1.0.18
*
* @return bool
*/
private static function is_jetpack_staging_site() {
if ( class_exists( '\Automattic\Jetpack\Status' ) ) {
// Check with Jetpack 8.1+.
$jetpack_status = new \Automattic\Jetpack\Status();

if ( is_callable( array( $jetpack_status, 'is_staging_site' ) ) ) {
return $jetpack_status->is_staging_site();
}
}

return ( class_exists( 'Jetpack' ) && is_callable( 'Jetpack::is_staging_site' ) && Jetpack::is_staging_site() );
}

/**
* Get the current theme info, theme name and version.
* @return array
Expand Down
8 changes: 5 additions & 3 deletions includes/admin/settings/class-geodir-settings-cpt.php
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ public function get_settings( $current_section = '' ) {
// author
'author_posts_private' => '0',
'author_favorites_private' => '0',
'limit_posts' => '',
// Page template
'page_add' => '0',
'page_details' => '0',
Expand Down Expand Up @@ -370,16 +371,17 @@ public function get_settings( $current_section = '' ) {
array(
'type' => 'number',
'id' => 'limit_posts',
'name' => __( 'Limit Posts', 'geodirectory' ),
'name' => __( 'Limit Posts Per User', 'geodirectory' ),
'desc' => __( 'Limit total posts allowed per user. Leave blank or enter 0 to allow unlimited posts.', 'geodirectory' ),
'std' => '',
'placeholder' => __( 'Unlimited', 'geodirectory' ),
'value' => ( (int) $post_type_values['limit_posts'] === 0 ? '' : ( (int) $post_type_values['limit_posts'] < 0 ? -1 : (int) $post_type_values['limit_posts'] ) ),
'custom_attributes' => array(
'min' => '-1',
'step' => '1'
),
'desc_tip' => true,
'advanced' => true,
'value' => ( isset( $post_type_values['limit_posts'] ) && $post_type_values['limit_posts'] ? (int) $post_type_values['limit_posts'] : '' )
'advanced' => true
),
array( 'type' => 'sectionend', 'id' => 'cpt_settings_author' ),

Expand Down
33 changes: 19 additions & 14 deletions includes/class-geodir-permalinks.php
Original file line number Diff line number Diff line change
Expand Up @@ -417,28 +417,36 @@ public function search_rewrite_rules(){
*
* @return array $rules.
*/
public function author_rewrite_rules( $rules ){
public function author_rewrite_rules( $rules ) {
global $wp_rewrite;

$post_types = geodir_get_posttypes( 'array' );
$saves_slug_arr = array();
if(!empty($post_types)){
foreach($post_types as $post_type => $cpt){

$cpt_slug = isset($cpt['rewrite']['slug']) ? $cpt['rewrite']['slug'] : '';
if ( ! empty( $post_types ) ) {
$author_rewrite_base = $wp_rewrite->author_base . "/([^/]+)";

// The author permalink structure
$author_permastruct = $wp_rewrite->get_author_permastruct();
if ( ! empty( $author_permastruct ) ) {
$author_rewrite_base = trim( str_replace( '%author%', "([^/]+)", $author_permastruct ), "/" );
}

foreach ( $post_types as $post_type => $cpt ) {
$cpt_slug = isset( $cpt['rewrite']['slug'] ) ? $cpt['rewrite']['slug'] : '';
$saves_slug = self::favs_slug( $cpt_slug );

// add CPT author rewrite rules
$this->add_rewrite_rule("^".$wp_rewrite->author_base."/([^/]+)/$cpt_slug/?$",'index.php?author_name=$matches[1]&post_type='.$post_type,'top');
$this->add_rewrite_rule("^".$wp_rewrite->author_base."/([^/]+)/$cpt_slug/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&post_type='.$post_type.'&paged=$matches[2]','top');
// Add CPT author rewrite rules
$this->add_rewrite_rule("^" . $author_rewrite_base . "/$cpt_slug/?$",'index.php?author_name=$matches[1]&post_type='.$post_type,'top');
$this->add_rewrite_rule("^" . $author_rewrite_base . "/$cpt_slug/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&post_type='.$post_type.'&paged=$matches[2]','top');

// favs
if(!isset($saves_slug_arr[$saves_slug])){ // only add this once unless the favs slug changes per CPT
$this->add_rewrite_rule("^".$wp_rewrite->author_base."/([^/]+)/$saves_slug/?$",'index.php?author_name=$matches[1]&gd_favs=1');
$this->add_rewrite_rule("^".$wp_rewrite->author_base."/([^/]+)/$saves_slug/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&gd_favs=1&paged=$matches[2]','top');
$this->add_rewrite_rule("^" . $author_rewrite_base . "/$saves_slug/?$",'index.php?author_name=$matches[1]&gd_favs=1');
$this->add_rewrite_rule("^" . $author_rewrite_base . "/$saves_slug/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&gd_favs=1&paged=$matches[2]','top');
}
$this->add_rewrite_rule("^".$wp_rewrite->author_base."/([^/]+)/$saves_slug/$cpt_slug/?$",'index.php?author_name=$matches[1]&gd_favs=1&post_type='.$post_type,'top');
$this->add_rewrite_rule("^".$wp_rewrite->author_base."/([^/]+)/$saves_slug/$cpt_slug/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&gd_favs=1&post_type='.$post_type.'&paged=$matches[2]','top');
$this->add_rewrite_rule("^" . $author_rewrite_base . "/$saves_slug/$cpt_slug/?$",'index.php?author_name=$matches[1]&gd_favs=1&post_type='.$post_type,'top');
$this->add_rewrite_rule("^" . $author_rewrite_base . "/$saves_slug/$cpt_slug/page/?([0-9]{1,})/?$",'index.php?author_name=$matches[1]&gd_favs=1&post_type='.$post_type.'&paged=$matches[2]','top');

// Translate slug
do_action( 'geodir_permalinks_author_rewrite_rule', $post_type, $cpt, $this, $cpt_slug, $saves_slug, $saves_slug_arr );
Expand All @@ -450,9 +458,6 @@ public function author_rewrite_rules( $rules ){
return $rules;
}




/**
* Returns permalink structure using post link.
*
Expand Down
10 changes: 6 additions & 4 deletions includes/class-geodir-post-data.php
Original file line number Diff line number Diff line change
Expand Up @@ -1539,12 +1539,14 @@ public static function check_logged_out_author( $post_data ) {

// Check posts limit.
$args = array( 'post_type' => $post_data['post_type'], 'post_author' => $post_author );
$can_add_post = GeoDir_Post_Limit::user_can_add_post( $args );
if ( ! empty( $post_data['package_id'] ) ) {
$args['package_id'] = (int) $post_data['package_id'];
}

if ( ! $can_add_post ) {
$message = GeoDir_Post_Limit::posts_limit_message( $post_data['post_type'], $post_author );
$can_add_post = GeoDir_Post_Limit::user_can_add_post( $args, true );

$error = new WP_Error( 'add_listing_error', $message, array( 'status' => 400 ) );
if ( is_wp_error( $can_add_post ) ) {
$error = new WP_Error( 'add_listing_error', $can_add_post->get_error_message(), array( 'status' => 400 ) );
}
}
}
Expand Down
112 changes: 88 additions & 24 deletions includes/class-geodir-post-limit.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,37 @@ public static function rest_api_init() {
}
}

public static function cpt_posts_limit( $post_type, $post_author = 0 ) {
$post_type_info = geodir_get_posttype_info( $post_type );
public static function cpt_posts_limit( $args ) {
$defaults = array(
'post_type' => '',
'post_author' => 0
);

$args = wp_parse_args( $args, $defaults );

$post_type_info = geodir_get_posttype_info( $args['post_type'] );
$limit = 0;

if ( ! empty( $post_type_info ) && isset( $post_type_info['limit_posts'] ) ) {
$limit = (int) $post_type_info['limit_posts'];
}

return (int) apply_filters( 'geodir_cpt_posts_limit', $limit, $post_type, $post_author );
return (int) apply_filters( 'geodir_cpt_posts_limit', $limit, $args );
}

public static function cpt_posts_limits( $args = array() ) {
$defaults = array(
'post_type' => '',
'post_author' => 0
);

$params = wp_parse_args( $args, $defaults );

$limits = array(
'total' => (int) self::cpt_posts_limit( $params )
);

return apply_filters( 'geodir_cpt_posts_limits', $limits, $params );
}

public static function count_user_cpt_posts( $args = array() ) {
Expand Down Expand Up @@ -105,7 +126,7 @@ public static function count_user_cpt_posts( $args = array() ) {
return apply_filters( 'geodir_count_user_cpt_posts', $count, $query_args, $args );
}

public static function user_can_add_post( $args = array() ) {
public static function user_can_add_post( $args = array(), $wp_error = false ) {
global $wpdb;

$defaults = array(
Expand All @@ -129,47 +150,90 @@ public static function user_can_add_post( $args = array() ) {

if ( ! current_user_can( 'manage_options' ) ) {
if ( ! empty( $params['post_author'] ) && ! user_can( (int) $params['post_author'], 'manage_options' ) ) {
$posts_limit = (int) self::cpt_posts_limit( $params['post_type'], $params['post_author'] );
$message = '';
$posts_count = 0;
$posts_limits = self::cpt_posts_limits( $params );
$posts_limit = ! empty( $posts_limits['total'] ) ? (int) $posts_limits['total'] : 0;

if ( $posts_limit > 0 ) {
$posts_count = (int) self::count_user_cpt_posts( $params );

// Limit exceed.
if ( $posts_limit <= $posts_count ) {
$can_add = false;
$message = wp_sprintf( __( 'You have reached the limit of %s you can add at this time.', 'geodirectory' ), geodir_strtolower( geodir_post_type_name( $params['post_type'], true ) ) );
}
} else if ( $posts_limit < 0 ) {
$can_add = false; // Disabled from CPT
// Disabled from CPT
$can_add = false;
$message = wp_sprintf( __( 'You are not allowed to add the listing under %s.', 'geodirectory' ), geodir_strtolower( geodir_post_type_name( $params['post_type'], true ) ) );
}

if ( $can_add === false && $wp_error && $message ) {
$message = apply_filters( 'geodir_user_posts_limit_message', $message, $posts_limit, $posts_count, $posts_limits, $params );

if ( $message ) {
$can_add = new WP_Error( 'geodir_user_posts_limit', $message );
}
}

$can_add = apply_filters( 'geodir_check_user_posts_limits', $can_add, $posts_limits, $params, $args, $wp_error );
}
}

return apply_filters( 'geodir_user_can_add_post', $can_add, $params, $args );
return apply_filters( 'geodir_user_can_add_post', $can_add, $params, $args, $wp_error );
}

public static function posts_limit_message( $post_type, $post_author = 0 ) {
$posts_limit = (int) self::cpt_posts_limit( $post_type, $post_author );
$post_type_name = geodir_strtolower( geodir_post_type_name( $post_type ) );
public static function posts_limit_message( $args = array() ) {
$defaults = array(
'post_type' => '',
'post_author' => 0
);

$args = wp_parse_args( $args, $defaults );

$posts_limits = self::cpt_posts_limits( $args );
$post_type_name = geodir_strtolower( geodir_post_type_name( $args['post_type'] ) );

if ( $posts_limit < 0 ) {
if ( isset( $posts_limits['total'] ) && (int) $posts_limits['total'] < 0 ) {
$message = wp_sprintf( __( 'You are not allowed to add the listing under %s.', 'geodirectory' ), $post_type_name );
} else {
$message = wp_sprintf( __( 'You have reached the limit of %s you can add at this time.', 'geodirectory' ), $post_type_name );
}

return apply_filters( 'geodir_user_posts_limit_message', $message, $post_type, $posts_limit );
return apply_filters( 'geodir_user_posts_limit_message', $message, $args, $posts_limits );
}

public static function check_add_listing_output( $output, $args, $widget_args, $content ) {
if ( ! is_admin() && geodir_is_page( 'add-listing' ) && ! geodir_is_page( 'edit-listing' ) ) {
$post_type = geodir_get_current_posttype();

$can_add_post = self::user_can_add_post( array( 'post_type' => $post_type ) );
$_args = array(
'post_type' => $post_type,
'post_author' => null,
'group' => 'add'
);

if ( ! $can_add_post ) {
$message = geodir_notification( array( 'add_listing_error' => self::posts_limit_message( $post_type, (int) get_current_user_id() ) ) );
$package = geodir_get_post_package( array(), $post_type );

$output = apply_filters( 'geodir_posts_limit_add_listing_message', $message, $post_type );
if ( ! empty( $package ) && ! empty( $package->id ) ) {
$_args['package_id'] = $package->id;
}

$can_add_post = self::user_can_add_post( $_args, true );

if ( is_wp_error( $can_add_post ) ) {
if ( geodir_design_style() ) {
$message = aui()->alert( array(
'type' => 'info',
'content' => $can_add_post->get_error_message(),
'class' => 'mb-0'
) );
} else {
$message = geodir_notification( array( 'add_listing_error' => $can_add_post->get_error_message() ) );
}

$output = apply_filters( 'geodir_posts_limit_add_listing_message', $message, $post_type, $_args );
}
}

Expand All @@ -178,16 +242,16 @@ public static function check_add_listing_output( $output, $args, $widget_args, $

public static function check_rest_api_post( $prepared_post, $request ) {
if ( empty( $prepared_post->ID ) ) {
$args = array( 'post_type' => $prepared_post->post_type );
if ( ! empty( $prepared_post->post_author ) ) {
$args['post_author'] = absint( $prepared_post->post_author );
}
$args = array(
'post_type' => $prepared_post->post_type,
'post_author' => ! empty( $prepared_post->post_author ) ? absint( $prepared_post->post_author ) : null,
'package_id' => ! empty( $prepared_post->package_id ) ? absint( $prepared_post->package_id ) : 0
);

$can_add_post = self::user_can_add_post( $args );
if ( ! $can_add_post ) {
$message = self::posts_limit_message( $prepared_post->post_type, ( isset( $args['post_author'] ) ? $args['post_author'] : 0 ) );
$can_add_post = self::user_can_add_post( $args, true );

return new WP_Error( 'rest_posts_limit', $message, array( 'status' => 400 ) );
if ( is_wp_error( $can_add_post ) ) {
return new WP_Error( 'rest_posts_limit', $can_add_post->get_error_message(), array( 'status' => 400 ) );
}
}

Expand Down
2 changes: 1 addition & 1 deletion includes/widgets/class-geodir-widget-map.php
Original file line number Diff line number Diff line change
Expand Up @@ -583,7 +583,7 @@ public function output( $args = array(), $widget_args = array(), $content = '' )
}

// post map
if ( $map_args['map_type'] == 'post' ) {
if ( $map_args['map_type'] == 'post' || ( ! empty( $map_args['tags'] ) && is_scalar( $map_args['tags'] ) && ( strpos( $map_args['tags'], '.' ) === 0 || strpos( $map_args['tags'], '#' ) === 0 ) ) ) {
$map_args['country'] = '';
$map_args['region'] = '';
$map_args['city'] = '';
Expand Down

0 comments on commit df0315e

Please sign in to comment.