Overview

Packages

  • ACF
  • Automation
    • Actions
    • Activation
    • LeadProfile
    • Menus
    • Queries
    • Rules
    • Triggers
  • CTA
    • Activation
    • AJAXListeners
    • Cloning
    • Core
    • Display
    • EditPage
    • Enqueues
    • GPLSettings
    • LeadsProfile
    • Loading
    • Menus
    • Notices
    • PlacementMetabox
    • PostType
    • Templates
    • Tracking
    • Variations
    • YOAST
  • InboundPro
    • Activation
    • AdminAJAX
    • AutoLoading
    • Cron
    • DataInterface
    • FieldMapping
    • Funnels
    • InboundNowAPI
    • Installer
    • LeadStatuses
    • Menus
    • Notifications
    • Ouath
    • Reports
    • Settings
    • Tracking
    • Translations
    • Updates
  • LandingPages
    • Activation
    • DataInterfaces
    • Management
    • Menus
    • NeedsAttention
    • Notices
    • Sidebars
    • Templates
    • Tracking
    • Variations
    • WelcomeScreen
    • Widgets
  • Leads
    • Activation
    • Akismet
    • BranchControl
    • BulkActions
    • Dashboard
    • Management
    • Notifications
    • Settings
    • Tracking
    • UserProfile
    • WPLogin
  • Mailer
    • Activation
    • Admin
    • ClickTracking
    • DataInterface
    • InboundForms
    • Leads
    • Management
    • Notifications
    • Scheduling
    • Sending
    • Settings
    • ShortcodesAndTokens
    • SparkPost
    • Templates
    • Unsubscribes
    • Variations
    • YoastSEO
  • None
  • Shared
    • APIs
    • CPT
    • DatabaseRoutines
    • DebugTricks
    • DoubleOptin
    • EventManagement
    • FieldMapping
    • InboundForms
    • LeadSorage
    • MarketingButton
    • Menus
    • Notifications
    • Shortcodes
    • ShortcodesAndTokens
    • StandAloneLicensing
    • Tracking
    • VisualEditor
  • xDepreciated

Classes

  • CTA_Activation
  • CTA_Activation_Update_Routines
  • CTA_Admin_Notices
  • CTA_Ajax_Listeners
  • CTA_Clone_Post
  • CTA_Conversion_Tracking
  • CTA_Dynamic_Widget
  • CTA_Enqueues
  • CTA_Load_Extensions
  • CTA_Menus
  • CTA_Metaboxes
  • CTA_Metaboxes_Global
  • CTA_Post_Type
  • CTA_Render
  • CTA_Settings
  • CTA_Static_Widget
  • CTA_Template_Manager
  • CTA_Template_Manager_List
  • CTA_Variations
  • CTA_WordPress_Leads
  • CTA_WordPress_SEO
  • Inbound_Ajax
  • Inbound_Akismet
  • Inbound_Analytics
  • Inbound_API
  • Inbound_API_Keys_Generation
  • Inbound_API_Keys_Table
  • Inbound_API_Wrapper
  • Inbound_Asset_Loader
  • Inbound_Automation_Action_Add_Remove_List
  • Inbound_Automation_Action_Add_Remove_Tag
  • Inbound_Automation_Action_Create_Lead
  • Inbound_Automation_Action_Create_User
  • Inbound_Automation_Action_Kill_Tasks
  • Inbound_Automation_Action_Relay_Data
  • Inbound_Automation_Action_Send_Email
  • Inbound_Automation_Action_Wait
  • Inbound_Automation_Activation
  • Inbound_Automation_Activation_Update_Routines
  • Inbound_Automation_Adminbar
  • Inbound_Automation_Lead_Profile
  • Inbound_Automation_Loader
  • Inbound_Automation_Plugin
  • Inbound_Automation_Post_Type
  • Inbound_Automation_Processing
  • Inbound_Automation_Query_Lead
  • Inbound_Automation_Query_User
  • Inbound_Automation_Trigger_inbound_double_optin_confirm
  • Inbound_Automation_Trigger_inbound_store_lead_post
  • Inbound_Automation_Trigger_inbound_track_link
  • Inbound_Automation_Trigger_Publish_Post
  • Inbound_Automation_Trigger_Save_Post_Deactivated
  • Inbound_Automation_Trigger_Set_Object_Terms
  • Inbound_Automation_Trigger_Update_Lead
  • Inbound_Automation_Trigger_User_Register
  • Inbound_Calls_To_Action_Plugin
  • Inbound_Confirm_Double_Optin
  • Inbound_Cron
  • Inbound_CTA_Quick_View
  • Inbound_Customizer
  • Inbound_Debug_Scripts
  • Inbound_Email_Meta
  • Inbound_Email_Preview
  • Inbound_Email_Template_Shortcodes
  • Inbound_Event_Report
  • Inbound_Events
  • Inbound_Events_Report
  • Inbound_Extension_Loads
  • Inbound_Forms
  • Inbound_Funnel_Reporting
  • Inbound_Impressions_Report
  • Inbound_Landing_Pages_Plugin
  • Inbound_Lead_Statuses
  • Inbound_Leads
  • Inbound_Leads_Custom_fields
  • Inbound_Leads_Plugin
  • Inbound_License
  • Inbound_List_Double_Optin
  • Inbound_Load_Shared
  • Inbound_Logging_Automation
  • Inbound_Login
  • Inbound_Mail_Daemon
  • Inbound_Mailer_ACF
  • Inbound_Mailer_Activation
  • Inbound_Mailer_Activation_Update_Routines
  • Inbound_Mailer_Ajax_Listeners
  • Inbound_Mailer_Clone_Post
  • Inbound_Mailer_Common_Settings
  • Inbound_Mailer_Customizer
  • Inbound_Mailer_Direct_Email_Leads
  • Inbound_Mailer_Enqueues
  • Inbound_Mailer_Forms_Integration
  • Inbound_Mailer_Load_Templates
  • Inbound_Mailer_Menus
  • Inbound_Mailer_Metaboxes
  • Inbound_Mailer_Notifications
  • Inbound_Mailer_Plugin
  • Inbound_Mailer_Post_Type
  • Inbound_Mailer_Scheduling
  • Inbound_Mailer_Settings
  • Inbound_Mailer_SparkPost
  • Inbound_Mailer_Stats_Report
  • Inbound_Mailer_Template_Manager
  • Inbound_Mailer_Template_Manager_List
  • Inbound_Mailer_Tokens
  • Inbound_Mailer_Tracking
  • Inbound_Mailer_Unsubscribe
  • Inbound_Mailer_Variations
  • Inbound_Mailer_WordPress_SEO
  • Inbound_Maintenance_Lists
  • Inbound_Marketing_Button
  • Inbound_Menus_Admin
  • Inbound_Menus_Adminbar
  • Inbound_Metaboxes_Automation
  • Inbound_Metaboxes_Leads
  • Inbound_Notifications
  • Inbound_Now_Store
  • Inbound_Options_API
  • Inbound_Pro_Activation
  • Inbound_Pro_Admin_Ajax_Listeners
  • Inbound_Pro_Automatic_Updates
  • Inbound_Pro_Downloads
  • Inbound_Pro_Notifications
  • Inbound_Pro_Oauth_Engine
  • Inbound_Pro_Plugin
  • Inbound_Pro_Settings
  • Inbound_Quick_View
  • Inbound_Reporting_Templates
  • Inbound_Search_And_Comment_Report
  • Inbound_Shared_ACF_BootStrap
  • Inbound_Shortcodes
  • Inbound_Shortcodes_Cookies
  • Inbound_Shortcodes_Fields
  • Inbound_SparkPost
  • Inbound_SparkPost_Stats
  • Inbound_Template_Utils
  • Inbound_Templating_Engine
  • Inbound_Tracking
  • Inbound_Translation_Updater
  • Inbound_Updater
  • Inbound_Upgrade_For_More_Reports
  • Inbound_Upgrade_Routines
  • Inbound_Visitor_Event_Report
  • Inbound_Visitor_Impressions_Report
  • Inbound_Visitors_Report
  • INBOUNDNOW_EXTEND
  • Landing_Pages_ACF
  • Landing_Pages_Activation
  • Landing_Pages_Activation_Update_Routines
  • Landing_Pages_Admin_Menus
  • Landing_Pages_Admin_Notices
  • Landing_Pages_Install
  • Landing_Pages_Load_Extensions
  • Landing_Pages_Meta
  • Landing_Pages_Metaboxes
  • Landing_Pages_Post_Type
  • Landing_Pages_Row_Actions
  • Landing_Pages_Settings
  • Landing_Pages_Sidebars
  • Landing_Pages_Split_Testing_Stats
  • Landing_Pages_Template_Management
  • Landing_Pages_Template_Switcher
  • Landing_Pages_Templates_List_Table
  • Landing_Pages_Variations
  • LandingPages_Welcome
  • Leads_Activation
  • Leads_Activation_Update_Routines
  • Leads_Admin_Notices
  • Leads_Batch_Processor
  • Leads_Branching
  • Leads_Dashboard
  • Leads_Field_Map
  • Leads_Manager
  • Leads_Post_Type
  • Leads_Settings
  • Leads_Tracking
  • Leads_User_Profile
  • LeadStorage
  • LP_EXTENSION_LICENSENING
  • LP_EXTENSION_UPDATER
  • LP_Variation_Rotation
  • LP_Widget_Conversion_Area

Functions

  • cta_check_active
  • cta_example_template_function
  • cta_load_dynamic_widget
  • CTA_Load_Extensions
  • cta_load_static_widget
  • CTA_Render
  • inbound_add_conversion_to_lead
  • inbound_automation_check_active
  • inbound_automation_load_definitions
  • inbound_automation_metaboxes
  • inbound_automation_processing
  • inbound_color
  • inbound_color_scheme
  • inbound_confirm_email_service_provider
  • inbound_email_kill_ie8
  • inbound_email_load_templates
  • inbound_form_add_lead_list
  • inbound_form_auto_publish
  • inbound_form_delete_transient
  • inbound_form_get_data
  • inbound_form_save
  • inbound_forms_change_columns
  • inbound_forms_cpt
  • inbound_forms_custom_columns
  • inbound_forms_default_content
  • inbound_get_form_names
  • inbound_get_parent_directory
  • inbound_Hex_2_RGB
  • inbound_inbound_email_example_template_function
  • inbound_load_lead_manager
  • inbound_load_token_engine
  • inbound_mailer_acf_integration
  • Inbound_Mailer_Common_Settings
  • Inbound_Mailer_Load_Templates
  • inbound_qtrans_disable
  • inbound_record_log
  • inbound_register_form_role_capabilities
  • inbound_shortcode_alert
  • inbound_shortcode_callout
  • inbound_shortcode_columns
  • inbound_shortcode_content_box
  • inbound_shortcode_divider
  • inbound_shortcode_empty_paragraph_fix
  • inbound_shortcode_four_fifth_columns
  • inbound_shortcode_full_columns
  • inbound_shortcode_gmap
  • inbound_shortcode_icon
  • inbound_shortcode_intro
  • inbound_shortcode_landing_pages
  • inbound_shortcode_leadp
  • inbound_shortcode_list_icons
  • inbound_shortcode_one_fifth_columns
  • inbound_shortcode_one_fourth_columns
  • inbound_shortcode_one_half_columns
  • inbound_shortcode_one_third_columns
  • inbound_shortcode_portfolio
  • inbound_shortcode_pricing
  • inbound_shortcode_profile
  • inbound_shortcode_social_links
  • inbound_shortcode_tabs
  • inbound_shortcode_teaser
  • inbound_shortcode_testimonial
  • inbound_shortcode_three_fifth_columns
  • inbound_shortcode_three_fourth_columns
  • inbound_shortcode_two_fifth_columns
  • inbound_shortcode_two_third_columns
  • inbound_shortcode_video
  • inbound_store_lead
  • inbound_template_brightness
  • inbound_template_color
  • Inbound_Templating_Engine
  • inbound_travis_check
  • inboundnow_add_master_license
  • json_encode_fallback
  • landingpage_fix_known_wp_title_isses
  • landingpages_is_active
  • load_CTA_Settings
  • load_CTA_Template_Manager
  • load_Inbound_API_Keys_Generation
  • load_Inbound_Lead_Statuses
  • load_Inbound_Leads_Custom_fields
  • load_inbound_mail_daemon
  • load_Inbound_Mailer_Settings
  • load_Inbound_Mailer_Template_Manager
  • load_Inbound_Pro_Downloads
  • lp_ab_testing_get_current_variation_id
  • lp_add_option
  • lp_body_class
  • lp_check_active
  • lp_content_area
  • lp_conversion_area
  • lp_discover_important_wrappers
  • lp_footer
  • lp_generate_drowndown
  • lp_get_parent_directory
  • lp_get_value
  • lp_global_config
  • lp_head
  • lp_in_admin_header
  • lp_init
  • lp_list_feature
  • lp_load_widgets
  • lp_main_headline
  • lp_manage_templates
  • lp_move_template_files
  • lp_rebuild_attributes
  • lp_templates_upload
  • mailer_check_active
  • wp_cta_check_active
  • wp_cta_kill_ie8
  • wp_cta_load_calls_to_action
  • wp_leads_get_page_final_id
  • wpl_url_to_postid
  • wpleads_add_lead_to_list
  • wpleads_check_active
  • wpleads_get_lead_lists_as_array
  • Overview
  • Package
  • Class
  1:   2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12:  13:  14:  15:  16:  17:  18:  19:  20:  21:  22:  23:  24:  25:  26:  27:  28:  29:  30:  31:  32:  33:  34:  35:  36:  37:  38:  39:  40:  41:  42:  43:  44:  45:  46:  47:  48:  49:  50:  51:  52:  53:  54:  55:  56:  57:  58:  59:  60:  61:  62:  63:  64:  65:  66:  67:  68:  69:  70:  71:  72:  73:  74:  75:  76:  77:  78:  79:  80:  81:  82:  83:  84:  85:  86:  87:  88:  89:  90:  91:  92:  93:  94:  95:  96:  97:  98:  99: 100: 101: 102: 103: 104: 105: 106: 107: 108: 109: 110: 111: 112: 113: 114: 115: 116: 117: 118: 119: 120: 121: 122: 123: 124: 125: 126: 127: 128: 129: 130: 131: 132: 133: 134: 135: 136: 137: 138: 139: 140: 141: 142: 143: 144: 145: 146: 147: 148: 149: 150: 151: 152: 153: 154: 155: 156: 157: 158: 159: 160: 161: 162: 163: 164: 165: 166: 167: 168: 169: 170: 171: 172: 173: 174: 175: 176: 177: 178: 179: 180: 181: 182: 183: 184: 185: 186: 187: 188: 189: 190: 191: 192: 193: 194: 195: 196: 197: 198: 199: 200: 201: 202: 203: 204: 205: 206: 207: 208: 209: 210: 211: 212: 213: 214: 215: 216: 217: 218: 219: 220: 221: 222: 223: 224: 225: 226: 227: 228: 229: 230: 231: 232: 233: 234: 235: 236: 237: 238: 239: 240: 241: 242: 243: 244: 245: 246: 247: 248: 249: 250: 251: 252: 253: 254: 255: 256: 257: 258: 259: 260: 261: 262: 263: 264: 265: 266: 267: 268: 269: 270: 271: 272: 273: 274: 275: 276: 277: 278: 279: 280: 281: 282: 283: 284: 285: 286: 287: 288: 289: 290: 291: 292: 293: 294: 295: 296: 297: 298: 299: 300: 301: 302: 303: 304: 305: 306: 307: 308: 309: 310: 311: 312: 313: 314: 315: 316: 317: 318: 319: 320: 321: 322: 323: 324: 325: 326: 327: 328: 329: 330: 331: 332: 333: 334: 335: 336: 337: 338: 339: 340: 341: 342: 343: 344: 345: 346: 347: 348: 349: 350: 351: 352: 353: 354: 355: 356: 357: 358: 359: 360: 361: 362: 363: 364: 365: 366: 367: 368: 369: 370: 371: 372: 373: 374: 375: 376: 377: 378: 379: 380: 381: 382: 383: 384: 385: 386: 387: 388: 389: 390: 391: 392: 393: 394: 395: 396: 397: 398: 399: 400: 401: 402: 403: 404: 405: 406: 407: 408: 409: 410: 411: 412: 413: 414: 415: 416: 417: 418: 419: 420: 421: 422: 423: 424: 425: 426: 427: 428: 429: 430: 431: 432: 433: 434: 435: 436: 437: 438: 439: 440: 441: 442: 443: 444: 445: 446: 447: 448: 449: 450: 451: 452: 453: 454: 455: 456: 457: 458: 459: 460: 461: 462: 463: 464: 465: 466: 467: 468: 469: 470: 471: 472: 473: 474: 475: 476: 477: 478: 479: 480: 481: 482: 483: 484: 485: 486: 487: 488: 489: 490: 491: 492: 493: 494: 495: 496: 497: 498: 499: 500: 501: 502: 503: 504: 505: 506: 507: 508: 509: 510: 511: 512: 513: 514: 515: 516: 517: 518: 519: 520: 521: 522: 523: 524: 525: 526: 527: 528: 529: 530: 531: 532: 533: 534: 535: 536: 537: 538: 539: 540: 541: 542: 543: 544: 545: 546: 547: 548: 549: 550: 551: 552: 553: 554: 555: 556: 557: 558: 559: 560: 561: 562: 563: 564: 565: 566: 567: 568: 569: 570: 571: 572: 573: 574: 575: 576: 577: 578: 579: 580: 581: 582: 583: 584: 585: 586: 587: 588: 589: 590: 591: 592: 593: 594: 595: 596: 597: 598: 599: 600: 601: 602: 603: 604: 605: 606: 607: 608: 609: 610: 611: 612: 613: 614: 615: 616: 617: 618: 619: 620: 621: 622: 623: 624: 625: 626: 627: 628: 629: 630: 631: 632: 633: 634: 635: 636: 637: 638: 639: 640: 641: 642: 643: 644: 645: 646: 647: 648: 649: 650: 651: 652: 653: 654: 655: 656: 657: 658: 659: 660: 661: 662: 663: 664: 665: 666: 667: 668: 669: 670: 671: 672: 673: 674: 675: 676: 677: 678: 679: 680: 681: 682: 683: 684: 685: 686: 687: 688: 689: 690: 691: 692: 693: 694: 695: 696: 697: 698: 699: 700: 701: 702: 703: 704: 705: 706: 707: 708: 709: 710: 711: 712: 713: 714: 715: 716: 717: 718: 719: 720: 721: 722: 723: 724: 725: 726: 727: 728: 729: 730: 731: 732: 733: 734: 735: 736: 737: 738: 739: 740: 741: 742: 743: 744: 745: 746: 747: 748: 749: 750: 751: 752: 753: 754: 755: 756: 757: 758: 759: 760: 761: 762: 763: 764: 765: 766: 767: 768: 769: 770: 771: 772: 773: 774: 775: 776: 777: 778: 779: 780: 781: 782: 783: 784: 785: 786: 787: 788: 789: 790: 791: 792: 793: 794: 795: 796: 797: 798: 799: 800: 801: 802: 803: 804: 805: 806: 807: 808: 809: 810: 811: 812: 813: 814: 815: 816: 817: 818: 819: 820: 821: 822: 823: 824: 825: 826: 827: 828: 829: 830: 831: 832: 833: 834: 835: 836: 837: 838: 839: 840: 841: 842: 843: 844: 845: 846: 847: 848: 849: 850: 851: 852: 853: 854: 855: 856: 857: 858: 859: 860: 861: 862: 863: 864: 865: 866: 867: 868: 869: 870: 871: 872: 873: 874: 875: 876: 877: 878: 879: 880: 881: 882: 883: 884: 885: 886: 887: 888: 889: 890: 891: 892: 893: 894: 
<?php

/**
 * Class improves the wp-lead CPT listing page
 *
 * @package Leads
 * @subpackage Management
 */


class Leads_Post_Type {

    /**
     * Initiate class
     */
    public function __construct() {
        self::load_hooks();
    }

    /**
     * Load hooks and filters
     */
    public static function load_hooks() {

        /* add custom columns to wp-lead post type */
        add_filter("manage_wp-lead_posts_columns", array(__CLASS__, 'register_columns'));

        /* process custom columns */
        add_action("manage_posts_custom_column", array(__CLASS__, 'render_columns'), 10, 2);

        /* setup column sorting */
        add_filter("manage_edit-wp-lead_sortable_columns", array(__CLASS__, 'define_sortable_columns'));
        add_action('posts_clauses', array(__CLASS__, 'process_column_sorting'), 1, 2);

        /* modify row actions */
        add_filter('post_row_actions', array(__CLASS__, 'filter_row_actions'), 10, 2);

        /* adds ability to filter leads by custom taxonomy */
        add_action('restrict_manage_posts', array(__CLASS__, 'define_filters'));
        add_filter('parse_query', array(__CLASS__, 'process_filters'), 10, 1);

        /* setup bulk edit options */
        add_action('admin_footer-edit.php', array(__CLASS__, 'register_bulk_edit_fields'));

        /* process bulk actions  */
        add_action('load-edit.php', array(__CLASS__, 'process_bulk_actions'));

         /* record last time a piece of meta data was updated */
        add_action('updated_post_meta', array(__CLASS__, 'record_meta_update'), 10, 4);

        /* redirect lead notification email links to lead profile */
        add_action('admin_init', array(__CLASS__, 'redirect_email_profile_links'));

        /* gets json object of all lead meta data */
        add_action('wp_ajax_inbound_get_all_lead_data', array(__CLASS__, 'ajax_get_all_lead_data'));

        /* mark lead status as read */
        add_action('wp_ajax_wp_leads_mark_as_read_save', array(__CLASS__, 'ajax_mark_lead_as_read'));
        /* mark lead status as unread */
        add_action('wp_ajax_wp_leads_mark_as_unread_save', array(__CLASS__, 'ajax_mark_lead_as_unread'));
        /* mark lead status as read on first open */
        add_action('wp_ajax_wp_leads_auto_mark_as_read', array(__CLASS__, 'ajax_auto_mark_as_read'));
        /* mark lead status as read and stop waiting for the lead to opt into lists*/
        add_action('wp_ajax_wp_leads_stop_waiting_for_double_optin', array(__CLASS__, 'ajax_stop_waiting_for_double_optin'));
        
        /* add extra menu items */
        add_action('admin_menu', array(__CLASS__, 'setup_admin_menu'));

        /* enqueue scripts and styles in admin  */
        add_action('admin_enqueue_scripts', array(__CLASS__, 'enqueue_admin_scripts'));

        /* trash events when lead trashed */
        add_action('trash_wp-lead' , array( __CLASS__ , 'trash_lead_actions'));

        /* delete events when lead deleted */
        add_action('delete_post' , array( __CLASS__ , 'delete_lead_actions'));

    }

    /**
     * Register custom columns for the wp-lead post type
     * @param $cols
     * @return array
     */
    public static function register_columns($cols) {

        $cols = array(
            "cb" => "<input type=\"checkbox\" />",
            "lead-picture" => __('Lead', 'inbound-pro' ),
            "first-name" => __('First Name', 'inbound-pro' ),
            "last-name" => __('Last Name', 'inbound-pro' ),
            "title" => __('Email', 'inbound-pro' ),
            "status" => __('Status', 'inbound-pro' ),
            'action-count' => (class_exists('Inbound_Analytics')) ? __('Logs', 'inbound-pro' ) : __('Events', 'inbound-pro' ),
            "page-views" => __('Page Views', 'inbound-pro' ),
            "modified" => __('Updated', 'inbound-pro' )
        );

        /* not sure about this, needs documentation - H */
        if (isset($_GET['wp_leads_filter_field']) && $_GET['wp_leads_filter_field'] != "") {
            $the_val = $_GET['wp_leads_filter_field'];
            $nice_names = wpl_nice_field_names();
            if (array_key_exists($the_val, $nice_names)) {
                $the_val = $nice_names[$the_val];
            }
            $cols['custom'] = $the_val;
        }

        return $cols;
    }

    /**
     * Renders custom columns for wp-lead post type
     * @param $column
     * @param $post_id
     * @return mixed
     */
    public static function render_columns($column, $lead_id) {
        global $post;

        if ($post->post_type != 'wp-lead') {
            return $column;
        }

        switch ($column) {
            case "lead-picture":
                $gravatar = self::get_gravatar($post->ID);
                echo '<img class="lead-grav-img" width="50" height="50" src="' . $gravatar . '">';
                break;
            case "first-name":
                $first_name = get_post_meta($lead_id, 'wpleads_first_name', true);
                if (!$first_name || $first_name == 'false') {
                    $first_name = __('n/a', 'inbound-pro' );
                }
                echo $first_name;
                break;
            case "last-name":
                $last_name = get_post_meta($lead_id, 'wpleads_last_name', true);
                if (!$last_name) {
                    $last_name = __('n/a', 'inbound-pro' );
                }
                echo $last_name;
                break;
            case "status":
                $lead_status = get_post_meta($lead_id, 'wp_lead_status', true);
                self::display_status_pill($lead_status);
                break;
            case "action-count":
                if (class_exists('Inbound_Analytics')) {
                    $actions = Inbound_Events::get_total_activity($lead_id , 'any' , array());
                    ?>
                    <a href='<?php echo admin_url('index.php?action=inbound_generate_report&class=Inbound_Events_Report&range=10000&lead_id='.$post->ID.'&show_graph=false&title='.__('Logs','inbound-pro') .'&tb_hide_nav=true&TB_iframe=true&width=1000&height=600'); ?>' class='thickbox inbound-thickbox' title="<?php echo  sprintf(__('past %s days','inbound-pro') , 99999 ); ?>">
                        <?php echo $actions; ?>
                    </a>
                    <?php
                } else {
                    $actions = Inbound_Events::get_total_activity($lead_id , 'any' , array('inbound_list_add','sparkpost_delivery'));
                    echo $actions;
                }
                break;
            case "custom":
                if (isset($_GET['wp_leads_filter_field'])) {
                    $the_val = $_GET['wp_leads_filter_field'];
                }
                $custom_val = get_post_meta($lead_id, $the_val, true);
                if (!$custom_val) {
                    $custom_val = 'N/A';
                }
                echo $custom_val;
                break;
            case "page-views":
                $page_view_count = Inbound_Events::get_page_views_count($lead_id);
                if (class_exists('Inbound_Analytics')) {
                    ?>
                    <a href='<?php echo admin_url('index.php?action=inbound_generate_report&class=Inbound_Visitor_Impressions_Report&range=10000&lead_id='.$post->ID.'&show_graph=false&title='.__('Logs','inbound-pro') .'&tb_hide_nav=true&TB_iframe=true&width=1000&height=600'); ?>' class='thickbox inbound-thickbox' title="<?php echo  sprintf(__('past %s days','inbound-pro') , 99999 ); ?>">
                        <?php echo $page_view_count; ?>
                    </a>
                    <?php
                } else {
                    echo($page_view_count ? $page_view_count : 0);
                }


                break;
            case "company":
                $company = get_post_meta($lead_id, 'wpleads_company_name', true);
                echo $company;
                break;
            case 'modified':
                $m_orig = get_post_field('post_modified', $lead_id, 'raw');
                $m_stamp = strtotime($m_orig);
                $modified = date('n/j/y g:i a', $m_stamp);

                echo '<p class="mod-date">';
                echo '<em>' . $modified . '</em><br />';
                echo '</p>';
                break;
        }
    }

    /**
     * Defines sortable columns
     * @param $columns
     * @return mixed
     */
    public static function define_sortable_columns($columns) {

        $columns['first-name'] = 'first-name';
        $columns['last-name'] = 'last-name';
        $columns['status'] = 'status';
        $columns['company'] = 'company';
        $columns['modified'] = 'modified';
        $columns['action-count'] = 'action-count';
        $columns['page-views'] = 'page-views';
        if (isset($_GET['wp_leads_filter_field'])) {
            $the_val = $_GET['wp_leads_filter_field'];
            $columns['custom'] = $the_val;
        }


        return $columns;
    }

    /**
     * Adds custom taxonomies of wp-lead post type to filter controls
     */
    public static function define_filters() {
        global $typenow, $wpdb;

        if ($typenow != "wp-lead") {
            return;
        }

        /* add lead status filter */
        $statuses = Inbound_Leads::get_lead_statuses();
        echo '<select name="wp_lead_status">';
        echo '<option value="0">'.__('Lead Status','inbound-pro').'</option>';
        foreach( $statuses as $status)  {
            if (isset($_GET['wp_lead_status'])) {
                $selected = $status['key'] == $_GET['wp_lead_status'] ? ' selected ' : '';
            } else {
                $selected = '';
            }
            echo '<option value="'.$status['key'].'" data-color="'.$status['color'].'" ' .  $selected . '>' . $status['label'] .  '</option>';
        }
        echo "</select>";

        /* prepare taxonomy filters */
        $filters = get_object_taxonomies($typenow);
        foreach ($filters as $tax_slug) {
            $tax_obj = get_taxonomy($tax_slug);
            (isset($_GET[$tax_slug])) ? $current = $_GET[$tax_slug] : $current = 0;
            wp_dropdown_categories(array('show_option_all' => __($tax_obj->label), 'taxonomy' => $tax_slug, 'name' => $tax_obj->name, 'orderby' => 'name', 'selected' => $current, 'hierarchical' => $tax_obj->hierarchical, 'show_count' => true, 'hide_empty' => false));
        }

        /* add lead status filter */
        $mapped_fields = Leads_Field_Map::get_lead_fields();
        $mapped_fields = Leads_Field_Map::prioritize_lead_fields($mapped_fields);
        echo '<select name="meta_key" id="lead-meta-filter">';
        echo '<option value="0">'.__('Lead Field','inbound-pro').'</option>';
        foreach( $mapped_fields as $field)  {

            if (isset($field['enable']) && $field['enable'] == 'off') {
                continue;
            }

            if (isset($_GET['meta_key'])) {
                $selected = $field['key'] == $_GET['meta_key'] ? ' selected ' : '';
            } else {
                $selected = '';
            }
            echo '<option value="'.$field['key'].'" data-field-key="'.$field['key'].'" ' .  $selected . '>' . $field['label'] .  '</option>';
        }
        echo "</select>";
        echo '<input type="text" name="meta_value" id="lead-meta-value" placeholder="'.__('search value here...').'" value="'.(isset($_GET['meta_value']) ? sanitize_text_field($_GET['meta_value']) : '' ).'">';

    }

    /**
     * Modifies action row
     * @param $actions
     * @param $post
     * @return mixed
     */
    public static function filter_row_actions($actions, $post) {
        if ($post->post_type != 'wp-lead') {
            return $actions;
        }

        if (isset( $actions['edit'])) {
            $actions['edit'] = str_replace('Edit', __('View', 'inbound-pro' ), $actions['edit']);
        }

        unset($actions['inline hide-if-no-js']);

        return $actions;
    }

    public static function process_column_sorting($pieces, $query) {

        global $wpdb, $table_prefix;

        if (!isset($_GET['post_type']) || $_GET['post_type'] != 'wp-lead' || isset($_GET['page'])) {
            return $pieces;
        }

        if ($query->is_main_query() && ($orderby = $query->get('orderby'))) {
            $order = strtoupper($query->get('order'));

            if (!in_array($order, array('ASC', 'DESC'))) {
                $order = 'ASC';
            }

            switch ($orderby) {

                case 'first-name':

                    $pieces['join'] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'wpleads_first_name'";

                    $pieces['orderby'] = " wp_rd.meta_value $order , " . $pieces['orderby'];

                    break;

                case 'last-name':

                    $pieces['join'] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'wpleads_last_name'";

                    $pieces['orderby'] = " wp_rd.meta_value $order , " . $pieces['orderby'];

                    break;

                case 'status':

                    $pieces['join'] .= " LEFT JOIN $wpdb->postmeta wp_rd ON wp_rd.post_id = {$wpdb->posts}.ID AND wp_rd.meta_key = 'wp_lead_status'";

                    $pieces['orderby'] = " wp_rd.meta_value $order , " . $pieces['orderby'];

                    break;

                case 'page-views':

                    $pieces['join'] .= " LEFT JOIN {$table_prefix}inbound_page_views ipv ON ipv.lead_id = {$wpdb->posts}.ID ";

                    $pieces['groupby'] = " {$wpdb->posts}.ID ";

                    $pieces['orderby'] = " COUNT(ipv.lead_id) " . $order;

                    break;


                case 'action-count':

                    $pieces['join'] .= " LEFT JOIN {$table_prefix}inbound_events ie ON ie.lead_id = {$wpdb->posts}.ID ";

                    $pieces['groupby'] = " {$wpdb->posts}.ID ";

                    $pieces['orderby'] = "COUNT(ie.lead_id) $order ";

                    break;
            }
        } else {
            $pieces['orderby'] = " post_modified  DESC , " . $pieces['orderby'];
        }


        return $pieces;
    }

    /**
     * Handles modification to the filter query
     * @param $query
     */
    public static function process_filters($query) {
        global $pagenow, $post;

        if (!isset($_REQUEST['post_type']) || $_REQUEST['post_type'] != 'wp-lead' || isset($_GET['page'])) {
            return;
        }

        $filters = get_object_taxonomies('wp-lead');
        $qv = &$query->query_vars;

        foreach ($filters as $tax_slug) {

            if ($pagenow == 'edit.php' && isset($qv[$tax_slug]) && is_numeric($qv[$tax_slug])) {
                if ($qv[$tax_slug] != 0) {
                    $term = get_term_by('id', $qv[$tax_slug], $tax_slug);
                    $qv[$tax_slug] = $term->slug;
                }
            }
        }

        /* prepare query by date */
        if (isset($_GET['current_date'])) {
            $timezone_day = _x('d', 'timezone date format');
            $wordpress_date_day = date_i18n($timezone_day);
            set_query_var('day', $wordpress_date_day); // Show only leads from today
            return;
        }

        /* prepare query by month */
        if (isset($_GET['current_month'])) {
            $timezone_month = _x('m', 'timezone date format');
            $wordpress_date_month = date_i18n($timezone_month);
            set_query_var('monthnum', $wordpress_date_month); // Show only leads from today
            return;
        }

        /* prepare query by custom meta field */
        if (isset($_GET['meta_value']) && $_GET['meta_value'] != '') {
            $query->query_vars['meta_key'] = $_GET['meta_key'];
            if (isset($_GET['meta_value']) && $_GET['meta_value'] != '') {
                $query->query_vars['meta_value'] = $_GET['meta_value'];
            }
        }

        /* preapre query by email search */
        if (isset($_GET['lead-email']) && $_GET['lead-email'] != '') {
            $query->query_vars['meta_key'] = 'wpleads_email_address';
            $query->query_vars['meta_value'] = $_GET['lead-email'];

        }

        /* preapre query by status */
        if (isset($_GET['wp_lead_status']) && $_GET['wp_lead_status'] != '0') {
            $query->query_vars['meta_key'] = 'wp_lead_status';
            $query->query_vars['meta_value'] = $_GET['wp_lead_status'];
        }

        //print_r($query->query_vars);exit;
    }

    /**
     * Adds additiona options to bulk edit fields
     */
    public static function register_bulk_edit_fields() {
        global $post_type;

        if ($post_type != 'wp-lead') {
            return;
        }

        $lists = wpleads_get_lead_lists_as_array();

        $html = "<select id='wordpress_list_select' name='action_wordpress_list_id'>";
        foreach ($lists as $id => $label) {
            $html .= "<option value='" . $id . "'>" . $label . "</option>";
        }
        $html .= "</select>";


        ?>
        <script type="text/javascript">
            jQuery(document).ready(function () {

                jQuery('<option>').val('add-to-list').text('<?php _e('Add to Contact List', 'inbound-pro' ) ?>').appendTo("select[name='action']");
                jQuery('<option>').val('add-to-list').text('<?php _e('Add to Contact List', 'inbound-pro' ) ?>').appendTo("select[name='action2']");

                jQuery(document).on('change', 'select[name=action]', function () {
                    var this_id = jQuery(this).val();
                    if (this_id.indexOf("add-to-list") >= 0) {
                        var html = "<?php echo $html; ?>";

                        jQuery("select[name='action']").after(html);
                    }
                    else {
                        jQuery('#posts-filter').prop('target', 'self');
                        jQuery('#wordpress_list_select').remove();
                    }
                });

            });
        </script>
        <?php
    }

    /**
     * process bulk actions for wp-lead post type
     */
    public static function process_bulk_actions() {

        if (!isset($_REQUEST['post_type']) || $_REQUEST['post_type'] != 'wp-lead' || !isset($_REQUEST['post'])) {
            return;
        }

        $wp_list_table = _get_list_table('WP_Posts_List_Table');
        $action = $wp_list_table->current_action();


        if (!current_user_can('manage_options')) {
            die();
        }

        $lead_ids = array_map('intval', $_REQUEST['post']);

        switch ($action) {
            case 'add-to-list':
                $list_id = $_REQUEST['action_wordpress_list_id'];
                $added = 0;

                foreach ($lead_ids as $lead_id) {

                    $list_cpt = get_post($list_id, ARRAY_A);
                    $list_slug = $list_cpt['post_name'];
                    $list_title = $list_cpt['post_title'];

                    wpleads_add_lead_to_list($list_id, $lead_id, $add = true);
                    $added++;
                }
                $sendback = add_query_arg(array('added' => $added, 'post_type' => 'wp-lead', 'ids' => join(',', $lead_ids)), $sendback);
                break;
            default:
                return;
        }

        // 4. Redirect client
        wp_redirect($sendback);
        exit();

    }

    /**
     * Prepare nice names for custom fields
     * @return array
     */
    public static function prepare_nice_names() {
        $nice_names = array("wpleads_company_name" => "Company Name", "wpleads_city" => "City", "wpleads_areaCode" => "Area Code", "wpleads_country_name" => "Country Name", "wpleads_region_code" => "State Abbreviation", "wpleads_region_name" => "State Name", "wp_lead_status" => "Lead Status", "events_triggered" => "Number of Events Triggered", "lp_page_views_count" => "Page View Count");

        $nice_names = apply_filters('wpleads_sort_by_custom_field_nice_names', $nice_names);
        return $nice_names;
    }

    /**
     * Listens for a change to a leads meta data and update the change timestamp
     * @param $meta_id
     * @param $lead_id
     * @param $meta_key
     * @param $meta_value
     */
    public static function record_meta_update($meta_id, $lead_id, $meta_key, $meta_value) {
        $ignore = array('_edit_lock', '_edit_last');
        $post_type = get_post_type($lead_id);
        if ($post_type != 'wp-lead' || in_array($meta_key, $ignore)) {
            return;
        }

        remove_action('updated_post_meta', 'wpleads_after_post_meta_change', 10);
        remove_action('added_post_meta', 'wpleads_after_post_meta_change', 10);

        $timezone_format = _x('Y-m-d G:i:s', 'timezone date format');
        $wordpress_date_time = date_i18n($timezone_format);

        update_post_meta($lead_id, 'wpleads_last_updated', $wordpress_date_time);
        do_action('wpleads_after_post_meta_change', $lead_id);
    }

    /**
     * Listens for incoming profile access link (found in a new lead notification)
     */
    public static function redirect_email_profile_links() {
        global $wpdb;

        if (!isset($_GET['lead-email-redirect']) || $_GET['lead-email-redirect'] == '') {
            return;
        }

        $lead_id = LeadStorage::lookup_lead_by_email($_GET['lead-email-redirect']);

        if ($lead_id) {
            $redirect = 'post.php?post=' . $lead_id . '&action=edit';
            wp_redirect($redirect, 301);
            exit;
        }


    }

    /**
     * Add action links in Plugins table
     */
    public static function display_plugin_quick_links($links) {

        return array_merge(
            array(
                'settings' => '<a href="' . admin_url('edit.php?post_type=wp-lead&page=wpleads_global_settings') . '">' . __('Settings', 'ts-fab') . '</a>'
            ),
            $links
        );

    }

    public static function display_status_pill( $status ) {
        global $lead_statuses;

        if (!$lead_statuses) {
            $lead_statuses = Inbound_Leads::get_lead_statuses();
        }

        $pill = ( isset($lead_statuses[$status]) ) ? $lead_statuses[$status] : $lead_statuses['new'];


        echo '<label class="lead-status-pill lead-status-' . $pill['key'] . '" style="background-color:'.$pill['color'].'" data-status="'.$pill['key'].'">';
        echo $pill['label'];
        echo '</label>';
    }

    /**
     * Converts ARRAY to XML
     * @param $array
     * @param $node_name
     * @return string $xml
     */
    public static function convert_array_to_xml($mixed, $node_name) {
        $xml = '';

        if (is_array($mixed) || is_object($mixed)) {
            foreach ($mixed as $key => $value) {
                if (is_numeric($key)) {
                    $key = $node_name;
                }

                $xml .= '       <' . $key . '>' . "\n           " . self::convert_array_to_xml($value, $node_name) . '      </' . $key . '>' . "\n";
            }
        } else {
            $xml = htmlspecialchars($mixed, ENT_QUOTES) . "\n";
        }

        return $xml;
    }

    /**
     * Prepares XML from Lead data
     * @param $array
     * @param string $node_block
     * @param string $node_name
     * @return string $xml
     */
    public static function build_lead_xml($array, $node_block = 'lead_data', $node_name = 'lead_data') {

        $xml = "";
        $xml .= '   <' . $node_block . '>' . "\n";
        $xml .= "" . self::convert_array_to_xml($array, $node_name);
        $xml .= '   </' . $node_block . '>' . "\n";

        return $xml;
    }

    /**
     * Adds menu items to wp-lead's post type
     */
    public static function setup_admin_menu() {

        /* Lead Management */
        add_submenu_page(
            'edit.php?post_type=wp-lead',
            __('Bulk Actions', 'inbound-pro' ),
            __('Bulk Actions', 'inbound-pro' ),
            'edit_leads',
            'lead_management',
            array('Leads_Manager', 'display_ui')
        );

        /* Manage Forms - now has it's own menu item
        add_submenu_page(
            'edit.php?post_type=wp-lead',
            __('Forms', 'inbound-pro' ),
            __('Forms', 'inbound-pro' ),
            'edit_leads',
            'inbound-forms-redirect',
            100
        );*/

        /* Settings */
        if ( defined('INBOUND_PRO_PATH') ) {

            add_submenu_page(
                'edit.php?post_type=wp-lead',
                __('Settings' , 'inbound-pro'),
                __('Settings' , 'inbound-pro'),
                'edit_leads',
                'inbound-pro-leads',
                array('Leads_Settings', 'redirect_inbound_pro_settings')
            );

            add_submenu_page(
                'edit.php?post_type=wp-lead',
                __('API Keys' , 'inbound-pro'),
                __('API Keys' , 'inbound-pro'),
                'activate_plugins',
                'wpleads_global_settings',
                array('Leads_Settings', 'display_stand_alone_settings')
            );
        }else {
            add_submenu_page(
                'edit.php?post_type=wp-lead',
                __('Settings' , 'inbound-pro'),
                __('Settings' , 'inbound-pro'),
                'edit_leads',
                'wpleads_global_settings',
                array('Leads_Settings', 'display_stand_alone_settings')
            );
        }


    }

    /**
     *
     */
    public static function get_gravatar($lead_id, $size = 50) {
        $email = get_post_meta($lead_id, 'wpleads_email_address', true);

        $size = ($size) ? $size : 50;

        $default = WPL_URLPATH . '/assets/images/gravatar_default_50.jpg'; // doesn't work for some sites

        $gravatar = "//www.gravatar.com/avatar/" . md5(strtolower(trim($email))) . "?d=" . urlencode($default) . "&s=" . $size;
        $extra_image = get_post_meta($lead_id, 'lead_main_image', true);

        // Fix for localhost view
        if (in_array($_SERVER['REMOTE_ADDR'], array('127.0.0.1', '::1'))) {
            $gravatar = $default;
        }

        if (preg_match("/gravatar_default_/", $gravatar) && $extra_image != "") {
            $gravatar = $extra_image;
        }

        return $gravatar;
    }


    /**
     * Enqueue scripts and styles for admin
     */
    public static function enqueue_admin_scripts($hook) {
        global $post;

        $post_type = isset($post) ? get_post_type($post) : null;

        $screen = get_current_screen();

        /*  list setup page */
        if ($screen->id === 'edit-wplead_list_category') {
            wp_enqueue_script('wpleads-list-page', WPL_URLPATH . 'assets/js/wpl.list-page.js', array('jquery'));
            wp_enqueue_style('wpleads-list-page-css', WPL_URLPATH . 'assets/css/wpl.list-page.css');
            return;
        }

        /*  list page */
        if ($screen->id === 'edit-wp-lead') {
            wp_enqueue_script('wpleads-list', WPL_URLPATH . 'assets/js/wpl.leads-list.js');
            wp_enqueue_style('wpleads-list-css', WPL_URLPATH . 'assets/css/wpl.leads-list.css');
            return;
        }
    }

    /**
     * Deletes page_views and events related to deleted lead
     * @param $lead_id
     */
    public static function delete_lead_actions( $lead_id ) {
        if (did_action( 'delete_post' ) > 1) {
            return;
        }

        if (get_post_type($lead_id) != 'wp-lead') {
            return;
        }

        global $wpdb;

        /* delete lead page_views and inbound_events */
        $wpdb->delete( $wpdb->prefix . "inbound_page_views", array( 'lead_id' => $lead_id ) );
        $wpdb->delete( $wpdb->prefix . "inbound_events", array( 'lead_id' => $lead_id ) );

        /* delete automation rules directed towards leads */
        $lead = get_post($lead_id);
        $wpdb->query("DELETE FROM ".$wpdb->prefix . "inbound_automation_queue WHERE `trigger_data` LIKE '%{$lead->post_title}%' AND `tasks` LIKE '%send_email%'");
        error_log($lead->post_title);
    }

    /**
     * Deletes page_views and events related to deleted lead
     * @param $lead_id
     */
    public static function trash_lead_actions( $lead_id ) {

        global $wpdb;

        /* delete automation rules directed towards leads */
        $lead = get_post($lead_id);
        $wpdb->query("DELETE FROM ".$wpdb->prefix . "inbound_automation_queue WHERE `trigger_data` LIKE '%{$lead->post_title}%' AND `tasks` LIKE '%send_email%'");

    }

    /**
     * Ajax listener to return json object of all lead meta data
     */
    public static function ajax_get_all_lead_data() {

        if (!current_user_can('activate_plugins')) {
            return;
        }

        $wp_lead_id = intval($_POST['wp_lead_id']);
        if (isset($wp_lead_id) && is_numeric($wp_lead_id)) {
            global $wpdb;
            $data = array();
            $wpdb->query($wpdb->prepare("
          SELECT `meta_key`, `meta_value`
            FROM $wpdb->postmeta
            WHERE `post_id` = %d", $wp_lead_id
            ));

            foreach ($wpdb->last_result as $k => $v) {
                $data[$v->meta_key] = $v->meta_value;
            };

            echo json_encode($data, JSON_FORCE_OBJECT);
            wp_die();
        }
    }

    /**
     * Ajax listener to mark lead as read
     */
    public static function ajax_mark_lead_as_read() {
        global $wpdb;

        $lead_id = intval($_POST['page_id']);

        update_post_meta($lead_id, 'wp_lead_status', 'read');
        header('HTTP/1.1 200 OK');
        exit;
    }

    /**
     * Ajax listener to mark lead as unread
     */
    public static function ajax_mark_lead_as_unread() {
        global $wpdb;

        $lead_id = intval($_POST['page_id']);

        update_post_meta($lead_id, 'wp_lead_status', 'new');
        header('HTTP/1.1 200 OK');
        exit;
    }

    /**
     * Ajax listener to automatically mark a lead as read when the lead is opened for the first time
     */
    public static function ajax_auto_mark_as_read() {
        global $wpdb;

        $lead_id = intval($_POST['page_id']);

        update_post_meta($lead_id, 'wp_lead_status', 'read');
        header('HTTP/1.1 200 OK');
    }
    
    /**
     * Ajax listener to stop waiting for double optin
     * Deletes the array of lists waiting for confirmation from the lead,
     * moves the lead out of the waiting for confirmation list,
     * and marks the lead as read
     */
    public static function ajax_stop_waiting_for_double_optin() {
        global $wpdb;
 
         /*get the double optin waiting list id*/
        if(!defined('INBOUND_PRO_CURRENT_VERSION')){
            $double_optin_list_id = get_option('list-double-optin-list-id', '');
        }else{
            $settings = Inbound_Options_API::get_option('inbound-pro', 'settings', array());
            $double_optin_list_id = $settings['leads']['list-double-optin-list-id'];
        }

        $lead_id = intval($_POST['page_id']);

        delete_post_meta($lead_id, 'double_optin_lists');
        Inbound_Leads::remove_lead_from_list($lead_id, (int)$double_optin_list_id);
        
        update_post_meta($lead_id, 'wp_lead_status', 'read');
        header('HTTP/1.1 200 OK');
        exit;
    }

    
}

new Leads_Post_Type;
Inbound Pro Plugin - PHP Codex API documentation generated by ApiGen