linccce

All in one

Jul 22nd, 2016
352
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 25.82 KB | None | 0 0
  1.     use Aura\Router\Exception;
  2.     use Facebook;
  3.     use FacebookAds\Api;
  4.     use FacebookAds\Object;
  5.     use Facebook\Exceptions;
  6.     use Ip\Controller;
  7.     use Ip\Pagination\Pagination;
  8.  
  9.     /*
  10.      * @class AdvertisingController
  11.      */
  12.  
  13.     class AdvertisingController
  14.     {
  15.         private $access_token = null;
  16.         private $app_id = null;
  17.         private $app_secret = null;
  18.         static $fb_params;
  19.         public $fb_instance;
  20.         public $requests;
  21.         private $my_facebook_id;
  22.         public $current_facebook_ads_account = null;
  23.         public $current_facebook_ads_campaign = null;
  24.  
  25.         public function __construct()
  26.         {
  27.             if (ipDb()->isMySQL()) {
  28.                 try {
  29.                     $result = ipDb()->fetchAll("SELECT * FROM ip_advertising_fb_admin_data");
  30.                     foreach (current($result) as $property_name => $property_value) {
  31.                         if (property_exists(get_class($this), $property_name)) {
  32.                             $this->$property_name = $property_value;
  33.                         } elseif ($property_name == 'used_account') {
  34.                             $this->current_facebook_ads_account = $property_value;
  35.                         } elseif ($property_name == 'app_user') {
  36.                             $this->my_facebook_id = $property_value;
  37.                         }
  38.                     }
  39.                 } catch (Exception $e) {
  40.                     print_r($e);
  41.                 }
  42.  
  43.                 if (is_null($this->app_id) && is_null($this->app_secret)) {
  44.                     ipDb()->insert('advertising_fb_admin_data', array(
  45.                         'app_id' => '1406995719530360',
  46.                         'app_secret' => '046c382d24e764cca8a344a78fbc41ea'
  47.                     ));
  48.                 }
  49.             }
  50.  
  51.             $this->setFacebookParams();
  52.  
  53.             $this->fb_instance = new Facebook\Facebook(static::$fb_params);
  54.         }
  55.  
  56.         private function setFacebookParams()
  57.         {
  58.             static::$fb_params = [
  59.                 'app_id' => $this->app_id,
  60.                 'app_secret' => $this->app_secret,
  61.                 'default_graph_version' => 'v2.7',
  62.             ];
  63.         }
  64.  
  65.         public function AdvertisingList()
  66.         {
  67.             ipResponse()->setTitle(__('Cars for Adverts list', 'Advertising', false));
  68.  
  69.             if (is_null($this->access_token)) {
  70.                 $this->redirectToAdvertisingLogin();
  71.             } elseif (is_null($this->current_facebook_ads_account)) {
  72.                 $this->AdvertisingSelectAdAccount();
  73.             }
  74.  
  75.             $query = $this->getQuery();
  76.  
  77.             $inventory = new Models\AdvertisingInventory();
  78.  
  79.             $listings = $inventory->getListings($query, 20);
  80.             $totalPages = $inventory->getTotalPages(20);
  81.             $pagination = new Pagination([
  82.                 'totalPages' => $totalPages,
  83.                 'currentPage' => $query['page'],
  84.                 'pagerSize' => 20
  85.             ]);
  86.  
  87.             return ipView('view/advertisingList.php', [
  88.                 'listings' => $listings,
  89.                 'searchOptions' => $query,
  90.                 'pageSize' => 20,
  91.                 'advertising_reset' => !is_null($this->current_facebook_ads_account) ? $this->AdvertisingAccountResetLink() : '',
  92.                 'pagination' => $pagination->render(ipFile('Plugin/Advertising/view/pagination.php'))
  93.             ])->render();
  94.         }
  95.  
  96.         /***************************
  97.          * Redirect methods block
  98.          ***************************/
  99.  
  100.         private function header302()
  101.         {
  102.             header('HTTP/1.1 302 Found');
  103.         }
  104.  
  105.         private function redirectToAdvertisingList()
  106.         {
  107.             $this->header302();
  108.             header('Location: ' . ipRouteUrl('Advertising_list'));
  109.         }
  110.  
  111.         private function redirectToAdvertisingLogin()
  112.         {
  113.             $this->header302();
  114.             header('Location: ' . ipRouteUrl('Advertising_login'));
  115.         }
  116.  
  117.         /*******************************
  118.          * End of redirect methods block
  119.          *******************************/
  120.  
  121.         /*****************************
  122.          * Facebook Login Block
  123.          *****************************/
  124.  
  125.         public function AdvertisingLogin()
  126.         {
  127.             $helper = $this->fb_instance->getRedirectLoginHelper();
  128.  
  129.             $permissions = ['ads_management', 'read_insights', 'ads_read', 'manage_pages']; // optional
  130.  
  131.             $reauthentication_url = $helper->getReAuthenticationUrl('http://portal.automote.local/AdvertisingLoginCallback', $permissions);
  132.  
  133.             $loginUrl = $helper->getLoginUrl('http://portal.automote.local/AdvertisingLoginCallback', $permissions);
  134.  
  135.             ipDb()->update('advertising_fb_admin_data', array(
  136.                 'reauthentication_url' => $reauthentication_url,
  137.                 'login_url' => $loginUrl
  138.             ), array(
  139.                 'app_id' => $this->app_id
  140.             ));
  141.  
  142.             return ipView('view/advertisingFacebookLogin.php', [
  143.                 'loginUrl' => $loginUrl
  144.             ])->render();
  145.  
  146.         }
  147.  
  148.         public function AdvertisingAccountResetLink()
  149.         {
  150.             $resetUrl = ipRouteUrl('Advertising_unset_ad_account');;
  151.             return <<<HTML
  152.                 <div class="panel-group" style="position: fixed; top: 50px; left: 0; z-index: 1050">
  153.                     <div class="panel panel-default">
  154.                         <div class="panel-body">Reset Account</div>
  155.                     </div>
  156.                     <div class="panel panel-default">
  157.                         <div class="panel-body">
  158.                             <a href='$resetUrl' class='btn btn-default'><span class='glyphicon glyphicon-refresh'></span> Reset account select!</a>
  159.                             <span>Current Account selected: $this->current_facebook_ads_account</span>
  160.                         </div>
  161.                     </div>
  162.                 </div>
  163. HTML;
  164.         }
  165.  
  166.         public function AdvertisingLoginCallback()
  167.         {
  168.  
  169.             $helper = $this->fb_instance->getRedirectLoginHelper();
  170.             try {
  171.                 $accessToken = $helper->getAccessToken();
  172.             } catch (Facebook\Exceptions\FacebookResponseException $e) {
  173.                 // When Graph returns an error
  174.                 if ($e->getMessage() == 'This authorization code has been used.') {
  175.                     $this->redirectToAdvertisingList();
  176.                 } else {
  177.                     echo 'Graph returned an error: ' . $e->getMessage();
  178.                     exit;
  179.                 }
  180.             } catch (Facebook\Exceptions\FacebookSDKException $e) {
  181.                 // When validation fails or other local issues
  182.                 echo 'Facebook SDK returned an error: ' . $e->getMessage();
  183.                 exit;
  184.             }
  185.  
  186.             if (isset($accessToken)) {
  187.                 // Logged in!
  188.  
  189.                 $LongLivedAccessToken = $this->AdvertisingExtendingFacebookToken($accessToken->__toString());
  190.  
  191.                 $longLivedAccessTokenObject = new Facebook\Authentication\AccessToken($LongLivedAccessToken);
  192.  
  193.                 $this->fb_instance->setDefaultAccessToken($longLivedAccessTokenObject);
  194.  
  195.                 $request = $this->fb_instance->get('/me');
  196.  
  197.                 $app_user = $request->getDecodedBody()['id'];
  198.  
  199.                 $logout_url = $helper->getLogoutUrl($longLivedAccessTokenObject, ipRouteUrl('Advertising_list'));
  200.  
  201.  
  202.                 ipDb()->update('advertising_fb_admin_data', array(
  203.                     'access_token' => $LongLivedAccessToken,
  204.                     'app_user' => $app_user,
  205.                     'logout_url' => $logout_url
  206.                 ), array(
  207.                     'app_id' => $this->app_id
  208.                 ));
  209.  
  210.                 $this->redirectToAdvertisingList();
  211.                 // Now you can redirect to another page and use the
  212.             }
  213.         }
  214.  
  215.         private function AdvertisingExtendingFacebookToken($token)
  216.         {
  217.             // OAuth 2.0 client handler
  218.             $oAuth2Client = $this->fb_instance->getOAuth2Client();
  219.  
  220.             // Exchanges a short-lived access token for a long-lived one
  221.             return $oAuth2Client->getLongLivedAccessToken($token)->__toString();
  222.         }
  223.  
  224.         /*******************************
  225.          * End of Facebook Login Block
  226.          ******************************/
  227.  
  228.         /*****************************
  229.          * Facebook Ads Authentication Block
  230.          *****************************/
  231.  
  232.         public function AdvertisingSelectAdAccount()
  233.         {
  234.  
  235.             Api::init($this->app_id, $this->app_secret, $this->access_token);
  236.  
  237.             // Add to header of your file
  238.  
  239.             if (is_null($this->current_facebook_ads_account)) {
  240.  
  241.                 $common_facebook_ad_account_user = new Object\AdAccountUser($this->my_facebook_id);
  242.  
  243.                 $account = new Object\AdAccount(); // Getting AdAccount instance
  244.  
  245.                 $current_user_facebook_ad_accounts = $common_facebook_ad_account_user->getAdAccounts(/*$account::getFields()*/)->getArrayCopy();
  246.  
  247.                 $head = "";
  248.                 $body = "";
  249.                 $i = 0;
  250.                 foreach ($current_user_facebook_ad_accounts as $data) {
  251.                     $data = $data->exportAllData();
  252.                     $data = array_filter($data, function ($value) {
  253.                         return $value !== '' && !is_array($value);
  254.                     });
  255.                     if ($i == 0) {
  256.                         $head = "<tr><th>" . join("</th><th>", array_keys($data)) . "</th><th>Select Account</th></tr>";
  257.                     }
  258.                     $select_ad_account_link = ipRouteUrl('Advertising_set_current_facebook_ads_account', array('account_id' => $data['id']));
  259.                     $body .= "<tr><td>" . join("</td><td>", $data) . "</td><td><a href='$select_ad_account_link'>Select</a></td></tr>";
  260.                     $i++;
  261.                 }
  262.  
  263.                 $route = ipRouteUrl('Advertising_new_ad_account');
  264.                 echo <<<HTML
  265.                 <div class="container">
  266.                 <div class="well">
  267.                 <h3>List of available accounts</h3>
  268.                 <table class="table table-bordered">
  269.                 <thead>
  270.            $head
  271. </thead>
  272. <tbody>
  273.     $body
  274. </tbody>
  275. </table>
  276. <a href="$route" class="btn btn-lg btn-primary">Create new account</a>
  277. </div>
  278. </div>
  279. HTML;
  280.             } else {
  281.                 $this->redirectToAdvertisingList();
  282.             }
  283.  
  284.  
  285.         }
  286.  
  287.         /*****************************
  288.          * End Facebook Ads Authentication Block
  289.          *****************************/
  290.  
  291.         /*****************************
  292.          * Facebook Ads Controls Block
  293.          *****************************/
  294.  
  295.         /**
  296.          * @param string $current_facebook_ads_account
  297.          */
  298.         public function AdvertisingSetCurrentFacebookAdsAccount($account_id)
  299.         {
  300.             $this->current_facebook_ads_account = $account_id;
  301.             ipDb()->update('advertising_fb_admin_data', array(
  302.                 'used_account' => $account_id
  303.             ), array(
  304.                 'app_id' => $this->app_id
  305.             ));
  306.  
  307.             $this->redirectToAdvertisingList();
  308.         }
  309.  
  310.         /**
  311.          * @param string $app_id
  312.          */
  313.         public function AdvertisingSetCurrentFacebookAdsCampaign($campaign_id)
  314.         {
  315.             $this->current_facebook_ads_campaign = $campaign_id;
  316.             $_SESSION['current_facebook_ads_account'] = $campaign_id;
  317.             $this->redirectToAdvertisingList();
  318.         }
  319.  
  320.         /**
  321.          * @return null
  322.          */
  323.         public function AdvertisingUnsetAdAccount()
  324.         {
  325.             unset($this->current_facebook_ads_account);
  326.             unset($_SESSION['current_facebook_ads_account']);
  327.             ipDb()->update('advertising_fb_admin_data', array(
  328.                 'used_account' => null
  329.             ), array(
  330.                 'app_id' => $this->app_id
  331.             ));
  332.             $this->redirectToAdvertisingList();
  333.         }
  334.  
  335.         /*
  336.          * @public AdvertisingPublishItem
  337.          * @use FacebookAds\Http\Exception\RequestException
  338.          */
  339.  
  340.         public function AdvertisingPublishItem($id)
  341.         {
  342.  
  343.             $inventory = new Models\AdvertisingInventory();
  344.             $item = $inventory->getListing($id)->getAttributes();
  345.             $item['properties'] = json_decode($item['properties'], true);
  346.  
  347.             /**
  348.              * Step 1 Instantiate an API
  349.              *
  350.              * @link https://developers.facebook.com/docs/marketing-api/sdks#init-sdk
  351.              */
  352.  
  353.             try {
  354.                 Api::init($this->app_id, $this->app_secret, $this->access_token);
  355.             } catch (\Exception $e) {
  356.                 return $this->AdvertisingRenderErrorMessage($e);
  357.             }
  358.  
  359.             /**
  360.              * Step 2 Query to create campaign
  361.              * @var $campaign
  362.              * @link https://developers.facebook.com/docs/marketing-api/reference/ad-campaign-group
  363.              *
  364.              * TODO: Implement ad image widget sizing: https://www.facebook.com/business/help/103816146375741
  365.              */
  366.             try {
  367.  
  368.                 $account = new Object\AdAccount($this->current_facebook_ads_account);
  369.  
  370.                 $campaign = new Object\Campaign(null, $this->current_facebook_ads_account); // Getting AdAccount instance
  371.  
  372.                 $ad_campaigns = $account->getCampaigns($campaign::getFields())->getArrayCopy(); // Checks of there are any campaigns already created
  373.  
  374.                 $current_campaign = null;
  375.  
  376.                 if (!empty($ad_campaigns)) {
  377.                     foreach ($ad_campaigns as $ad_campaign) { // loops through campaigns
  378.                         var_dump($ad_campaign); exit();
  379.                         $campaign_data = $ad_campaign->exportAllData();
  380.                         if ($campaign_data['name'] == 'Sell old inventory') { // If campaign name matches the name given to new campaign, created campaign is then used
  381.                             $current_campaign = $ad_campaign;
  382.                         }
  383.                     }
  384.                 }
  385.  
  386.                 if (!is_null($current_campaign)) { // Checks if campaign is already assigned if yes, then its used.
  387.                     $campaign = $current_campaign;
  388.                 } else { // else new one is created
  389.                     $campaign->setData(array(
  390.                         Object\Fields\CampaignFields::NAME => 'Sell old inventory',
  391.                         Object\Fields\CampaignFields::OBJECTIVE => Object\Values\CampaignObjectiveValues::LINK_CLICKS,
  392.                     ));
  393.  
  394.                     $campaign->validate()->create(array(
  395.                         Object\Campaign::STATUS_PARAM_NAME => Object\Campaign::STATUS_PAUSED
  396.                     ));
  397.                 }
  398.  
  399.                 //$campaignData = $campaign->getData();
  400.  
  401.                 echo 'Campaign ID: ' . $campaign->id . " <br>\n";
  402.  
  403.             } catch (\Exception $e) {
  404.                 return $this->AdvertisingRenderErrorMessage($e);
  405.             }
  406.  
  407.             /**
  408.              * Step 3 Search for targeting topic
  409.              * @var $targeting
  410.              * @link https://developers.facebook.com/docs/marketing-api/targeting-specs/v2.7
  411.              */
  412.             try {
  413.                 $results = Object\TargetingSearch::search(
  414.                     Object\Search\TargetingSearchTypes::INTEREST,
  415.                     null,
  416.                     'Automobiles');
  417.  
  418.                 $target = (count($results)) ? $results->current() : null;
  419.  
  420.                 //if (!is_null($target)) {
  421.  
  422.                 echo "Using target: " . $target->name . "<br/>";
  423.  
  424.                 $targeting = new Object\Targeting();
  425.                 $targeting->{Object\Fields\TargetingFields::GEO_LOCATIONS} = array(
  426.                     'countries' => array('NZ')
  427.                 );
  428.                 $targeting->{Object\Fields\TargetingFields::INTERESTS} = array(
  429.                     array(
  430.                         'id' => $target->id,
  431.                         'name' => $target->name,
  432.                     ),
  433.                 );
  434.                 //}
  435.             } catch (\Exception $e) {
  436.                 return $this->AdvertisingRenderErrorMessage($e);
  437.             }
  438.  
  439.             /**
  440.              * Step 4 Create an AdSet
  441.              * @var $adset
  442.              * @link https://developers.facebook.com/docs/marketing-api/reference/ad-campaign
  443.              */
  444.             try {
  445.  
  446.                 $adset = new Object\AdSet(null, $this->current_facebook_ads_account);
  447.                 //$fields = $adset::getFields();
  448.  
  449.                 $available_adsets = $campaign->getAdSets(array(Object\Fields\AdSetFields::NAME,Object\Fields\AdSetFields::ID));
  450.  
  451.                 $current_adset = null;
  452.  
  453.                 if (!empty($available_adsets)) {
  454.                     foreach ($available_adsets as $ad_set) { // loops through adsets
  455.                         if ($adset->name == $item['title']) { // If adset name matches the name given to new campaign, created campaign is then used
  456.                             $current_adset = $ad_set;
  457.                         }
  458.                     }
  459.                 }
  460.  
  461.                 if (!is_null($current_adset)) {
  462.                     $adset = $current_adset;
  463.                     /*$adset->read(array(
  464.                         Object\Fields\AdSetFields::PROMOTED_OBJECT,
  465.                         Object\Fields\AdSetFields::NAME
  466.                     ));
  467.                     echo $adset->name."<br>";
  468.                     echo $adset->promoted_object."<br>";*/
  469.                 } else {
  470.  
  471.                     $start_time = (new \DateTime("+1 week"))->format(\DateTime::ISO8601);
  472.                     $end_time = (new \DateTime("+2 week"))->format(\DateTime::ISO8601);
  473.  
  474.  
  475.                     $adset->setData(array(
  476.                         Object\Fields\AdSetFields::NAME => $item['title'],
  477.                         Object\Fields\AdSetFields::OPTIMIZATION_GOAL => Object\Values\AdSetOptimizationGoalValues::LINK_CLICKS,
  478.                         Object\Fields\AdSetFields::BILLING_EVENT => Object\Values\AdSetBillingEventValues::LINK_CLICKS,
  479.                         Object\Fields\AdSetFields::BID_AMOUNT => 100,
  480.                         Object\Fields\AdSetFields::DAILY_BUDGET => 500,
  481.                         Object\Fields\AdSetFields::CAMPAIGN_ID => $campaign->id,
  482.                         Object\Fields\AdSetFields::TARGETING => $targeting,
  483.                         Object\Fields\AdSetFields::START_TIME => $start_time,
  484.                         Object\Fields\AdSetFields::END_TIME => $end_time,
  485.                         /*Object\Fields\AdSetFields::PROMOTED_OBJECT => (object) array(
  486.                             Object\Fields\AdPromotedObjectFields::PAGE_ID => '178698438995498',
  487.                         )*/
  488.                     ));
  489.                     $adset->create(array(
  490.                         Object\AdSet::STATUS_PARAM_NAME => Object\AdSet::STATUS_PAUSED,
  491.                     ));
  492.                 }
  493.                 echo 'Adset ID: ' . $adset->id . " <br>\n";
  494.             } catch (\Exception $e) {
  495.                 return $this->AdvertisingRenderErrorMessage($e);
  496.             }
  497.  
  498.  
  499.             //New method
  500.             /**
  501.              * Step 5 Create an AdImage
  502.              * @var $image
  503.              * @link https://developers.facebook.com/docs/marketing-api/reference/ad-image
  504.              */
  505.             try {
  506.                 $image = new Object\AdImage(null, $this->current_facebook_ads_account); // Invokes images preparation for Facebook ad instance
  507.                 $target_path = __DIR__ . '/assets/temp/'; // path where images will be streamed to
  508.  
  509.                 if (!file_exists($target_path)) {
  510.                     mkdir($target_path, 0755); // creating path if one doesn't exist
  511.                 }
  512.  
  513.                 $files_in_dir = array_diff(scandir($target_path), array('..', '.')); // checking for existing files in target directory
  514.                 $size = getimagesize($item['properties']['images_large'][0]); // getting image details
  515.                 $extension = image_type_to_extension($size[2]); // getting image extension from type
  516.                 $target_file_name = preg_replace(['/\./', '/\\s/', '/\\t/'], "_", strtolower($item['title'])) . $extension; // creating proper file name
  517.                 $target_file = $target_path . $target_file_name; // setting full target directory
  518.  
  519.                 if (array_search($target_file_name, $files_in_dir) === false) { // checking if target file already exists
  520.  
  521.                     $image_file = file_get_contents($item['properties']['images_large'][0]);
  522.  
  523.                     file_put_contents($target_file, $image_file);
  524.                 }
  525.  
  526.                 $image->{Object\Fields\AdImageFields::FILENAME} = $target_file; // Using target file as ad image
  527.  
  528.                 $image->create();
  529.  
  530.                 echo 'Image Hash: ' . $image->hash . " <br>\n";
  531.             } catch (\Exception $e) {
  532.                 return $this->AdvertisingRenderErrorMessage($e);
  533.             }
  534.  
  535.             /**
  536.              * Step 6 Create an AdCreative
  537.              */
  538.             try {
  539.  
  540.                 $creative = new Object\AdCreative(null, $this->current_facebook_ads_account);
  541.  
  542.                 $available_creatives = $adset->getAdCreatives($creative::getFields());
  543.  
  544.                 $current_creative = null;
  545.  
  546.                 if (!empty($available_creatives)) {
  547.                     foreach ($available_creatives as $creative) { // loops through adsets
  548.                         if ($creative->name == 'Creative of '.$item['title']) { // If adset name matches the name given to new campaign, created campaign is then used
  549.                             $current_creative = $creative;
  550.                         }
  551.                     }
  552.                 }
  553.  
  554.                 if (!is_null($current_creative)) {
  555.                     $creative = $current_creative;
  556.                 } else {
  557.  
  558.                     $creative = new Object\AdCreative(null, $this->current_facebook_ads_account);
  559.                     $creative->setData(array(
  560.                         Object\Fields\AdCreativeFields::NAME => 'Creative of '.$item['title'],
  561.                         Object\Fields\AdCreativeFields::TITLE => $item['title'],
  562.                         Object\Fields\AdCreativeFields::BODY => $item['properties']['description'],
  563.                         Object\Fields\AdCreativeFields::IMAGE_HASH => $image->hash,
  564.                         Object\Fields\AdCreativeFields::LINK_URL => ipConfig()->get('globalPortalUrl'),
  565.                         Object\Fields\AdCreativeFields::OBJECT_URL => ipConfig()->get('globalPortalUrl'),
  566.                     ));
  567.                     $creative->create();
  568.                 }
  569.                 echo 'Creative ID: ' . $creative->id . " <br>\n";
  570.             } catch (\Exception $e) {
  571.                 return $this->AdvertisingRenderErrorMessage($e);
  572.             }
  573.  
  574.  
  575.             /**
  576.              * Step 7 Create an Ad
  577.              *
  578.              * Then, use the image hash returned from above. Finally, create your ad along with ad creative.
  579.              * Please note that the ad creative is not created independently, rather its data structure
  580.              * is appended to the ad group
  581.              *
  582.              * @link https://developers.facebook.com/docs/marketing-api/reference/adgroup
  583.              */
  584.             try {
  585.  
  586.                 $ad = new Object\Ad(null, $this->current_facebook_ads_account);
  587.  
  588.                 $last_ad = $adset->getAds($ad::getFields())->end()->exportData();
  589.  
  590.                 //var_dump($last_ad); exit();
  591.  
  592.                 $ad->setData(array(
  593.                     Object\Fields\AdFields::NAME => $item['title'].' #1',
  594.                     Object\Fields\AdFields::ADSET_ID => $adset->id,
  595.                     Object\Fields\AdFields::CREATIVE => array('creative_id' => $creative->id),
  596.                 ));
  597.                 $ad->create(array(
  598.                     Object\Ad::STATUS_PARAM_NAME => Object\Ad::STATUS_PAUSED,
  599.                 ));
  600.                 echo 'Ad ID:' . $ad->id . " <br>\n";
  601.  
  602.             } catch (\Exception $e) {
  603.                 return $this->AdvertisingRenderErrorMessage($e);
  604.  
  605.             }
  606.  
  607.  
  608.             _e('Ad was posted successfully', 'Advertising');
  609.             return;
  610.         }
  611.  
  612.         /**
  613.          * @param null $current_facebook_ads_account
  614.          */
  615.         public function AdvertisingNewAdAccount()
  616.         {
  617.  
  618.         }
  619.  
  620.         /*****************************
  621.          * End of Facebook Ads Controls Block
  622.          *****************************/
  623.  
  624.         public function AdvertisingRenderErrorMessage($response)
  625.         {
  626.  
  627.             $class_name = get_class($response);
  628.             if (strpos($class_name, 'FacebookAds\Http\Exception') !== false) {
  629.                 return ipView('view/advertisingErrorMessageFacebookException.php', [
  630.                     'message' => $response->getMessage(),
  631.                     'code' => $response->getCode(),
  632.                     'trace' => $response->getTraceAsString(),
  633.                     'subcode' => $response->getErrorSubcode(), // @link https://developers.facebook.com/docs/marketing-api/error-reference#errorcodes
  634.                     'error_user_title' => $response->getErrorUserTitle(),
  635.                     'error_user_msg' => $response->getErrorUserMessage(),
  636.                     'blame_spec' => $response->getErrorBlameFieldSpecs()  // @link https://developers.facebook.com/docs/marketing-api/error-reference#blame_field_specs
  637.                 ])->render();
  638.             } else {
  639.                 return ipView('view/advertisingErrorMessageException.php', [
  640.                     'message' => $response->getMessage(),
  641.                     'code' => $response->getCode(),
  642.                     'trace' => $response->getTraceAsString()
  643.                 ])->render();
  644.             }
  645.         }
  646.  
  647.     }
Advertisement
Add Comment
Please, Sign In to add comment