web-dev-qa-db-fra.com

Comment obtenir une valeur d'un plugin dans un autre plugin via une action / un filtre

J'ai un plugin 'raccourcisseur d'url' qui raccourcit les URL. J'ai un autre plugin "publication automatique des médias sociaux" pour publier automatiquement une publication sur les médias sociaux. Mais ce plugin ne me permet pas d'utiliser des URL courtes, mais utilise à la place des permaliens qui sont définis comme 'domain.tld/postname /'. Maintenant, ce que je veux, c'est utiliser le lien court généré par le 1er plugin ci-dessus pour être utilisé dans le 2e plugin lors de la publication de l'article. Comme je suis totalement naïf dans la programmation/codage, toute aide est appréciée. C'est le fichier que je pense qui génère des crochets shorlink et action/filtre: pub_get_shortlink.php

<?php
global $wp_query;
$shortlink = '';
$options = $this->plugin_option;
/*
    Ensure shortener is loaded.. 
    i.e all options are set correctly
    Abort otherwise
*/
if ( !$this->shortener_loader() ){
    return '';
}

/*
    Page type switches
*/
if ($context == 'query') {
    if ( is_singular() ) {
        $id = $wp_query->get_queried_object_id();
        $context = 'post';
    } elseif ( is_front_page() ) {
        $context = 'blog';
    } else {
        return '';
    }
} 
$home_url = get_option('home');
/*
    If Homepage
*/
if ($context == 'blog') {
    if ( empty($id) )
        $url = $home_url;

    if ( $options->get('urlservice') != 'yourls' ){
        $shortlink = $this->shortener->generate($url);
    }
    return $shortlink;    
}

/*
    If post
*/
$post = get_post($id);
if ( empty($post) ){
    return '';
}  
/*
    Set up post details
*/
$post_id = $post->ID;
$post_type = $post->post_type;
$post_status = $post->post_status;
$url = '';
$saved_url = get_post_meta($post_id, 'shorturl', true);
/*
 ************************
 *
 * Main Shortening Work
 *
 ************************
 */
//check prior generation and publish status
if (empty($saved_url) && ($post_status == 'publish' || $transition == true) ){
    //Add 'Generating' Word stub to prevent generation loops (esp. Yourls)
    update_post_meta($post_id, 'shorturl', 'Generating...');
    //Use permalinks
    if ($options->get('useslug')=='yes'){
        $url = get_permalink($post_id);
        if ($url){
            $shortlink = $this->shortener->generate($url);
        }
    //Use IDs
    }else{
        switch ($post_type){
            case 'post' : $url = $home_url."/index.php?p=".$post_id; break;
            case 'page' : $url = $home_url."/index.php?page_id=".$post_id; break;
            default : break;
        }
        if ($url){
            $shortlink = $this->shortener->generate($url);
        }
    }    
    /*
     ***********************
     * Allow other plugins to use generated shortlink (1st generation) 
     ************************
     */
    if (!empty($shortlink)){
        do_action('fts_use_shortlink', $post_id, $shortlink); 
    } 
//assign saved URL if already generated    
} elseif (!empty($saved_url)) {
    $shortlink = $saved_url;
}
//Update Custom Field
if (empty($saved_url) && !empty($shortlink)){
    update_post_meta($post_id, 'shorturl', $shortlink);
}elseif( empty($saved_url) ){
    //remove 'Generating' Word stub in case generation failed
    delete_post_meta($post_id, 'shorturl', 'Generating...');
}
//Return Nice ID if shortlink is still empty
if ($options->get('niceid') == 'yes' && empty($shortlink)){
    $shortlink = $home_url.$options->get('niceid_prefix').$post_id;
}
/*
 ***********************
 * Allow other plugins to filter output
 ************************
 */
if (!empty($shortlink)){
    apply_filters('fts_filter_shortlink', $post_id, $shortlink); 
}    
/*
 ***********************
 * Finally!
 ************************
 */
return $shortlink;
?>

Et c'est la ligne de code qui, je pense, indique au 2ème plugin le lien vers le message: $ link = get_permalink ($ postpp-> ID); Le code complet est: publish.php

<?php 
if( !defined('ABSPATH') ){ exit();}
/*add_action('publish_post', 'xyz_link_publish');
add_action('publish_page', 'xyz_link_publish');

$xyz_smap_future_to_publish=get_option('xyz_smap_std_future_to_publish');

if($xyz_smap_future_to_publish==1)
    add_action('future_to_publish', 'xyz_link_smap_future_to_publish');

function xyz_link_smap_future_to_publish($post){
    $postid =$post->ID;
    xyz_link_publish($postid);
}*/
add_action(  'transition_post_status',  'xyz_link_smap_future_to_publish', 10, 3 );

function xyz_link_smap_future_to_publish($new_status, $old_status, $post){

    if(!isset($GLOBALS['smap_dup_publish']))
        $GLOBALS['smap_dup_publish']=array();
    $postid =$post->ID;
    $get_post_meta=get_post_meta($postid,"xyz_smap",true);                           // prevent duplicate publishing
    $post_permissin=get_option('xyz_smap_post_permission');
    $post_Twitter_permission=get_option('xyz_smap_twpost_permission');
    $lnpost_permission=get_option('xyz_smap_lnpost_permission');

    if(isset($_POST['xyz_smap_post_permission']))
        $post_permissin=intval($_POST['xyz_smap_post_permission']);
    if(isset($_POST['xyz_smap_twpost_permission']))
        $post_Twitter_permission=intval($_POST['xyz_smap_twpost_permission']);
    if(isset($_POST['xyz_smap_lnpost_permission']))
        $lnpost_permission=intval($_POST['xyz_smap_lnpost_permission']);
    if(!(isset($_POST['xyz_smap_post_permission']) || isset($_POST['xyz_smap_twpost_permission']) || isset($_POST['xyz_smap_lnpost_permission']))) 
    {

        if($post_permissin == 1 || $post_Twitter_permission == 1 || $lnpost_permission == 1 ) {

            if($new_status == 'publish')
            {
                if ($get_post_meta == 1 ) {
                    return;
                }
            }
            else return;
        }
    }
    if($post_permissin == 1 || $post_Twitter_permission == 1 || $lnpost_permission == 1 )
    {
        if($new_status == 'publish')
        {
            if(!in_array($postid,$GLOBALS['smap_dup_publish'])) {
                $GLOBALS['smap_dup_publish'][]=$postid;
                xyz_link_publish($postid);
            }
        }

    }
    else return;        

}

/*$xyz_smap_include_customposttypes=get_option('xyz_smap_include_customposttypes');
$carr=explode(',', $xyz_smap_include_customposttypes);
foreach ($carr  as $cstyps ) {
    add_action('publish_'.$cstyps, 'xyz_link_publish');

}*/

function xyz_link_publish($post_ID) {

    $_POST_CPY=$_POST;
    $_POST=stripslashes_deep($_POST);

    $post_permissin=get_option('xyz_smap_post_permission');
    if(isset($_POST['xyz_smap_post_permission']))
        $post_permissin=intval($_POST['xyz_smap_post_permission']);

    $post_Twitter_permission=get_option('xyz_smap_twpost_permission');
    if(isset($_POST['xyz_smap_twpost_permission']))
        $post_Twitter_permission=intval($_POST['xyz_smap_twpost_permission']);

    $lnpost_permission=get_option('xyz_smap_lnpost_permission');
    if(isset($_POST['xyz_smap_lnpost_permission']))
        $lnpost_permission=intval($_POST['xyz_smap_lnpost_permission']);

    if (($post_permissin != 1)&&($post_Twitter_permission != 1)&&($lnpost_permission != 1)) {
        $_POST=$_POST_CPY;
        return ;

    } else if (isset($_POST['_inline_edit']) AND (get_option('xyz_smap_default_selection_edit') == 0) ) {
        $_POST=$_POST_CPY;
        return;
    }



    $get_post_meta=get_post_meta($post_ID,"xyz_smap",true);
    if($get_post_meta!=1)
        add_post_meta($post_ID, "xyz_smap", "1");

    global $current_user;
    wp_get_current_user();
    $af=get_option('xyz_smap_af');


/////////////Twitter//////////
    $tappid=get_option('xyz_smap_twconsumer_id');
    $tappsecret=get_option('xyz_smap_twconsumer_secret');
    $twid=get_option('xyz_smap_tw_id');
    $taccess_token=get_option('xyz_smap_current_twappln_token');
    $taccess_token_secret=get_option('xyz_smap_twaccestok_secret');
    $messagetopost=get_option('xyz_smap_twmessage');
    if(isset($_POST['xyz_smap_twmessage']))
        $messagetopost=$_POST['xyz_smap_twmessage'];
    $appid=get_option('xyz_smap_application_id');


    $post_Twitter_image_permission=get_option('xyz_smap_twpost_image_permission');
    if(isset($_POST['xyz_smap_twpost_image_permission']))
        $post_Twitter_image_permission=intval($_POST['xyz_smap_twpost_image_permission']);
        ////////////////////////

    ////////////fb///////////
    $app_name=get_option('xyz_smap_application_name');
    $appsecret=get_option('xyz_smap_application_secret');
    $useracces_token=get_option('xyz_smap_fb_token');


    $message=get_option('xyz_smap_message');
    if(isset($_POST['xyz_smap_message']))
        $message=$_POST['xyz_smap_message'];
    //$fbid=get_option('xyz_smap_fb_id');



    $posting_method=get_option('xyz_smap_po_method');
    if(isset($_POST['xyz_smap_po_method']))
        $posting_method=intval($_POST['xyz_smap_po_method']);
        //////////////////////////////

    ////////////linkedin////////////

    $lnappikey=get_option('xyz_smap_lnapikey');
    $lnapisecret=get_option('xyz_smap_lnapisecret');
    $lmessagetopost=get_option('xyz_smap_lnmessage');
    if(isset($_POST['xyz_smap_lnmessage']))
        $lmessagetopost=$_POST['xyz_smap_lnmessage'];

  $xyz_smap_ln_shareprivate=get_option('xyz_smap_ln_shareprivate'); 
  if(isset($_POST['xyz_smap_ln_shareprivate']))
  $xyz_smap_ln_shareprivate=intval($_POST['xyz_smap_ln_shareprivate']);

  $xyz_smap_ln_sharingmethod=get_option('xyz_smap_ln_sharingmethod');
  if(isset($_POST['xyz_smap_ln_sharingmethod']))
  $xyz_smap_ln_sharingmethod=intval($_POST['xyz_smap_ln_sharingmethod']);


  $post_ln_image_permission=get_option('xyz_smap_lnpost_image_permission');
  if(isset($_POST['xyz_smap_lnpost_image_permission']))
    $post_ln_image_permission=intval($_POST['xyz_smap_lnpost_image_permission']);

    $lnaf=get_option('xyz_smap_lnaf');

    $postpp= get_post($post_ID);global $wpdb;
    $reg_exUrl = "/(http|https)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
    $entries0 = $wpdb->get_results( 'SELECT user_nicename FROM '.$wpdb->prefix.'users WHERE ID='.$postpp->post_author);
    foreach( $entries0 as $entry ) {            
        $user_nicename=$entry->user_nicename;}

    if ($postpp->post_status == 'publish')
    {
        $posttype=$postpp->post_type;
        $fb_publish_status=array();
        $ln_publish_status=array();
        $tw_publish_status=array();
        if ($posttype=="page")
        {

            $xyz_smap_include_pages=get_option('xyz_smap_include_pages');
            if($xyz_smap_include_pages==0)
            {$_POST=$_POST_CPY;return;}
        }

        else if($posttype=="post")
        {
            $xyz_smap_include_posts=get_option('xyz_smap_include_posts');
            if($xyz_smap_include_posts==0)
            {
                $_POST=$_POST_CPY;return;
            }

            $xyz_smap_include_categories=get_option('xyz_smap_include_categories');
            if($xyz_smap_include_categories!="All")
            {
                $carr1=explode(',', $xyz_smap_include_categories);

                $defaults = array('fields' => 'ids');
                $carr2=wp_get_post_categories( $post_ID, $defaults );
                $retflag=1;
                foreach ($carr2 as $key=>$catg_ids)
                {
                    if(in_array($catg_ids, $carr1))
                        $retflag=0;
                }


                if($retflag==1)
                {$_POST=$_POST_CPY;return;}
            }
        }

        else
        {

            $xyz_smap_include_customposttypes=get_option('xyz_smap_include_customposttypes');
            if($xyz_smap_include_customposttypes!='')
            {
                $carr=explode(',', $xyz_smap_include_customposttypes);

                if(!in_array($posttype, $carr))
                {
                    $_POST=$_POST_CPY;return;
                }
            }
            else
            {
                $_POST=$_POST_CPY;return;
            }

        }

        include_once ABSPATH.'wp-admin/includes/plugin.php';
        $pluginName = 'bitly/bitly.php';

        if (is_plugin_active($pluginName)) {
            remove_all_filters('post_link');
        }
    $link = get_permalink($postpp->ID);

        $xyz_smap_apply_filters=get_option('xyz_smap_std_apply_filters');
        $ar2=explode(",",$xyz_smap_apply_filters);
        $con_flag=$exc_flag=$tit_flag=0;
        if(isset($ar2))
        {
            if(in_array(1, $ar2)) $con_flag=1;
            if(in_array(2, $ar2)) $exc_flag=1;
            if(in_array(3, $ar2)) $tit_flag=1;
        }

        $content = $postpp->post_content;
        if($con_flag==1)
            $content = apply_filters('the_content', $content);
        $content = html_entity_decode($content, ENT_QUOTES, get_bloginfo('charset'));
        $excerpt = $postpp->post_excerpt;
        if($exc_flag==1)
            $excerpt = apply_filters('the_excerpt', $excerpt);
        $excerpt = html_entity_decode($excerpt, ENT_QUOTES, get_bloginfo('charset'));
        $content = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $content);
        $content=  preg_replace("/\\[caption.*?\\].*?\\[.caption\\]/is", "", $content);
        $content = preg_replace('/\[.+?\]/', '', $content);
        $excerpt = preg_replace('/<script\b[^>]*>(.*?)<\/script>/is', "", $excerpt);

        if($excerpt=="")
        {
            if($content!="")
            {
                $content1=$content;
                $content1=strip_tags($content1);
                $content1=strip_shortcodes($content1);

                $excerpt=implode(' ', array_slice(explode(' ', $content1), 0, 50));
            }
        }
        else
        {
            $excerpt=strip_tags($excerpt);
            $excerpt=strip_shortcodes($excerpt);
        }
        $description = $content;

        $description_org=$description;
        $attachmenturl=xyz_smap_getimage($post_ID, $postpp->post_content);
        if($attachmenturl!="")
            $image_found=1;
        else
            $image_found=0;

        $name = $postpp->post_title;
        $xyz_smap_caption_for_fb_attachment=get_option('xyz_smap_caption_for_fb_attachment');

        if($xyz_smap_caption_for_fb_attachment==1)
            $caption=$_SERVER['HTTP_Host'];
            else
            $caption=get_bloginfo('title');
            $caption = html_entity_decode($caption, ENT_QUOTES, get_bloginfo('charset'));


        if($tit_flag==1)
            $name = apply_filters('the_title', $name);
        $name = html_entity_decode($name, ENT_QUOTES, get_bloginfo('charset'));
        $name=strip_tags($name);
        $name=strip_shortcodes($name);

        $description=strip_tags($description);      
        $description=strip_shortcodes($description);

        $description=str_replace("&nbsp;","",$description);

        $excerpt=str_replace("&nbsp;","",$excerpt);
        if($useracces_token!="" && $appsecret!="" && $appid!="" && $post_permissin==1)
        {
            $descriptionfb_li=xyz_smap_string_limit($description, 10000);

            $user_page_id=get_option('xyz_smap_fb_numericid');

            $xyz_smap_pages_ids=get_option('xyz_smap_pages_ids');
            if($xyz_smap_pages_ids=="")
                $xyz_smap_pages_ids=-1;

            $xyz_smap_pages_ids1=explode(",",$xyz_smap_pages_ids);


            foreach ($xyz_smap_pages_ids1 as $key=>$value)
            {
                if($value!=-1)
                {
                    $value1=explode("-",$value);
                    $acces_token=$value1[1];$page_id=$value1[0];
                }
                else
                {
                    $acces_token=$useracces_token;$page_id=$user_page_id;
                }

                $fb=new Facebook\Facebook(array(
                        'app_id'  => $appid,
                        'app_secret' => $appsecret,
                        'cookie' => true
                ));
                $message1=str_replace('{POST_TITLE}', $name, $message);
                $message2=str_replace('{BLOG_TITLE}', $caption,$message1);
                $message3=str_replace('{PERMALINK}', $link, $message2);
                $message4=str_replace('{POST_EXCERPT}', $excerpt, $message3);
                $message5=str_replace('{POST_CONTENT}', $description, $message4);
                $message5=str_replace('{USER_NICENAME}', $user_nicename, $message5);
                $message5=str_replace('{POST_ID}', $post_ID, $message5);
                $publish_time=get_the_time('Y/m/d',$post_ID );
                $message5=str_replace('{POST_PUBLISH_DATE}', $publish_time, $message5);
                $message5=str_replace("&nbsp;","",$message5);
               $disp_type="feed";
                if($posting_method==1) //attach
                {
                    $attachment = array('message' => $message5,
                            'access_token' => $acces_token,
                            'link' => $link,
                            'name' => $name,
                            'caption' => $caption,
                            'description' => $descriptionfb_li,
                            'actions' => json_encode(array('name' => $name,
                            'link' => $link)),
                            'picture' => $attachmenturl

                    );
                }
                else if($posting_method==2)  //share link
                {
                    $attachment = array('message' => $message5,
                            'access_token' => $acces_token,
                            'link' => $link,
                            'name' => $name,
                            'caption' => $caption,
                            'description' => $descriptionfb_li,
                            'picture' => $attachmenturl


                    );
                }
                else if($posting_method==3) //simple text message
                {

                    $attachment = array('message' => $message5,
                            'access_token' => $acces_token              

                    );

                }
                else if($posting_method==4 || $posting_method==5) //text message with image 4 - app album, 5-timeline
                {
                    if($attachmenturl!="")
                    {


                        if($posting_method==5)
                        {
                            try{
                                $album_fount=0;

                                $albums = $fb->get("/$page_id/albums", $acces_token);
                                $arrayResults = $albums->getGraphEdge()->asArray();


                            }
                            catch (Exception $e)
                            {
                                $fb_publish_status[$page_id."/albums"]=$e->getMessage();
                                    }
                            if(isset($arrayResults))
                            {
                                foreach ($arrayResults as $album) {
                                    if (isset($album["name"]) && $album["name"] == "Timeline Photos") {
                                        $album_fount=1;$timeline_album = $album; break;
                                    }
                                }
                            }
                            if (isset($timeline_album) && isset($timeline_album["id"])) $page_id = $timeline_album["id"];
                            if($album_fount==0)
                            {
                                $attachment = array('name' => "Timeline Photos",
                                        'access_token' => $acces_token,
                                );
                                try{
                                    $album_create=$fb->post('/'.$page_id.'/albums', $attachment);
                                    $album_node=$album_create->getGraphNode();
                                    if (isset($album_node) && isset($album_node["id"]))
                                        $page_id = $album_node["id"];
                                }
                                catch (Exception $e)
                                {
                                    $fb_publish_status[$page_id."/albums"]=$e->getMessage();

                                }

                            }
                        }
                        else
                        {
                            try{
                                $album_fount=0;

                                $albums = $fb->get("/$page_id/albums", $acces_token);
                                $arrayResults = $albums->getGraphEdge()->asArray();

                            }
                            catch (Exception $e)
                            {
                                $fb_publish_status[$page_id."/albums"]=$e->getMessage();                    
                            }
                            if(isset($arrayResults))
                            {
                                foreach ($arrayResults as $album)
                                {
                                    if (isset($album["name"]) && $album["name"] == $app_name) {
                                        $album_fount=1;
                                        $app_album = $album; break;
                                    }
                                }

                            }
                            if (isset($app_album) && isset($app_album["id"])) $page_id = $app_album["id"];
                            if($album_fount==0)
                            {
                                $attachment = array('name' => $app_name,
                                        'access_token' => $acces_token,
                                );
                                try{
                                    $album_create=$fb->post('/'.$page_id.'/albums', $attachment);
                                    $album_node=$album_create->getGraphNode();
                                    if (isset($album_node) && isset($album_node["id"]))
                                        $page_id = $album_node["id"];
                                }
                                catch (Exception $e)
                                {
                                    $fb_publish_status[$page_id."/albums"]=$e->getMessage();
                                }

                            }
                        }


                        $disp_type="photos";
                        $attachment = array('message' => $message5,
                                'access_token' => $acces_token,
                                'url' => $attachmenturl 

                        );
                    }
                    else
                    {
                        $attachment = array('message' => $message5,
                                'access_token' => $acces_token

                        );
                    }

                }

                if($posting_method==1 || $posting_method==2)
                {

                    $attachment=xyz_wp_fbap_attachment_metas($attachment,$link);
                    update_post_meta($post_ID, "xyz_smap_insert_og", "1");
                }
                try{
                $result = $fb->post('/'.$page_id.'/'.$disp_type.'/', $attachment);}
                            catch(Exception $e)
                            {
                                $fb_publish_status[$page_id."/".$disp_type]=$e->getMessage();
                            }

            }

            if(count($fb_publish_status)>0)
              $fb_publish_status_insert=serialize($fb_publish_status);
            else
                $fb_publish_status_insert=1;

            $time=time();
            $post_fb_options=array(
                    'postid'    =>  $post_ID,
                    'acc_type'  =>  "Facebook",
                    'publishtime'   =>  $time,
                    'status'    =>  $fb_publish_status_insert
            );

            $smap_fb_update_opt_array=array();

            $smap_fb_arr_retrive=(get_option('xyz_smap_fbap_post_logs'));

            $smap_fb_update_opt_array[0]=isset($smap_fb_arr_retrive[0]) ? $smap_fb_arr_retrive[0] : '';
            $smap_fb_update_opt_array[1]=isset($smap_fb_arr_retrive[1]) ? $smap_fb_arr_retrive[1] : '';
            $smap_fb_update_opt_array[2]=isset($smap_fb_arr_retrive[2]) ? $smap_fb_arr_retrive[2] : '';
            $smap_fb_update_opt_array[3]=isset($smap_fb_arr_retrive[3]) ? $smap_fb_arr_retrive[3] : '';
            $smap_fb_update_opt_array[4]=isset($smap_fb_arr_retrive[4]) ? $smap_fb_arr_retrive[4] : '';

            array_shift($smap_fb_update_opt_array);
            array_Push($smap_fb_update_opt_array,$post_fb_options);
            update_option('xyz_smap_fbap_post_logs', $smap_fb_update_opt_array);




        }

........... code coupé en raison de la limite

Comment puis-je obtenir $ shortlink du 1er plugin enregistrer la valeur dans $ link dans le 2ème. Thnx j'avance!

1
Vik

lorsque le lien court est créé, une action est lancée avec cette ligne:

do_action('fts_use_shortlink', $post_id, $shortlink);

alors vous pouvez accrocher votre code à l'action avec ce code, par exemple dans le 2ème plugin:

add_action("fts_use_shortlink", function ($post_id, $shortlink) {

    // here you can use $shortlink


}, 10, 2);
// 10 is the priority when there is several hooks
// 2 is here for the 2 arguments $post_id and $shortlink
1
mmm