Plugin Directory

Changeset 1841594


Ignore:
Timestamp:
03/16/2018 07:24:15 PM (8 years ago)
Author:
leadfox
Message:

Version 2.04

Location:
leadfox
Files:
5 deleted
2 edited
7 copied

Legend:

Unmodified
Added
Removed
  • leadfox/tags/2.04/leadfox.php

    r1777996 r1841594  
    22/**
    33 * Plugin Name: Leadfox
    4  * Version: 2.03
     4 * Version: 2.04
    55 * Author: Leadfox
    66 * Description: Leadfox converts visitors into ripe leads and paying customers.
     
    1010defined("ABSPATH") or die("No script kiddies please!");
    1111
    12 add_action("plugins_loaded", function() {
     12add_action("plugins_loaded", "leadfox_plugins_loaded");
     13
     14function leadfox_plugins_loaded() {
    1315    // Styles
    1416    wp_register_style("leadfox", plugins_url("css/leadfox.css", __FILE__));
     
    2729    define("LF_PUT", "PUT");
    2830    define("LF_DELETE", "DELETE");
    29    
    30     /**
    31      * Plugin installation
    32      */
    33     function lf_install()
    34     {
    35     }
    36     register_activation_hook(__FILE__, "lf_install");
    37    
    38     /**
    39      * Uninstall leadfox plugin
    40      */
    41     function lf_uninstall()
    42     {
    43     }
    44     register_deactivation_hook(__FILE__, "lf_uninstall");
    45 
    46     /**
    47      * Register settings page
    48      */
    49     function leadfox_admin_init()
    50     {
    51         register_setting("leadfox", "apikey");
    52         register_setting("leadfox", "secret");
    53         register_setting("leadfox", "list");
    54     }
    55     add_action("admin_init", "leadfox_admin_init");
    56    
    57     /**
    58      * Get a POST variable
    59      * @param {string} var - Post variable name
    60      * @return {string} - Value or null
    61      */
    62     function leadfox_post($var)
    63     {
    64         return isset($_POST[$var]) ? $_POST[$var] : null;
    65     }
    66 
    67     /**
    68      * Get the URL of an image for this plugin
    69      * @param {string} s - Image file name
    70      * @return {string} - Url of the image
    71      */
    72     function leadfox_img($s)
    73     {
    74         return plugins_url("images/".$s, __FILE__);
    75     }
    76 
    77     /**
    78      * Get options or post data
    79      * @return {array} - Options
    80      */
    81     function leadfox_options()
    82     {
    83         // Get post or configuration values
    84         return array(
    85             "apikey" => get_option("leadfox_apikey"),
    86             "secret" => get_option("leadfox_secret"),
    87             "list" => get_option("leadfox_list")
    88         );
    89     }
    90 
    91     /**
    92      * Update options
    93      */
    94     function leadfox_update_options()
    95     {
    96         $apikey = leadfox_post("apikey");
    97         if (!empty($apikey)) update_option("leadfox_apikey", $apikey, true);
    98 
    99         $secret = leadfox_post("secret");
    100         if (!empty($secret)) update_option("leadfox_secret", $secret, true);
    101 
    102         $list = leadfox_post("list");
    103         if (!empty($list)) update_option("leadfox_list", $list, true);
    104 
    105         leadfox_notice(__("Your settings were saved correctly."), "success");
    106     }
    107 
    108     /**
    109      * Check if a user as one of the roles passed
    110      * @param {array} info - User roles
    111      * @param {array} roles - Roles to find
    112      * @return {boolean} - If any of the roles were found for user
    113      */
    114     function leadfox_hasrole($info, $roles)
    115     {
    116         foreach ($roles as $role) if (in_array($role, $info)) return true;
    117         return false;
    118     }
    119    
    120     /**
    121      * Get Rest API auth
    122      */
    123     function leadfox_rest_auth()
    124     {
    125         $options = leadfox_options();
    126    
    127         $auth = leadfox_rest(LF_POST, "auth", array(
    128             "apikey" => $options["apikey"],
    129             "secret" => $options["secret"]
    130         ), true);
    131    
    132         if ($auth["status"] == 200) return $auth["body"];
    133         else leadfox_notice(__("You must provide a valid API key and secret."), "error");
    134     }
    135    
    136     /**
    137      * Make a call to the Leadfox Rest API
    138      * @param {string} method - Method to use
    139      * @param {string} url - The relative endpoint url
    140      * @param {array} body - The request's body
    141      * @return {array} - The response array
    142      */
    143     function leadfox_rest($method, $url, $body = null, $isauth = false)
    144     {
    145         global $leadfox_jwt;
    146    
    147         if (!$leadfox_jwt && !$isauth) $leadfox_jwt = leadfox_rest_auth();
    148    
    149         $headers = array(
    150             "Content-Type: application/json",
    151         );
    152    
    153         if ($leadfox_jwt) {
    154             array_push($headers, "Authorization: JWT ".$leadfox_jwt["jwt"]);
    155    
    156             if (isset($leadfox_jwt["clientid"])) {
    157                 if (strpos($url, "?") === false) $url .= "?";
    158                 else $url .= "&";
    159    
    160                 $url .= "clientid=".$leadfox_jwt["clientid"];
    161             }
    162         }
    163    
    164         $ch = curl_init();
    165    
    166         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    167         curl_setopt($ch, CURLOPT_URL, "https://rest.leadfox.co/v1/".$url);
    168         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    169         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    170    
    171         if (is_array($body)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    172    
    173         $response = curl_exec($ch);
    174         $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    175         $error = curl_error($ch);
    176    
    177         curl_close ($ch);
    178    
    179         $ret = array(
    180             "body" => json_decode($response, true),
    181             "status" => $status,
    182             "error" => $error
    183         );
    184    
    185         if (!$isauth && $status >= 400)
    186         {
    187             leadfox_notice(__("There was a problem communicating with Leadfox")." ($status).", "error");
    188             error_log("Leadfox Rest API: ".json_encode($ret));
    189         }
    190    
    191         return $ret;
    192     }
    193    
    194     /**
    195      * The leadfox admin menu hook
    196      */
    197     function leadfox_admin_menu()
    198     {
    199         add_menu_page(
    200             "Leadfox",
    201             "Leadfox",
    202             "manage_options",
    203             "leadfox-menu",
    204             "leadfox_menu_output",
    205             leadfox_img("icon.png")
    206         );
    207     }
    208     add_action("admin_menu", "leadfox_admin_menu");
    209    
    210     /**
    211      * Register a notice
    212      * @param {string} class - Message class
    213      * @param {string} msg - Message
    214      */
    215     function leadfox_notice($msg, $class = "info")
    216     {
    217         global $leadfox_notices;
    218         array_push($leadfox_notices, array("msg" => $msg, "class" => $class));
    219     }
    220    
    221     /**
    222      * Leadfox notices
    223      */
    224     function leadfox_admin_notices()
    225     {
    226         global $leadfox_notices;
    227         $html = "";
    228    
    229         foreach ($leadfox_notices as $notice) $html .= '<div class="notice notice-'.esc_attr($notice["class"]).' is-dismissible"><p>'.esc_html($notice["msg"]).'</p></div>';
    230    
    231         return $html;
    232     }
    233    
    234     /**
    235      * Output the page
    236      * @param {string} buffer - Output buffer
    237      */
    238     function leadfox_output($buffer)
    239     {
    240         return leadfox_admin_notices().$buffer;
    241     }
    242    
    243     /**
    244      * The plugin's page hook
    245      */
    246     function leadfox_menu_output()
    247     {
    248         $options = leadfox_options();
    249         $lists = leadfox_rest(LF_GET, "list?sort=name");
    250    
    251         ob_start("leadfox_output");
    252     ?>
    253         <div class="leadfox-plugin">
    254             <form name="leadfox-options" method="post">
    255                 <img src="<?php echo leadfox_img("logo.png") ?>" alt="Leadfox">
    256                 <h1>Wordpress Integration</h1>
    257    
    258                 <p><?php _e("Enter your API key and secret to integrate the LeadFox tracking codes.") ?><br>
    259                 <?php _e("These can be found under the API section of the Manage menu in your Leadfox account.") ?></p>
    260                 <label for="lf-key"><?php _e("API key") ?></label>
    261                 <div>
    262                     <input id="lf-key" type="text" name="apikey" value="<?php echo $options["apikey"] ?>">
    263                 </div>
    264    
    265                 <label for="lf-secret"><?php _e("Secret") ?></label>
    266                 <div>
    267                     <input id="lf-secret" type="text" name="secret" value="<?php echo $options["secret"] ?>">
    268                 </div>
    269    
    270                 <label for="lf-list"><?php _e("List for new contacts") ?></label>
    271                 <div>
    272                     <select id="lf-list" name="list">
    273                         <option value=""><?php _e("No list") ?></option>
    274    
    275                         <?php if ($lists["body"]) foreach ($lists["body"] as $list): ?>
    276                             <option value="<?php echo $list["_id"] ?>" <?php if ($list["_id"] == $options["list"]) echo "selected"?>><?php echo $list["name"] ?></option>
    277                         <?php endforeach ?>
    278                     </select>
    279                 </div>
    280    
    281                 <div>
    282                     <button type="submit" name="submit"><?php _e("Submit") ?></button>
    283                 </div>
    284    
    285                 <hr>
    286                 <label><?php _e("Sync all contacts now") ?></label>
    287                 <div>
    288                     <button type="submit" name="sync"><?php _e("Sync") ?></button>
    289                 </div>
    290             </form>
    291         </div> 
    292     <?php
    293         ob_end_flush();
    294     }
    295    
    296     /**
    297      * New user registration hook
    298      * @param {integer} uid - User ID
    299      * @return {boolean} - Contact updated?
    300      */
    301     function leadfox_user_register($uid)
    302     {
    303         $options = leadfox_options();
    304         $info = get_userdata($uid);
    305    
    306         if (leadfox_hasrole($info->roles, array("subscriber", "customer")) && $options["list"])
    307         {
    308             // Send new contact
    309             $body = array(
    310                 "name" => $info->first_name." ".$info->last_name,
    311                 "email" => $info->user_email,
    312                 "properties" => array(
    313                     "firstname" => $info->first_name,
    314                     "lastname" => $info->last_name,
    315                     "phone" => get_user_meta($uid, "billing_phone", true)
    316                 )
    317             );
    318    
    319             $contact = leadfox_rest(LF_POST, "contact/upsert", $body);
    320    
    321             // Add contact to list
    322             $url = "list/".$options["list"]."/contact/".$contact["body"]["_id"];
    323             $list = leadfox_rest(LF_POST, $url);
    324    
    325             return true;
    326         }
    327    
    328         return false;
    329     }
    330     add_action("user_register", "leadfox_user_register");
    331    
    332     /**
    333      * Sync all contacts
    334      */
    335     function leadfox_sync_contacts()
    336     {
    337         $count = 0;
    338    
    339         $users = get_users(array(
    340             "role__in" => array("subscriber", "customer"),
    341             "orderby" => "id",
    342             "fields" => array("id")
    343         ));
    344    
    345         foreach ($users as $user) if (leadfox_user_register($user->id)) $count++;
    346         leadfox_notice($count." ".__("contact(s) updated"), "success");
    347     }
    348    
    349     /**
    350      * Add the tracking code to all pages
    351      */
    352     function leadfox_footer()
    353     {
    354         global $wp;
    355         $url = home_url(add_query_arg(array(), $wp->request));
    356         $options = leadfox_options();
    357    
    358         if (strpos($url, "order-received") !== false)
    359         {
    360             echo "<script>(function(l,f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/lifecycle.js?key='+x+'&l='+l;a.parentNode.insertBefore(b,a);})(5,document,'script','".$options["apikey"]."');</script>";
    361         }
    362    
    363         echo "<script>(function(f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/init.js?key='+x;a.parentNode.insertBefore(b,a);})(document,'script','".$options["apikey"]."');</script>";
    364     }
    365     add_action("wp_footer", "leadfox_footer");
    366    
     31
    36732    // Main
    36833    if (leadfox_post("submit") !== null) leadfox_update_options();
    36934    if (leadfox_post("sync") !== null) leadfox_sync_contacts();
    370 });
     35}
     36
     37/**
     38 * Plugin installation
     39 */
     40function lf_install()
     41{
     42}
     43register_activation_hook(__FILE__, "lf_install");
     44
     45/**
     46 * Uninstall leadfox plugin
     47 */
     48function lf_uninstall()
     49{
     50}
     51register_deactivation_hook(__FILE__, "lf_uninstall");
     52
     53/**
     54 * Register settings page
     55 */
     56function leadfox_admin_init()
     57{
     58    register_setting("leadfox", "apikey");
     59    register_setting("leadfox", "secret");
     60    register_setting("leadfox", "list");
     61}
     62add_action("admin_init", "leadfox_admin_init");
     63
     64/**
     65 * Get a POST variable
     66 * @param {string} var - Post variable name
     67 * @return {string} - Value or null
     68 */
     69function leadfox_post($var)
     70{
     71    return isset($_POST[$var]) ? $_POST[$var] : null;
     72}
     73
     74/**
     75 * Get the URL of an image for this plugin
     76 * @param {string} s - Image file name
     77 * @return {string} - Url of the image
     78 */
     79function leadfox_img($s)
     80{
     81    return plugins_url("images/".$s, __FILE__);
     82}
     83
     84/**
     85 * Get options or post data
     86 * @return {array} - Options
     87 */
     88function leadfox_options()
     89{
     90    // Get post or configuration values
     91    return array(
     92        "apikey" => get_option("leadfox_apikey"),
     93        "secret" => get_option("leadfox_secret"),
     94        "list" => get_option("leadfox_list")
     95    );
     96}
     97
     98/**
     99 * Update options
     100 */
     101function leadfox_update_options()
     102{
     103    $apikey = leadfox_post("apikey");
     104    if (!empty($apikey)) update_option("leadfox_apikey", $apikey, true);
     105
     106    $secret = leadfox_post("secret");
     107    if (!empty($secret)) update_option("leadfox_secret", $secret, true);
     108
     109    $list = leadfox_post("list");
     110    if (!empty($list)) update_option("leadfox_list", $list, true);
     111
     112    leadfox_notice(__("Your settings were saved correctly."), "success");
     113}
     114
     115/**
     116 * Check if a user as one of the roles passed
     117 * @param {array} info - User roles
     118 * @param {array} roles - Roles to find
     119 * @return {boolean} - If any of the roles were found for user
     120 */
     121function leadfox_hasrole($info, $roles)
     122{
     123    foreach ($roles as $role) if (in_array($role, $info)) return true;
     124    return false;
     125}
     126
     127/**
     128 * Get Rest API auth
     129 */
     130function leadfox_rest_auth()
     131{
     132    $options = leadfox_options();
     133
     134    $auth = leadfox_rest(LF_POST, "auth", array(
     135        "apikey" => $options["apikey"],
     136        "secret" => $options["secret"]
     137    ), true);
     138
     139    if ($auth["status"] == 200) return $auth["body"];
     140    else leadfox_notice(__("You must provide a valid API key and secret."), "error");
     141}
     142
     143/**
     144 * Make a call to the Leadfox Rest API
     145 * @param {string} method - Method to use
     146 * @param {string} url - The relative endpoint url
     147 * @param {array} body - The request's body
     148 * @return {array} - The response array
     149 */
     150function leadfox_rest($method, $url, $body = null, $isauth = false)
     151{
     152    global $leadfox_jwt;
     153
     154    if (!$leadfox_jwt && !$isauth) $leadfox_jwt = leadfox_rest_auth();
     155
     156    $headers = array(
     157        "Content-Type: application/json",
     158    );
     159
     160    if ($leadfox_jwt) {
     161        array_push($headers, "Authorization: JWT ".$leadfox_jwt["jwt"]);
     162
     163        if (isset($leadfox_jwt["clientid"])) {
     164            if (strpos($url, "?") === false) $url .= "?";
     165            else $url .= "&";
     166
     167            $url .= "clientid=".$leadfox_jwt["clientid"];
     168        }
     169    }
     170
     171    $ch = curl_init();
     172
     173    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
     174    curl_setopt($ch, CURLOPT_URL, "https://rest.leadfox.co/v1/".$url);
     175    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     176    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     177
     178    if (is_array($body)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
     179
     180    $response = curl_exec($ch);
     181    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     182    $error = curl_error($ch);
     183
     184    curl_close ($ch);
     185
     186    $ret = array(
     187        "body" => json_decode($response, true),
     188        "status" => $status,
     189        "error" => $error
     190    );
     191
     192    if (!$isauth && $status >= 400)
     193    {
     194        leadfox_notice(__("There was a problem communicating with Leadfox")." ($status).", "error");
     195        error_log("Leadfox Rest API: ".json_encode($ret));
     196    }
     197
     198    return $ret;
     199}
     200
     201/**
     202 * The leadfox admin menu hook
     203 */
     204function leadfox_admin_menu()
     205{
     206    add_menu_page(
     207        "Leadfox",
     208        "Leadfox",
     209        "manage_options",
     210        "leadfox-menu",
     211        "leadfox_menu_output",
     212        leadfox_img("icon.png")
     213    );
     214}
     215add_action("admin_menu", "leadfox_admin_menu");
     216
     217/**
     218 * Register a notice
     219 * @param {string} class - Message class
     220 * @param {string} msg - Message
     221 */
     222function leadfox_notice($msg, $class = "info")
     223{
     224    global $leadfox_notices;
     225    array_push($leadfox_notices, array("msg" => $msg, "class" => $class));
     226}
     227
     228/**
     229 * Leadfox notices
     230 */
     231function leadfox_admin_notices()
     232{
     233    global $leadfox_notices;
     234    $html = "";
     235
     236    foreach ($leadfox_notices as $notice) $html .= '<div class="notice notice-'.esc_attr($notice["class"]).' is-dismissible"><p>'.esc_html($notice["msg"]).'</p></div>';
     237
     238    return $html;
     239}
     240
     241/**
     242 * Output the page
     243 * @param {string} buffer - Output buffer
     244 */
     245function leadfox_output($buffer)
     246{
     247    return leadfox_admin_notices().$buffer;
     248}
     249
     250/**
     251 * The plugin's page hook
     252 */
     253function leadfox_menu_output()
     254{
     255    $options = leadfox_options();
     256    $lists = leadfox_rest(LF_GET, "list?sort=name");
     257
     258    ob_start("leadfox_output");
     259?>
     260    <div class="leadfox-plugin">
     261        <form name="leadfox-options" method="post">
     262            <img src="<?php echo leadfox_img("logo.png") ?>" alt="Leadfox">
     263            <h1>Wordpress Integration</h1>
     264
     265            <p><?php _e("Enter your API key and secret to integrate the LeadFox tracking codes.") ?><br>
     266            <?php _e("These can be found under the API section of the Manage menu in your Leadfox account.") ?></p>
     267            <label for="lf-key"><?php _e("API key") ?></label>
     268            <div>
     269                <input id="lf-key" type="text" name="apikey" value="<?php echo $options["apikey"] ?>">
     270            </div>
     271
     272            <label for="lf-secret"><?php _e("Secret") ?></label>
     273            <div>
     274                <input id="lf-secret" type="text" name="secret" value="<?php echo $options["secret"] ?>">
     275            </div>
     276
     277            <label for="lf-list"><?php _e("List for new contacts") ?></label>
     278            <div>
     279                <select id="lf-list" name="list">
     280                    <option value=""><?php _e("No list") ?></option>
     281
     282                    <?php if ($lists["body"]) foreach ($lists["body"] as $list): ?>
     283                        <option value="<?php echo $list["_id"] ?>" <?php if ($list["_id"] == $options["list"]) echo "selected"?>><?php echo $list["name"] ?></option>
     284                    <?php endforeach ?>
     285                </select>
     286            </div>
     287
     288            <div>
     289                <button type="submit" name="submit"><?php _e("Submit") ?></button>
     290            </div>
     291
     292            <hr>
     293            <label><?php _e("Sync all contacts now") ?></label>
     294            <div>
     295                <button type="submit" name="sync"><?php _e("Sync") ?></button>
     296            </div>
     297        </form>
     298    </div> 
     299<?php
     300    ob_end_flush();
     301}
     302
     303/**
     304 * New user registration hook
     305 * @param {integer} uid - User ID
     306 * @return {boolean} - Contact updated?
     307 */
     308function leadfox_user_register($uid)
     309{
     310    $options = leadfox_options();
     311    $info = get_userdata($uid);
     312
     313    if (leadfox_hasrole($info->roles, array("subscriber", "customer")) && $options["list"])
     314    {
     315        // Send new contact
     316        $body = array(
     317            "name" => $info->first_name." ".$info->last_name,
     318            "email" => $info->user_email,
     319            "properties" => array(
     320                "firstname" => $info->first_name,
     321                "lastname" => $info->last_name,
     322                "phone" => get_user_meta($uid, "billing_phone", true)
     323            )
     324        );
     325
     326        $contact = leadfox_rest(LF_POST, "contact/upsert", $body);
     327
     328        // Add contact to list
     329        $url = "list/".$options["list"]."/contact/".$contact["body"]["_id"];
     330        $list = leadfox_rest(LF_POST, $url);
     331
     332        return true;
     333    }
     334
     335    return false;
     336}
     337add_action("user_register", "leadfox_user_register");
     338
     339/**
     340 * Sync all contacts
     341 */
     342function leadfox_sync_contacts()
     343{
     344    $count = 0;
     345
     346    $users = get_users(array(
     347        "role__in" => array("subscriber", "customer"),
     348        "orderby" => "id",
     349        "fields" => array("id")
     350    ));
     351
     352    foreach ($users as $user) if (leadfox_user_register($user->id)) $count++;
     353    leadfox_notice($count." ".__("contact(s) updated"), "success");
     354}
     355
     356/**
     357 * Add the tracking code to all pages
     358 */
     359function leadfox_footer()
     360{
     361    global $wp;
     362    $url = home_url(add_query_arg(array(), $wp->request));
     363    $options = leadfox_options();
     364
     365    if (strpos($url, "order-received") !== false)
     366    {
     367        echo "<script>(function(l,f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/lifecycle.js?key='+x+'&l='+l;a.parentNode.insertBefore(b,a);})(5,document,'script','".$options["apikey"]."');</script>";
     368    }
     369
     370    echo "<script>(function(f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/init.js?key='+x;a.parentNode.insertBefore(b,a);})(document,'script','".$options["apikey"]."');</script>";
     371}
     372add_action("wp_footer", "leadfox_footer");
  • leadfox/tags/2.04/readme.txt

    r1777996 r1841594  
    44Requires at least: 3.7
    55Tested up to: 4.8.2
    6 Stable tag: 2.03
     6Stable tag: 2.04
    77License: GPLv2 or later
    88
     
    3636== Changelog ==
    3737
     38= 2.04 =
     39Fixed a bug with the call to add_action("plugins_loaded", ...)
     40
    3841= 2.03 =
    3942Fixed a bug with configuration options being lost when reloading the options page.
  • leadfox/trunk/leadfox.php

    r1777996 r1841594  
    22/**
    33 * Plugin Name: Leadfox
    4  * Version: 2.03
     4 * Version: 2.04
    55 * Author: Leadfox
    66 * Description: Leadfox converts visitors into ripe leads and paying customers.
     
    1010defined("ABSPATH") or die("No script kiddies please!");
    1111
    12 add_action("plugins_loaded", function() {
     12add_action("plugins_loaded", "leadfox_plugins_loaded");
     13
     14function leadfox_plugins_loaded() {
    1315    // Styles
    1416    wp_register_style("leadfox", plugins_url("css/leadfox.css", __FILE__));
     
    2729    define("LF_PUT", "PUT");
    2830    define("LF_DELETE", "DELETE");
    29    
    30     /**
    31      * Plugin installation
    32      */
    33     function lf_install()
    34     {
    35     }
    36     register_activation_hook(__FILE__, "lf_install");
    37    
    38     /**
    39      * Uninstall leadfox plugin
    40      */
    41     function lf_uninstall()
    42     {
    43     }
    44     register_deactivation_hook(__FILE__, "lf_uninstall");
    45 
    46     /**
    47      * Register settings page
    48      */
    49     function leadfox_admin_init()
    50     {
    51         register_setting("leadfox", "apikey");
    52         register_setting("leadfox", "secret");
    53         register_setting("leadfox", "list");
    54     }
    55     add_action("admin_init", "leadfox_admin_init");
    56    
    57     /**
    58      * Get a POST variable
    59      * @param {string} var - Post variable name
    60      * @return {string} - Value or null
    61      */
    62     function leadfox_post($var)
    63     {
    64         return isset($_POST[$var]) ? $_POST[$var] : null;
    65     }
    66 
    67     /**
    68      * Get the URL of an image for this plugin
    69      * @param {string} s - Image file name
    70      * @return {string} - Url of the image
    71      */
    72     function leadfox_img($s)
    73     {
    74         return plugins_url("images/".$s, __FILE__);
    75     }
    76 
    77     /**
    78      * Get options or post data
    79      * @return {array} - Options
    80      */
    81     function leadfox_options()
    82     {
    83         // Get post or configuration values
    84         return array(
    85             "apikey" => get_option("leadfox_apikey"),
    86             "secret" => get_option("leadfox_secret"),
    87             "list" => get_option("leadfox_list")
    88         );
    89     }
    90 
    91     /**
    92      * Update options
    93      */
    94     function leadfox_update_options()
    95     {
    96         $apikey = leadfox_post("apikey");
    97         if (!empty($apikey)) update_option("leadfox_apikey", $apikey, true);
    98 
    99         $secret = leadfox_post("secret");
    100         if (!empty($secret)) update_option("leadfox_secret", $secret, true);
    101 
    102         $list = leadfox_post("list");
    103         if (!empty($list)) update_option("leadfox_list", $list, true);
    104 
    105         leadfox_notice(__("Your settings were saved correctly."), "success");
    106     }
    107 
    108     /**
    109      * Check if a user as one of the roles passed
    110      * @param {array} info - User roles
    111      * @param {array} roles - Roles to find
    112      * @return {boolean} - If any of the roles were found for user
    113      */
    114     function leadfox_hasrole($info, $roles)
    115     {
    116         foreach ($roles as $role) if (in_array($role, $info)) return true;
    117         return false;
    118     }
    119    
    120     /**
    121      * Get Rest API auth
    122      */
    123     function leadfox_rest_auth()
    124     {
    125         $options = leadfox_options();
    126    
    127         $auth = leadfox_rest(LF_POST, "auth", array(
    128             "apikey" => $options["apikey"],
    129             "secret" => $options["secret"]
    130         ), true);
    131    
    132         if ($auth["status"] == 200) return $auth["body"];
    133         else leadfox_notice(__("You must provide a valid API key and secret."), "error");
    134     }
    135    
    136     /**
    137      * Make a call to the Leadfox Rest API
    138      * @param {string} method - Method to use
    139      * @param {string} url - The relative endpoint url
    140      * @param {array} body - The request's body
    141      * @return {array} - The response array
    142      */
    143     function leadfox_rest($method, $url, $body = null, $isauth = false)
    144     {
    145         global $leadfox_jwt;
    146    
    147         if (!$leadfox_jwt && !$isauth) $leadfox_jwt = leadfox_rest_auth();
    148    
    149         $headers = array(
    150             "Content-Type: application/json",
    151         );
    152    
    153         if ($leadfox_jwt) {
    154             array_push($headers, "Authorization: JWT ".$leadfox_jwt["jwt"]);
    155    
    156             if (isset($leadfox_jwt["clientid"])) {
    157                 if (strpos($url, "?") === false) $url .= "?";
    158                 else $url .= "&";
    159    
    160                 $url .= "clientid=".$leadfox_jwt["clientid"];
    161             }
    162         }
    163    
    164         $ch = curl_init();
    165    
    166         curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
    167         curl_setopt($ch, CURLOPT_URL, "https://rest.leadfox.co/v1/".$url);
    168         curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    169         curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    170    
    171         if (is_array($body)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
    172    
    173         $response = curl_exec($ch);
    174         $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    175         $error = curl_error($ch);
    176    
    177         curl_close ($ch);
    178    
    179         $ret = array(
    180             "body" => json_decode($response, true),
    181             "status" => $status,
    182             "error" => $error
    183         );
    184    
    185         if (!$isauth && $status >= 400)
    186         {
    187             leadfox_notice(__("There was a problem communicating with Leadfox")." ($status).", "error");
    188             error_log("Leadfox Rest API: ".json_encode($ret));
    189         }
    190    
    191         return $ret;
    192     }
    193    
    194     /**
    195      * The leadfox admin menu hook
    196      */
    197     function leadfox_admin_menu()
    198     {
    199         add_menu_page(
    200             "Leadfox",
    201             "Leadfox",
    202             "manage_options",
    203             "leadfox-menu",
    204             "leadfox_menu_output",
    205             leadfox_img("icon.png")
    206         );
    207     }
    208     add_action("admin_menu", "leadfox_admin_menu");
    209    
    210     /**
    211      * Register a notice
    212      * @param {string} class - Message class
    213      * @param {string} msg - Message
    214      */
    215     function leadfox_notice($msg, $class = "info")
    216     {
    217         global $leadfox_notices;
    218         array_push($leadfox_notices, array("msg" => $msg, "class" => $class));
    219     }
    220    
    221     /**
    222      * Leadfox notices
    223      */
    224     function leadfox_admin_notices()
    225     {
    226         global $leadfox_notices;
    227         $html = "";
    228    
    229         foreach ($leadfox_notices as $notice) $html .= '<div class="notice notice-'.esc_attr($notice["class"]).' is-dismissible"><p>'.esc_html($notice["msg"]).'</p></div>';
    230    
    231         return $html;
    232     }
    233    
    234     /**
    235      * Output the page
    236      * @param {string} buffer - Output buffer
    237      */
    238     function leadfox_output($buffer)
    239     {
    240         return leadfox_admin_notices().$buffer;
    241     }
    242    
    243     /**
    244      * The plugin's page hook
    245      */
    246     function leadfox_menu_output()
    247     {
    248         $options = leadfox_options();
    249         $lists = leadfox_rest(LF_GET, "list?sort=name");
    250    
    251         ob_start("leadfox_output");
    252     ?>
    253         <div class="leadfox-plugin">
    254             <form name="leadfox-options" method="post">
    255                 <img src="<?php echo leadfox_img("logo.png") ?>" alt="Leadfox">
    256                 <h1>Wordpress Integration</h1>
    257    
    258                 <p><?php _e("Enter your API key and secret to integrate the LeadFox tracking codes.") ?><br>
    259                 <?php _e("These can be found under the API section of the Manage menu in your Leadfox account.") ?></p>
    260                 <label for="lf-key"><?php _e("API key") ?></label>
    261                 <div>
    262                     <input id="lf-key" type="text" name="apikey" value="<?php echo $options["apikey"] ?>">
    263                 </div>
    264    
    265                 <label for="lf-secret"><?php _e("Secret") ?></label>
    266                 <div>
    267                     <input id="lf-secret" type="text" name="secret" value="<?php echo $options["secret"] ?>">
    268                 </div>
    269    
    270                 <label for="lf-list"><?php _e("List for new contacts") ?></label>
    271                 <div>
    272                     <select id="lf-list" name="list">
    273                         <option value=""><?php _e("No list") ?></option>
    274    
    275                         <?php if ($lists["body"]) foreach ($lists["body"] as $list): ?>
    276                             <option value="<?php echo $list["_id"] ?>" <?php if ($list["_id"] == $options["list"]) echo "selected"?>><?php echo $list["name"] ?></option>
    277                         <?php endforeach ?>
    278                     </select>
    279                 </div>
    280    
    281                 <div>
    282                     <button type="submit" name="submit"><?php _e("Submit") ?></button>
    283                 </div>
    284    
    285                 <hr>
    286                 <label><?php _e("Sync all contacts now") ?></label>
    287                 <div>
    288                     <button type="submit" name="sync"><?php _e("Sync") ?></button>
    289                 </div>
    290             </form>
    291         </div> 
    292     <?php
    293         ob_end_flush();
    294     }
    295    
    296     /**
    297      * New user registration hook
    298      * @param {integer} uid - User ID
    299      * @return {boolean} - Contact updated?
    300      */
    301     function leadfox_user_register($uid)
    302     {
    303         $options = leadfox_options();
    304         $info = get_userdata($uid);
    305    
    306         if (leadfox_hasrole($info->roles, array("subscriber", "customer")) && $options["list"])
    307         {
    308             // Send new contact
    309             $body = array(
    310                 "name" => $info->first_name." ".$info->last_name,
    311                 "email" => $info->user_email,
    312                 "properties" => array(
    313                     "firstname" => $info->first_name,
    314                     "lastname" => $info->last_name,
    315                     "phone" => get_user_meta($uid, "billing_phone", true)
    316                 )
    317             );
    318    
    319             $contact = leadfox_rest(LF_POST, "contact/upsert", $body);
    320    
    321             // Add contact to list
    322             $url = "list/".$options["list"]."/contact/".$contact["body"]["_id"];
    323             $list = leadfox_rest(LF_POST, $url);
    324    
    325             return true;
    326         }
    327    
    328         return false;
    329     }
    330     add_action("user_register", "leadfox_user_register");
    331    
    332     /**
    333      * Sync all contacts
    334      */
    335     function leadfox_sync_contacts()
    336     {
    337         $count = 0;
    338    
    339         $users = get_users(array(
    340             "role__in" => array("subscriber", "customer"),
    341             "orderby" => "id",
    342             "fields" => array("id")
    343         ));
    344    
    345         foreach ($users as $user) if (leadfox_user_register($user->id)) $count++;
    346         leadfox_notice($count." ".__("contact(s) updated"), "success");
    347     }
    348    
    349     /**
    350      * Add the tracking code to all pages
    351      */
    352     function leadfox_footer()
    353     {
    354         global $wp;
    355         $url = home_url(add_query_arg(array(), $wp->request));
    356         $options = leadfox_options();
    357    
    358         if (strpos($url, "order-received") !== false)
    359         {
    360             echo "<script>(function(l,f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/lifecycle.js?key='+x+'&l='+l;a.parentNode.insertBefore(b,a);})(5,document,'script','".$options["apikey"]."');</script>";
    361         }
    362    
    363         echo "<script>(function(f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/init.js?key='+x;a.parentNode.insertBefore(b,a);})(document,'script','".$options["apikey"]."');</script>";
    364     }
    365     add_action("wp_footer", "leadfox_footer");
    366    
     31
    36732    // Main
    36833    if (leadfox_post("submit") !== null) leadfox_update_options();
    36934    if (leadfox_post("sync") !== null) leadfox_sync_contacts();
    370 });
     35}
     36
     37/**
     38 * Plugin installation
     39 */
     40function lf_install()
     41{
     42}
     43register_activation_hook(__FILE__, "lf_install");
     44
     45/**
     46 * Uninstall leadfox plugin
     47 */
     48function lf_uninstall()
     49{
     50}
     51register_deactivation_hook(__FILE__, "lf_uninstall");
     52
     53/**
     54 * Register settings page
     55 */
     56function leadfox_admin_init()
     57{
     58    register_setting("leadfox", "apikey");
     59    register_setting("leadfox", "secret");
     60    register_setting("leadfox", "list");
     61}
     62add_action("admin_init", "leadfox_admin_init");
     63
     64/**
     65 * Get a POST variable
     66 * @param {string} var - Post variable name
     67 * @return {string} - Value or null
     68 */
     69function leadfox_post($var)
     70{
     71    return isset($_POST[$var]) ? $_POST[$var] : null;
     72}
     73
     74/**
     75 * Get the URL of an image for this plugin
     76 * @param {string} s - Image file name
     77 * @return {string} - Url of the image
     78 */
     79function leadfox_img($s)
     80{
     81    return plugins_url("images/".$s, __FILE__);
     82}
     83
     84/**
     85 * Get options or post data
     86 * @return {array} - Options
     87 */
     88function leadfox_options()
     89{
     90    // Get post or configuration values
     91    return array(
     92        "apikey" => get_option("leadfox_apikey"),
     93        "secret" => get_option("leadfox_secret"),
     94        "list" => get_option("leadfox_list")
     95    );
     96}
     97
     98/**
     99 * Update options
     100 */
     101function leadfox_update_options()
     102{
     103    $apikey = leadfox_post("apikey");
     104    if (!empty($apikey)) update_option("leadfox_apikey", $apikey, true);
     105
     106    $secret = leadfox_post("secret");
     107    if (!empty($secret)) update_option("leadfox_secret", $secret, true);
     108
     109    $list = leadfox_post("list");
     110    if (!empty($list)) update_option("leadfox_list", $list, true);
     111
     112    leadfox_notice(__("Your settings were saved correctly."), "success");
     113}
     114
     115/**
     116 * Check if a user as one of the roles passed
     117 * @param {array} info - User roles
     118 * @param {array} roles - Roles to find
     119 * @return {boolean} - If any of the roles were found for user
     120 */
     121function leadfox_hasrole($info, $roles)
     122{
     123    foreach ($roles as $role) if (in_array($role, $info)) return true;
     124    return false;
     125}
     126
     127/**
     128 * Get Rest API auth
     129 */
     130function leadfox_rest_auth()
     131{
     132    $options = leadfox_options();
     133
     134    $auth = leadfox_rest(LF_POST, "auth", array(
     135        "apikey" => $options["apikey"],
     136        "secret" => $options["secret"]
     137    ), true);
     138
     139    if ($auth["status"] == 200) return $auth["body"];
     140    else leadfox_notice(__("You must provide a valid API key and secret."), "error");
     141}
     142
     143/**
     144 * Make a call to the Leadfox Rest API
     145 * @param {string} method - Method to use
     146 * @param {string} url - The relative endpoint url
     147 * @param {array} body - The request's body
     148 * @return {array} - The response array
     149 */
     150function leadfox_rest($method, $url, $body = null, $isauth = false)
     151{
     152    global $leadfox_jwt;
     153
     154    if (!$leadfox_jwt && !$isauth) $leadfox_jwt = leadfox_rest_auth();
     155
     156    $headers = array(
     157        "Content-Type: application/json",
     158    );
     159
     160    if ($leadfox_jwt) {
     161        array_push($headers, "Authorization: JWT ".$leadfox_jwt["jwt"]);
     162
     163        if (isset($leadfox_jwt["clientid"])) {
     164            if (strpos($url, "?") === false) $url .= "?";
     165            else $url .= "&";
     166
     167            $url .= "clientid=".$leadfox_jwt["clientid"];
     168        }
     169    }
     170
     171    $ch = curl_init();
     172
     173    curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);
     174    curl_setopt($ch, CURLOPT_URL, "https://rest.leadfox.co/v1/".$url);
     175    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
     176    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
     177
     178    if (is_array($body)) curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($body));
     179
     180    $response = curl_exec($ch);
     181    $status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
     182    $error = curl_error($ch);
     183
     184    curl_close ($ch);
     185
     186    $ret = array(
     187        "body" => json_decode($response, true),
     188        "status" => $status,
     189        "error" => $error
     190    );
     191
     192    if (!$isauth && $status >= 400)
     193    {
     194        leadfox_notice(__("There was a problem communicating with Leadfox")." ($status).", "error");
     195        error_log("Leadfox Rest API: ".json_encode($ret));
     196    }
     197
     198    return $ret;
     199}
     200
     201/**
     202 * The leadfox admin menu hook
     203 */
     204function leadfox_admin_menu()
     205{
     206    add_menu_page(
     207        "Leadfox",
     208        "Leadfox",
     209        "manage_options",
     210        "leadfox-menu",
     211        "leadfox_menu_output",
     212        leadfox_img("icon.png")
     213    );
     214}
     215add_action("admin_menu", "leadfox_admin_menu");
     216
     217/**
     218 * Register a notice
     219 * @param {string} class - Message class
     220 * @param {string} msg - Message
     221 */
     222function leadfox_notice($msg, $class = "info")
     223{
     224    global $leadfox_notices;
     225    array_push($leadfox_notices, array("msg" => $msg, "class" => $class));
     226}
     227
     228/**
     229 * Leadfox notices
     230 */
     231function leadfox_admin_notices()
     232{
     233    global $leadfox_notices;
     234    $html = "";
     235
     236    foreach ($leadfox_notices as $notice) $html .= '<div class="notice notice-'.esc_attr($notice["class"]).' is-dismissible"><p>'.esc_html($notice["msg"]).'</p></div>';
     237
     238    return $html;
     239}
     240
     241/**
     242 * Output the page
     243 * @param {string} buffer - Output buffer
     244 */
     245function leadfox_output($buffer)
     246{
     247    return leadfox_admin_notices().$buffer;
     248}
     249
     250/**
     251 * The plugin's page hook
     252 */
     253function leadfox_menu_output()
     254{
     255    $options = leadfox_options();
     256    $lists = leadfox_rest(LF_GET, "list?sort=name");
     257
     258    ob_start("leadfox_output");
     259?>
     260    <div class="leadfox-plugin">
     261        <form name="leadfox-options" method="post">
     262            <img src="<?php echo leadfox_img("logo.png") ?>" alt="Leadfox">
     263            <h1>Wordpress Integration</h1>
     264
     265            <p><?php _e("Enter your API key and secret to integrate the LeadFox tracking codes.") ?><br>
     266            <?php _e("These can be found under the API section of the Manage menu in your Leadfox account.") ?></p>
     267            <label for="lf-key"><?php _e("API key") ?></label>
     268            <div>
     269                <input id="lf-key" type="text" name="apikey" value="<?php echo $options["apikey"] ?>">
     270            </div>
     271
     272            <label for="lf-secret"><?php _e("Secret") ?></label>
     273            <div>
     274                <input id="lf-secret" type="text" name="secret" value="<?php echo $options["secret"] ?>">
     275            </div>
     276
     277            <label for="lf-list"><?php _e("List for new contacts") ?></label>
     278            <div>
     279                <select id="lf-list" name="list">
     280                    <option value=""><?php _e("No list") ?></option>
     281
     282                    <?php if ($lists["body"]) foreach ($lists["body"] as $list): ?>
     283                        <option value="<?php echo $list["_id"] ?>" <?php if ($list["_id"] == $options["list"]) echo "selected"?>><?php echo $list["name"] ?></option>
     284                    <?php endforeach ?>
     285                </select>
     286            </div>
     287
     288            <div>
     289                <button type="submit" name="submit"><?php _e("Submit") ?></button>
     290            </div>
     291
     292            <hr>
     293            <label><?php _e("Sync all contacts now") ?></label>
     294            <div>
     295                <button type="submit" name="sync"><?php _e("Sync") ?></button>
     296            </div>
     297        </form>
     298    </div> 
     299<?php
     300    ob_end_flush();
     301}
     302
     303/**
     304 * New user registration hook
     305 * @param {integer} uid - User ID
     306 * @return {boolean} - Contact updated?
     307 */
     308function leadfox_user_register($uid)
     309{
     310    $options = leadfox_options();
     311    $info = get_userdata($uid);
     312
     313    if (leadfox_hasrole($info->roles, array("subscriber", "customer")) && $options["list"])
     314    {
     315        // Send new contact
     316        $body = array(
     317            "name" => $info->first_name." ".$info->last_name,
     318            "email" => $info->user_email,
     319            "properties" => array(
     320                "firstname" => $info->first_name,
     321                "lastname" => $info->last_name,
     322                "phone" => get_user_meta($uid, "billing_phone", true)
     323            )
     324        );
     325
     326        $contact = leadfox_rest(LF_POST, "contact/upsert", $body);
     327
     328        // Add contact to list
     329        $url = "list/".$options["list"]."/contact/".$contact["body"]["_id"];
     330        $list = leadfox_rest(LF_POST, $url);
     331
     332        return true;
     333    }
     334
     335    return false;
     336}
     337add_action("user_register", "leadfox_user_register");
     338
     339/**
     340 * Sync all contacts
     341 */
     342function leadfox_sync_contacts()
     343{
     344    $count = 0;
     345
     346    $users = get_users(array(
     347        "role__in" => array("subscriber", "customer"),
     348        "orderby" => "id",
     349        "fields" => array("id")
     350    ));
     351
     352    foreach ($users as $user) if (leadfox_user_register($user->id)) $count++;
     353    leadfox_notice($count." ".__("contact(s) updated"), "success");
     354}
     355
     356/**
     357 * Add the tracking code to all pages
     358 */
     359function leadfox_footer()
     360{
     361    global $wp;
     362    $url = home_url(add_query_arg(array(), $wp->request));
     363    $options = leadfox_options();
     364
     365    if (strpos($url, "order-received") !== false)
     366    {
     367        echo "<script>(function(l,f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/lifecycle.js?key='+x+'&l='+l;a.parentNode.insertBefore(b,a);})(5,document,'script','".$options["apikey"]."');</script>";
     368    }
     369
     370    echo "<script>(function(f,o,x){var a=f.getElementsByTagName(o)[0],b=f.createElement(o);b.async=true;b.id='__js_lm_00';b.src='//app.leadfox.co/js/api/init.js?key='+x;a.parentNode.insertBefore(b,a);})(document,'script','".$options["apikey"]."');</script>";
     371}
     372add_action("wp_footer", "leadfox_footer");
  • leadfox/trunk/readme.txt

    r1777996 r1841594  
    44Requires at least: 3.7
    55Tested up to: 4.8.2
    6 Stable tag: 2.03
     6Stable tag: 2.04
    77License: GPLv2 or later
    88
     
    3636== Changelog ==
    3737
     38= 2.04 =
     39Fixed a bug with the call to add_action("plugins_loaded", ...)
     40
    3841= 2.03 =
    3942Fixed a bug with configuration options being lost when reloading the options page.
Note: See TracChangeset for help on using the changeset viewer.