Bitly create short URL from wordpress admin

Arun - 01/04/2020 10:53 AM

Files 1

functions.php
php
<?php

add_action( 'acf/save_post', 'post_published_notification', 10, 2 );
function post_published_notification( $ID ) {
	 session_start();
	global $post; 
	if(get_post_type($ID) != 'partner')
	       return; 
    $title = $post->post_title;
    $permalink = get_permalink( $ID );
    $check_short_url = get_post_meta( $ID, 'bitly_url', true); 
	if($post->post_type == 'partner' &&  $check_short_url == 'Yes')  { //remove 1==2 arun 7 aug 2019
	   	$long_url = $permalink;
		$apiv4_shorten = 'https://api-ssl.bitly.com/v4/shorten';
		$genericAccessToken = '9a97b5129f7f20b3169f7874d1720934bfe6d777';
	  
		$data = array(
		    'domain' => '8twelve.co',
		    'long_url' => $long_url
		);

		$payload = json_encode($data);
		$header = array(
		    'Authorization: Bearer '.$genericAccessToken,
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen($payload)
		);
		$ch = curl_init($apiv4_shorten);
		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
		$result = curl_exec($ch);
		$resp = json_decode($result);

		$short_link = $resp->link;
		$bitlink_id = $resp->id;

		$apiv4_custom_bitlinks = 'https://api-ssl.bitly.com/v4/custom_bitlinks';
		$custom_bitlink = '8twelve.co/'.get_post_meta($ID, 'add_custom_text', true);
		$data2 = array(
		    'bitlink_id' =>  $bitlink_id,
		    'custom_bitlink' => $custom_bitlink
		);

		$payload2 = json_encode($data2);
		$header2 = array(
		    'Authorization: Bearer '.$genericAccessToken,
		    'Content-Type: application/json',
		    'Content-Length: ' . strlen($payload2)
		);
		$ch2 = curl_init($apiv4_custom_bitlinks);
		curl_setopt($ch2, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch2, CURLOPT_POSTFIELDS, $payload2);
		curl_setopt($ch2, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch2, CURLOPT_HTTPHEADER, $header2);
		$result2 = curl_exec($ch2);
		$http_code = curl_getinfo($ch2, CURLINFO_HTTP_CODE);
		$resp2 = json_decode($result2);
		
		if($http_code == 409){
			if(!empty($resp2->errors)){
				set_transient( "bitly_check_url", "no" );
				remove_action('save_post', 'post_published_notification');
				wp_update_post(array('ID' => $ID, 'post_status' => 'draft'));
				add_action('save_post', 'post_published_notification');
			}
			
		}else{
				update_post_meta($ID, 'short_link', $short_link); 
				$custom_bitlinks_arr = $resp2->bitlink->custom_bitlinks;
				update_post_meta($ID, 'custom_bitlinks', $custom_bitlinks_arr);
				delete_transient( "bitly_check_url" );
				//add_action( 'admin_notices', 'ravs_admin_notice' );
				//remove_action('acf/save_post', 'post_published_notification');

			}
		}
}



add_action( 'admin_notices', 'bitly_show_url_error' );

function bitly_show_url_error()
{
    // Only show error message incase transient variable is set
    if ( get_transient( "bitly_check_url" ) == "no" ) {
        echo "<div id='message' class='error'><p><strong>The bitly URL that you entered already exists, please enter a different short URL.</strong></p></div>";
        delete_transient( "bitly_check_url" );
    }
}
// Simple way to Remove published post notice while error handled
add_filter( 'post_updated_messages', 'bitly_disable_publish_successful_msg' );
function bitly_disable_publish_successful_msg( $messages )
{
    return array();
}
add_action('add_meta_boxes', 'add_contact_form_meta_box');

function add_contact_form_meta_box() {
    add_meta_box('bitly-url-meta-box-id', 'Short URL', 'bitly_url_meta_box', 'partner', 'normal', 'low');
}

function bitly_url_meta_box($post) {
   // echo get_post_meta($post->ID, 'short_link', true);
    $custom_bitlinks = get_post_meta($post->ID, 'custom_bitlinks');
    $is_existing_bitlink = get_post_meta($post->ID, 'bitly_url');
    if($is_existing_bitlink[0] == "Use Existing URL"){
    	$existing_bitlink = get_post_meta($post->ID, 'bitly_current_short_url');
    	echo $existing_bitlink[0];
    }else{
    ?>
    <ul>
    	<?php  foreach($custom_bitlinks as $cbit) { ?>
    		<li><?php echo $cbit[0]; ?></li>
    	 <?php } ?>	
    </ul>

    <?php
    }
}

// other action - show hide something else

add_action('admin_footer', 'show_custom_box_url');

function show_custom_box_url(){
	?>
	<script type="text/javascript">
		jQuery(document).ready(function(){
			
			jQuery("ul.acf-radio-list li input[type='radio']").change(function(e){
				var current_val = e.target.value;
				if(current_val == "Yes" || current_val == "Use Existing URL"){
					jQuery("#bitly-url-meta-box-id").show(500);
				}else{
					jQuery("#bitly-url-meta-box-id").hide(500);
				}
			});
		});
	</script> 

	<?
}