Advertisement
Guest User

Untitled

a guest
Nov 25th, 2013
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 16.59 KB | None | 0 0
  1. <?php
  2. /*
  3. Plugin Name: Intercom for WordPress
  4. Plugin URI: http://lumpylemon.co.uk/plugins/intercom-crm-for-wordpress
  5. Description: Integrate the <a href="http://intercom.io">Intercom</a> CRM and messaging app into your WordPress website.
  6. Author: Simon Blackbourn
  7. Author URI: https://twitter.com/lumpysimon
  8. Version: 0.7
  9.  
  10.  
  11.  
  12.     -----------
  13.     description
  14.     -----------
  15.  
  16.     Intercom is a customer relationship management (CRM) and messaging tool for web app owners. WordPress is being widely used as a web app nowadays, so Intercom is an ideal companion app to find out more about your users, contact them, get their instant feedback, and track your relationship with them over time so you can spot those who need attention.
  17.  
  18.     This plugin generates the Javascript install code to integrate all of this functionality into your WordPress-powered web app, so you can track and communicate with your users both on the front-end and on your admin pages.
  19.  
  20.     It allows you to securely connect to Intercom using secure key authentication mode, and you can optionally send extra custom data about your users.
  21.  
  22.  
  23.  
  24.     -------
  25.     license
  26.     -------
  27.  
  28.     This is a plugin for WordPress (http://wordpress.org).
  29.  
  30.     Copyright Simon Blackbourn (simon@lumpylemon.co.uk)
  31.  
  32.     Released under the GPL license: http://www.opensource.org/licenses/gpl-license.php
  33.  
  34.     This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2 of the License, or (at your option) any later version.
  35.  
  36.     This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  37.  
  38.  
  39.  
  40.     --------
  41.     about me
  42.     --------
  43.  
  44.     I'm Simon Blackbourn, co-founder of Lumpy Lemon, a friendly UK-based WordPress design & development company specialising in custom-built WordPress CMS sites. I work mainly, but not exclusively, with not-for-profit organisations.
  45.  
  46.     Find me on Twitter, Skype & GitHub: lumpysimon
  47.  
  48.  
  49.  
  50. */
  51.  
  52.  
  53.  
  54. defined( 'ABSPATH' ) or die();
  55.  
  56.  
  57.  
  58. define( 'LL_INTERCOM_VERSION', '0.7' );
  59.  
  60.  
  61.  
  62. ll_intercom::get_instance();
  63.  
  64.  
  65.  
  66. class ll_intercom {
  67.  
  68.  
  69.  
  70.     private static $instance = null;
  71.  
  72.  
  73.  
  74.     public static function get_instance() {
  75.  
  76.         if ( null == self::$instance ) {
  77.             self::$instance = new self;
  78.         }
  79.  
  80.         return self::$instance;
  81.  
  82.     }
  83.  
  84.  
  85.  
  86.     /**
  87.      * class constructor
  88.      * register the activation and de-activation hooks and hook into a bunch of actions
  89.      */
  90.     public function __construct() {
  91.  
  92.         register_activation_hook(   __FILE__, array( $this, 'hello'   ) );
  93.         register_deactivation_hook( __FILE__, array( $this, 'goodbye' ) );
  94.  
  95.         add_action( 'wp_footer',             array( $this, 'output_install_code'       ) );
  96.         add_action( 'admin_footer',          array( $this, 'output_admin_install_code' ) );
  97.         add_action( 'admin_menu',            array( $this, 'create_options_page'       ) );
  98.         add_action( 'network_admin_menu',    array( $this, 'create_options_page'       ) );
  99.         add_action( 'admin_menu',            array( $this, 'create_support_menu'       ) );
  100.         add_action( 'admin_init',            array( $this, 'settings_init'             ) );
  101.         add_action( 'admin_notices',         array( $this, 'notice'                    ) );
  102.         add_action( 'network_admin_notices', array( $this, 'notice'                    ) );
  103.  
  104.     }
  105.  
  106.  
  107.  
  108.     /**
  109.      * various initiation stuff when the plugin is activated
  110.      * @return null
  111.      */
  112.     function hello() {
  113.  
  114.         // add the 'hide from intercom' capability to the admin user
  115.  
  116.         $role = get_role( 'administrator' );
  117.         $role->add_cap( 'hide_from_intercom' );
  118.  
  119.     }
  120.  
  121.  
  122.  
  123.     /**
  124.      * stuff to do when the plugin is de-activated
  125.      * @return null
  126.      */
  127.     function goodbye() {
  128.  
  129.         // remove the 'hide from intercom' capability from the admin user
  130.  
  131.         $role = get_role( 'administrator' );
  132.         $role->remove_cap( 'hide_from_intercom' );
  133.  
  134.     }
  135.  
  136.  
  137.  
  138.     /**
  139.      * check if this plugin is activated network-wide
  140.      * @return boolean
  141.      */
  142.     function is_network_active() {
  143.  
  144.         if ( ! function_exists( 'is_plugin_active_for_network' ) )
  145.             require_once( ABSPATH . '/wp-admin/includes/plugin.php' );
  146.  
  147.         if ( is_plugin_active_for_network( plugin_basename( __FILE__ ) ) )
  148.             return true;
  149.  
  150.         return false;
  151.  
  152.     }
  153.  
  154.  
  155.  
  156.     /**
  157.      * retrieve the intercom options
  158.      * @return array 'll-intercom' options
  159.      */
  160.     function get_settings() {
  161.  
  162.         if ( self::is_network_active() )
  163.             return get_site_option( 'll-intercom' );
  164.  
  165.         return get_option( 'll-intercom' );
  166.  
  167.     }
  168.  
  169.  
  170.  
  171.     /**
  172.      * update the intercom options in the database
  173.      * @param  array $opts new options settings to save
  174.      * @return null
  175.      */
  176.     function update_settings( $opts ) {
  177.  
  178.         if ( is_network_admin() ) {
  179.             update_site_option( 'll-intercom', $opts );
  180.         } else {
  181.             update_option( 'll-intercom', $opts );
  182.         }
  183.  
  184.     }
  185.  
  186.  
  187.  
  188.     /**
  189.      * output the intercom javascript install code
  190.      * @return null
  191.      */
  192.     function output_install_code() {
  193.  
  194.         global $current_user;
  195.  
  196.         // don't do anything if the current user is hidden from intercom
  197.         // or is not logged in
  198.  
  199.         if ( current_user_can( 'hide_from_intercom' ) or !is_user_logged_in() )
  200.             return;
  201.  
  202.         // retrieve the options and user info
  203.         $opts = self::get_settings();
  204.         get_currentuserinfo();
  205.  
  206.         // don't do anything if the app id and secret key fields have not been set
  207.  
  208.         if ( !isset( $opts['app-id'] ) or !isset( $opts['secure'] ) or empty( $opts['app-id'] ) or empty( $opts['secure'] ) )
  209.             return;
  210.  
  211.         // if we're sending the user role as custom data then
  212.         // figure out the current user's role
  213.  
  214.         $role = false;
  215.  
  216.         if ( $opts['send-user-role'] ) {
  217.             $user = new WP_User( $current_user->ID );
  218.             if ( !empty( $user->roles ) and is_array( $user->roles ) ) {
  219.                 foreach ( $user->roles as $user_role ) {
  220.                     $role = $user_role;
  221.                 }
  222.             }
  223.         }
  224.  
  225.         // calculate the security hash using the user id
  226.  
  227.         $hash = hash_hmac(
  228.             'sha256',
  229.             $current_user->ID,
  230.             $opts['secure']
  231.             );
  232.  
  233.         // set the required username format
  234.  
  235.         switch ( $opts['username'] ) {
  236.             case 'firstlast' :
  237.                 $username = $current_user->user_firstname . ' ' . $current_user->user_lastname;
  238.             break;
  239.             default:
  240.                 $username = $current_user->display_name;
  241.             break;
  242.         }
  243.  
  244.         // generate the custom data if required
  245.  
  246.         $custom = array();
  247.  
  248.         if ( $role ) {
  249.             $custom['Role'] = $role;
  250.         }
  251.  
  252.         if ( $opts['send-user-url'] and isset( $current_user->user_url ) and !empty( $current_user->user_url ) ) {
  253.             $custom['Website'] = $current_user->user_url;
  254.         }
  255.  
  256.         // allow plugins/themes to add their own custom data
  257.  
  258.         $custom = apply_filters( 'll_intercom_custom_data', $custom );
  259.  
  260.         // use intercom's default activator but allow plugins/themes to specify their own
  261.  
  262.         $activator = apply_filters( 'll_intercom_activator', '#IntercomDefaultWidget' );
  263.  
  264.         // now put everything together and generate the javascript output
  265.  
  266.         $settings = array(
  267.             'app_id'     => $opts['app-id'],
  268.             'user_id'    => $current_user->ID,
  269.             'email'      => $current_user->user_email,
  270.             'name'       => $username,
  271.             'created_at' => strtotime( $current_user->user_registered ),
  272.             'user_hash'  => $hash,
  273.             'widget'     => (object) array(
  274.                 'activator' => $activator
  275.                 )
  276.             );
  277.  
  278.         if ( ! empty( $custom ) ) {
  279.             foreach ( $custom as $k => $v ) {
  280.                 $settings[$k] = $v;
  281.             }
  282.         }
  283.  
  284.         $out  = '<script id="IntercomSettingsScriptTag">';
  285.         $out .= '// Intercom for WordPress | v' . LL_INTERCOM_VERSION . ' | http://wordpress.org/plugins/intercom-for-wordpress' . "\n";
  286.         $out .= 'window.intercomSettings = ' . json_encode( (object) $settings ) . ';' . "\n";
  287.         $out .= '</script>' . "\n";
  288.         $out .= '<script>(function(){var w=window;var ic=w.Intercom;if(typeof ic==="function"){ic(\'reattach_activator\');ic(\'update\',intercomSettings);}else{var d=document;var i=function(){i.c(arguments)};i.q=[];i.c=function(args){i.q.push(args)};w.Intercom=i;function l(){var s=d.createElement(\'script\');s.type=\'text/javascript\';s.async=true;s.src=\'https://static.intercomcdn.com/intercom.v1.js\';var x=d.getElementsByTagName(\'script\')[0];x.parentNode.insertBefore(s,x);}if(w.attachEvent){w.attachEvent(\'onload\',l);}else{w.addEventListener(\'load\',l,false);}};})()</script>' . "\n";
  289.  
  290.         echo $out;
  291.  
  292.     }
  293.  
  294.  
  295.  
  296.     /**
  297.      * check the options and if required output the install code in the admin footer
  298.      * @return null
  299.      */
  300.     function output_admin_install_code() {
  301.  
  302.         $opts = self::get_settings();
  303.  
  304.         if ( $opts['show-in-admin'] ) {
  305.             self::output_install_code();
  306.         }
  307.  
  308.     }
  309.  
  310.  
  311.     /**
  312.      * show a 'settings saved' notice
  313.      * and a friendly reminder if the app ID or secret key haven't been entered
  314.      * @return null
  315.      */
  316.     function notice() {
  317.  
  318.         if ( isset( $_GET['page'] ) and ( 'intercom' == $_GET['page'] ) ) {
  319.  
  320.             if ( is_network_admin() and isset( $_GET['updated'] ) ) { ?>
  321.                 <div class="updated" id="ll-intercom-updated"><p><?php _e( 'Settings saved.' ); ?></p></div>
  322.                 <?php
  323.             }
  324.  
  325.         }
  326.  
  327.         // show a reminder to users who can update options
  328.  
  329.         if ( ! current_user_can( 'manage_options' ) )
  330.             return;
  331.  
  332.         $opts = self::get_settings();
  333.  
  334.         if ( !is_network_admin() and ( !isset( $opts['app-id'] ) or empty( $opts['app-id'] ) or !isset( $opts['secure'] ) or empty( $opts['secure'] ) ) ) {
  335.             echo '<div class="error" id="ll-intercom-notice"><p><strong>Intercom needs some attention</strong>. ';
  336.             if ( isset( $_GET['page'] ) and 'intercom' == $_GET['page'] ) {
  337.                 echo 'Please enter your Intercom application ID and secret key';
  338.             } else {
  339.                 echo 'Please <a href="options-general.php?page=intercom">configure the Intercom settings</a>';
  340.             }
  341.             echo ' to start tracking your users.</p></div>' . "\n";
  342.         }
  343.  
  344.     }
  345.  
  346.  
  347.  
  348.     /**
  349.      * create the relevant type of options page
  350.      * depending if we're single site or network active
  351.      * @return null
  352.      */
  353.     function create_options_page() {
  354.  
  355.         // annoyingly multisite doesn't play nicely with the settings api
  356.         // so we need to account for that by creating a special page
  357.  
  358.         if ( self::is_network_active() ) {
  359.  
  360.             add_submenu_page(
  361.                 'settings.php',
  362.                 'Intercom Settings',
  363.                 'Intercom',
  364.                 'manage_network_options',
  365.                 'intercom',
  366.                 array( $this, 'render_options_page' )
  367.                 );
  368.  
  369.         } else {
  370.  
  371.             add_options_page(
  372.                 'Intercom Settings',
  373.                 'Intercom',
  374.                 'manage_options',
  375.                 'intercom',
  376.                 array( $this, 'render_options_page' )
  377.                 );
  378.  
  379.         }
  380.  
  381.     }
  382.  
  383.     /**
  384.      * create the support menu link
  385.      *
  386.      * @return null
  387.      */
  388.     function create_support_menu() {
  389.  
  390.         add_filter( 'll_intercom_activator', 'pl_intercom_activator' );
  391.        
  392.         function pl_intercom_activator( $activator ) {
  393.             return '.toplevel_page_support';
  394.         }
  395.  
  396.         $opts = self::get_settings();
  397.  
  398.         if ($opts['create-support-menu']){
  399.             add_menu_page(
  400.                 'Support',
  401.                 'Support',
  402.                 'edit_posts',
  403.                 'support', //the slug
  404.                 '', //the function
  405.                 '', //the icon location
  406.                 29
  407.                 );
  408.         }
  409.  
  410.     }
  411.  
  412.     /**
  413.      * output the options page
  414.      * @return null
  415.      */
  416.     function render_options_page() {
  417.  
  418.         $opts = self::get_settings();
  419.  
  420.         $action = is_network_admin() ? 'settings.php?page=intercom' : 'options.php';
  421.  
  422.         ?>
  423.  
  424.         <div class="wrap">
  425.  
  426.         <?php screen_icon( 'options-general' ); ?>
  427.         <h2>Intercom for WordPress Configuration</h2>
  428.  
  429.         <div class="postbox-container" style="width:65%;">
  430.  
  431.             <form method="post" action="<?php echo $action; ?>">
  432.  
  433.                 <?php settings_fields( 'intercom' ); ?>
  434.  
  435.                 <table class="form-table">
  436.                     <tbody>
  437.  
  438.                         <tr valign="top">
  439.                             <th scope="row">App ID</th>
  440.                             <td>
  441.                                 <input name="ll-intercom[app-id]" type="text" value="<?php echo esc_attr( $opts['app-id'] ); ?>">
  442.                             </td>
  443.                         </tr>
  444.  
  445.                         <tr valign="top">
  446.                             <th scope="row">Secret key</th>
  447.                             <td>
  448.                                 <input name="ll-intercom[secure]" type="text" value="<?php echo esc_attr( $opts['secure'] ); ?>">
  449.                             </td>
  450.                         </tr>
  451.  
  452.                         <tr valign="top">
  453.                             <th scope="row">Username format</th>
  454.                             <td>
  455.                                 <label>
  456.                                     <input name="ll-intercom[username]" type="radio" value="firstlast" <?php checked( $opts['username'], 'firstlast' ); ?>>
  457.                                     <span>First name &amp; last name</span>
  458.                                 </label>
  459.                                 <br>
  460.                                 <label>
  461.                                     <input name="ll-intercom[username]" type="radio" value="display" <?php checked( $opts['username'], 'display' ); ?>>
  462.                                     <span>Display name</span>
  463.                                 </label>
  464.                             </td>
  465.                         </tr>
  466.  
  467.                         <tr valign="top">
  468.                             <th scope="row">Send user role?</th>
  469.                             <td>
  470.                                 <input name="ll-intercom[send-user-role]" type="checkbox" value="1" <?php checked( $opts['send-user-role'] ); ?>>
  471.                             </td>
  472.                         </tr>
  473.  
  474.                         <tr valign="top">
  475.                             <th scope="row">Send user website?</th>
  476.                             <td>
  477.                                 <input name="ll-intercom[send-user-url]" type="checkbox" value="1" <?php checked( $opts['send-user-url'] ); ?>>
  478.                             </td>
  479.                         </tr>
  480.  
  481.                         <tr valign="top">
  482.                             <th scope="row">Show on admin pages?</th>
  483.                             <td>
  484.                                 <input name="ll-intercom[show-in-admin]" type="checkbox" value="1" <?php checked( $opts['show-in-admin'] ); ?>>
  485.                             </td>
  486.                         </tr>
  487.  
  488.                         <tr valign="top">
  489.                             <th scope="row">Create support menu link?</th>
  490.                             <td>
  491.                                 <input name="ll-intercom[create-support-menu]" type="checkbox" value="1" <?php checked( $opts['create-support-menu'] ); ?>>
  492.                             </td>
  493.                         </tr>
  494.  
  495.                     </tbody>
  496.  
  497.                 </table>
  498.  
  499.                 <p class="submit">
  500.                     <input class="button-primary" name="ll-intercom-submit" type="submit" value="Save Settings">
  501.                 </p>
  502.  
  503.             </form>
  504.  
  505.         </div>
  506.  
  507.         <div class="postbox-container" style="width:20%;">
  508.  
  509.             <div class="metabox-holder">
  510.  
  511.                 <div class="meta-box-sortables" style="min-height:0;">
  512.                     <div class="postbox ll-intercom-info" id="ll-intercom-support">
  513.                         <h3 class="hndle"><span>Need Help?</span></h3>
  514.                         <div class="inside">
  515.                             <p>If something's not working, the first step is to read the <a href="http://wordpress.org/extend/plugins/intercom-for-wordpress/faq/">FAQ</a>.</p>
  516.                             <p>If your question is not answered there, please check the official <a href="http://wordpress.org/tags/intercom-for-wordpress?forum_id=10">support forum</a>.</p>
  517.                         </div>
  518.                     </div>
  519.                 </div>
  520.  
  521.                 <div class="meta-box-sortables" style="min-height:0;">
  522.                     <div class="postbox ll-intercom-info" id="ll-intercom-suggest">
  523.                         <h3 class="hndle"><span>Like this Plugin?</span></h3>
  524.                         <div class="inside">
  525.                             <p>If this plugin has helped you improve your customer relationships, please consider supporting it:</p>
  526.                             <ul>
  527.                                 <li><a href="http://wordpress.org/extend/plugins/intercom-for-wordpress/">Rate it and let other people know it works</a>.</li>
  528.                                 <li>Link to it or share it on Twitter or Facebook.</li>
  529.                                 <li>Write a review on your website or blog.</li>
  530.                                 <li><a href="https://twitter.com/lumpysimon">Follow me on Twitter</a></li>
  531.                                 <li><a href="http://lumpylemon.co.uk/">Commission me</a> for WordPress development, plugin or design work.</li>
  532.                             </ul>
  533.                         </div>
  534.                     </div>
  535.                 </div>
  536.  
  537.             </div>
  538.  
  539.         </div>
  540.         </div>
  541.         <?php
  542.  
  543.     }
  544.  
  545.  
  546.  
  547.     /**
  548.      * use the WordPress settings api to initiate the various settings
  549.      * and if it's a network settings page then validate & update any submitted settings
  550.      * @return null
  551.      */
  552.     function settings_init() {
  553.  
  554.         register_setting( 'intercom', 'll-intercom', array( $this, 'validate' ) );
  555.         if ( isset( $_REQUEST['_wpnonce'] ) and wp_verify_nonce( $_REQUEST['_wpnonce'], 'intercom-options' ) ) {
  556.  
  557.             $file = is_network_admin() ? 'settings.php' : 'options-general.php';
  558.  
  559.             if ( isset( $_POST['ll-intercom-submit'] ) and is_network_admin() ) {
  560.                 $opts = self::validate( $_POST['ll-intercom'] );
  561.                 self::update_settings( $opts );
  562.                 wp_redirect( add_query_arg( array(
  563.                     'page'    => 'intercom',
  564.                     'updated' => true
  565.                     ), $file ) );
  566.                 die();
  567.             }
  568.  
  569.         }
  570.  
  571.     }
  572.  
  573.  
  574.  
  575.     /**
  576.      * make sure that no dodgy stuff is trying to sneak through
  577.      * @param  array $input options to validate
  578.      * @return array        validated options
  579.      */
  580.     function validate( $input ) {
  581.  
  582.         $new['app-id']         = wp_kses( trim( $input['app-id'] ), array() );
  583.         $new['secure']         = wp_kses( trim( $input['secure'] ), array() );
  584.         $new['username']       = isset( $input['username'] ) ? wp_kses( trim( $input['username'] ), array() ) : 'firstlast';
  585.         $new['send-user-role'] = absint( $input['send-user-role'] );
  586.         $new['send-user-url']  = absint( $input['send-user-url'] );
  587.         $new['show-in-admin']  = absint( $input['show-in-admin'] );
  588.         $new['create-support-menu']  = absint( $input['create-support-menu'] );
  589.  
  590.         return $new;
  591.  
  592.     }
  593.  
  594.  
  595.  
  596. } // class
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement