From 4f2783e9f099e2c5eee91712a5ea2359de52e1ca Mon Sep 17 00:00:00 2001 From: Nicolas Date: Wed, 25 Sep 2019 18:17:00 +0200 Subject: [PATCH] renew the shorturl if permalink is changed. --- lib/admin.php | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/admin.php b/lib/admin.php index 6bc2d69..38ccdad 100644 --- a/lib/admin.php +++ b/lib/admin.php @@ -212,20 +212,25 @@ public function yourls_on_save( $post_id ) { return; } - // check for a link and bail if one exists - if ( false !== $exist = YOURLSCreator_Helper::get_yourls_meta( $post_id ) ) { - return; - } - // get my post URL and title - $url = YOURLSCreator_Helper::prepare_api_link( $post_id ); + $url = esc_url( YOURLSCreator_Helper::prepare_api_link( $post_id ) ); $title = get_the_title( $post_id ); // and optional keyword $keywd = ! empty( $_POST['yourls-keyw'] ) ? YOURLSCreator_Helper::prepare_api_keyword( $_POST['yourls-keyw'] ) : ''; // set my args for the API call - $args = array( 'url' => esc_url( $url ), 'title' => sanitize_text_field( $title ), 'keyword' => $keywd ); + $args = array( 'url' => $url, 'title' => sanitize_text_field( $title ), 'keyword' => $keywd ); + + // bail if shorturl exists AND longurl is the same + if ( false !== $shorturl = YOURLSCreator_Helper::get_yourls_meta( $post_id ) ) { + // make the API call + $args['shorturl'] = $shorturl; + $expanded = YOURLSCreator_Helper::run_yourls_api_call( 'expand', $args ); + if ( false !== $expanded['success'] && ! empty( $expanded['data']['shorturl'] ) && $expanded['data']['longurl'] === $url ) { + return; + } + } // make the API call $build = YOURLSCreator_Helper::run_yourls_api_call( 'shorturl', $args );