Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

renew the shorturl if permalink is changed #11

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 12 additions & 7 deletions lib/admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 );
Expand Down