array( 'title' => 'Overview', 'template' => 'overview' ), 'aimtell-campaigns' => array( 'title' => 'Campaigns', 'template' => 'campaigns' ), 'aimtell-blog-posts' => array( 'title' => 'Blog posts', 'template' => 'rss' ), 'aimtell-segments' => array( 'title' => 'Segments', 'template' => 'segments' ), 'aimtell-web-push-settings' => array( 'title' => 'Settings', 'template' => 'settings' ), ); } /** * The site is connected once all four values the tracking script needs are stored. */ function aimtell_is_connected() { return get_option( 'aimtell_uid' ) && get_option( 'aimtell_idSite' ) && get_option( 'aimtell_domain' ) && get_option( 'aimtell_webpushid' ); } function aimtell_admin_url( $slug = 'aimtell-web-push', $args = array() ) { return add_query_arg( array_merge( array( 'page' => $slug ), $args ), admin_url( 'admin.php' ) ); } function aimtellWP_admin_menu() { $pages = aimtell_admin_pages(); add_menu_page( 'Aimtell', 'Aimtell', 'manage_options', 'aimtell-web-push', 'aimtell_admin_render', AIMTELL_URL . 'assets/images/aimtell_icon.png' ); foreach ( $pages as $slug => $page ) { add_submenu_page( 'aimtell-web-push', $page['title'] . ' ‹ Aimtell', $page['title'], 'manage_options', $slug, 'aimtell_admin_render' ); } } add_action( 'admin_menu', 'aimtellWP_admin_menu' ); /** * Which screen to show. Account screens show sample data until the site is connected, * and the sign-in screen when the site is connected but this browser has no token. */ function aimtell_current_view() { $page = isset( $_GET['page'] ) ? sanitize_key( $_GET['page'] ) : 'aimtell-web-push'; $view = isset( $_GET['view'] ) ? sanitize_key( $_GET['view'] ) : ''; $pages = aimtell_admin_pages(); if ( ! isset( $pages[ $page ] ) ) { $page = 'aimtell-web-push'; } if ( 'settings' === $pages[ $page ]['template'] ) { return array( 'page' => $page, 'template' => 'settings', 'demo' => false ); } if ( 'signin' === $view || 'connect' === $view ) { return array( 'page' => $page, 'template' => $view, 'demo' => false ); } if ( ! aimtell_is_connected() ) { return array( 'page' => $page, 'template' => $pages[ $page ]['template'], 'demo' => true ); } if ( empty( $_COOKIE['aimtell_auth_token'] ) ) { return array( 'page' => $page, 'template' => 'signin', 'demo' => false ); } return array( 'page' => $page, 'template' => $pages[ $page ]['template'], 'demo' => false ); } function aimtell_admin_render() { if ( ! current_user_can( 'manage_options' ) ) { wp_die( esc_html__( 'You do not have sufficient permissions to access this page.' ) ); } $aimtell_view = aimtell_current_view(); $aimtell_demo = $aimtell_view['demo']; echo '
'; // WordPress moves admin notices to just after the first h1/h2 in .wrap; this empty one keeps them at the top echo '

'; include __FOLDERDIR__ . '/templates/' . $aimtell_view['template'] . '.php'; echo '
'; } /** * The sample-data banner shown above account screens until the site is connected. */ function aimtell_demo_banner() { ?>
You're exploring with sample data Click around. Nothing here is sent. Create a free account to see your own subscribers and campaigns.
Start free trial I have an account
id, 'aimtell' ) ) { $classes .= ' aimtell-admin'; } return $classes; } add_filter( 'admin_body_class', 'aimtell_admin_body_class' ); /** * WordPress categories for the blog post auto-send screen. */ function aimtell_category_list() { $out = array(); foreach ( get_categories( array( 'hide_empty' => false ) ) as $cat ) { // each category's own feed address, so a single category needs no query string $out[] = array( 'slug' => $cat->slug, 'name' => $cat->name, 'count' => (int) $cat->count, 'feed' => wp_specialchars_decode( get_category_feed_link( $cat->term_id ) ) ); } return $out; } /** * Plugin version plus the file's modified time, so browsers never keep a stale copy * of a script or stylesheet that changed without a version bump. */ function aimtell_asset_version( $path ) { global $aimtell_version; $file = __FOLDERDIR__ . '/' . $path; return $aimtell_version . ( file_exists( $file ) ? '.' . filemtime( $file ) : '' ); } function aimtellWP_admin_scripts( $hook ) { if ( false === strpos( $hook, 'aimtell' ) ) { return; } $view = aimtell_current_view(); wp_enqueue_style( 'aimtell-font', 'https://fonts.googleapis.com/css2?family=Montserrat:wght@400;500;600;700&display=swap', array(), null ); wp_enqueue_style( 'aimtell-admin', AIMTELL_URL . 'assets/css/admin.css', array(), aimtell_asset_version( 'assets/css/admin.css' ) ); if ( 'settings' === $view['template'] ) { return; } wp_enqueue_script( 'aimtell-core', AIMTELL_URL . 'assets/js/admin-core.js', array(), aimtell_asset_version( 'assets/js/admin-core.js' ), true ); $site_icon = has_site_icon() ? get_site_icon_url( 512 ) : ''; if ( ! $site_icon && has_custom_logo() ) { $site_icon = wp_get_attachment_image_url( get_theme_mod( 'custom_logo' ), 'full' ); } $pages = array(); foreach ( aimtell_admin_pages() as $slug => $page ) { $pages[ $page['template'] ] = aimtell_admin_url( $slug ); } $pages['signin'] = aimtell_admin_url( 'aimtell-web-push', array( 'view' => 'signin' ) ); $pages['connect'] = aimtell_admin_url( 'aimtell-web-push', array( 'view' => 'connect' ) ); wp_localize_script( 'aimtell-core', 'AimtellWP', array( 'api' => AIMTELL_API, 'dashboard' => AIMTELL_DASHBOARD, 'signupUrl' => AIMTELL_SIGNUP_URL, 'ajaxUrl' => admin_url( 'admin-ajax.php' ), 'nonce' => wp_create_nonce( 'aimtell_admin' ), 'restUrl' => esc_url_raw( rest_url( 'wp/v2/' ) ), 'restNonce' => wp_create_nonce( 'wp_rest' ), 'view' => $view['template'], 'demo' => $view['demo'] ? 1 : 0, 'connected' => aimtell_is_connected() ? 1 : 0, 'pages' => $pages, 'returnTo' => isset( $_GET['page'] ) ? aimtell_admin_url( sanitize_key( $_GET['page'] ) ) : $pages['overview'], 'site' => array( 'idSite' => (string) get_option( 'aimtell_idSite' ), 'domain' => (string) get_option( 'aimtell_domain' ), 'webPushID' => (string) get_option( 'aimtell_webpushid' ), 'uid' => (string) get_option( 'aimtell_uid' ), 'rssId' => (string) get_option( 'aimtell_rss_id' ), ), 'wp' => array( 'url' => home_url(), 'host' => wp_parse_url( home_url(), PHP_URL_HOST ), 'name' => get_bloginfo( 'name' ), 'icon' => $site_icon ? $site_icon : '', 'feed' => get_feed_link(), ), 'woo' => array( 'active' => is_woocommerce_activated() ? 1 : 0, 'cart' => get_aimtell_woocommerce_tracking(), 'browse' => get_aimtell_woocommerce_abandoned_browse(), 'delay' => get_aimtell_woocommerce_abandoned_browse_delay(), ), 'categories' => in_array( $view['template'], array( 'rss', 'segments' ), true ) ? aimtell_category_list() : array(), ) ); $page_scripts = array( 'overview' => 'overview', 'campaigns' => 'campaigns', 'rss' => 'rss', 'segments' => 'segments', 'signin' => 'signin', 'connect' => 'connect', ); if ( isset( $page_scripts[ $view['template'] ] ) ) { wp_enqueue_script( 'aimtell-' . $view['template'], AIMTELL_URL . 'assets/js/' . $page_scripts[ $view['template'] ] . '.js', array( 'aimtell-core' ), aimtell_asset_version( 'assets/js/' . $page_scripts[ $view['template'] ] . '.js' ), true ); } // the Media Library picker, for the site icon and for push icons and images if ( in_array( $view['template'], array( 'connect', 'campaigns', 'rss' ), true ) ) { wp_enqueue_media(); } } add_action( 'admin_enqueue_scripts', 'aimtellWP_admin_scripts' ); /** * Store the Aimtell site this WordPress site is connected to. * Called by the connect screen once the site exists on the Aimtell side. */ function aimtell_ajax_connect() { check_ajax_referer( 'aimtell_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'You do not have permission to change this.' ), 403 ); } $fields = array( 'aimtell_idSite' => isset( $_POST['idSite'] ) ? sanitize_text_field( wp_unslash( $_POST['idSite'] ) ) : '', 'aimtell_domain' => isset( $_POST['domain'] ) ? esc_url_raw( wp_unslash( $_POST['domain'] ) ) : '', 'aimtell_webpushid' => isset( $_POST['webPushID'] ) ? sanitize_text_field( wp_unslash( $_POST['webPushID'] ) ) : '', 'aimtell_uid' => isset( $_POST['uid'] ) ? sanitize_text_field( wp_unslash( $_POST['uid'] ) ) : '', ); foreach ( $fields as $value ) { if ( '' === $value ) { wp_send_json_error( array( 'message' => 'Aimtell did not return all the site details. Try connecting again.' ), 400 ); } } foreach ( $fields as $name => $value ) { update_option( $name, $value, 'yes' ); } update_option( 'aimtell_account_email', isset( $_POST['email'] ) ? sanitize_email( wp_unslash( $_POST['email'] ) ) : '', 'no' ); update_option( 'aimtell_site_name', isset( $_POST['name'] ) ? sanitize_text_field( wp_unslash( $_POST['name'] ) ) : '', 'no' ); // a different Aimtell site has its own blog feed, so forget the old one delete_option( 'aimtell_rss_id' ); wp_send_json_success( array( 'redirect' => aimtell_admin_url( 'aimtell-web-push' ) ) ); } add_action( 'wp_ajax_aimtell_connect', 'aimtell_ajax_connect' ); /** * Remember which Aimtell RSS notification the blog posts screen manages. */ function aimtell_ajax_set_rss() { check_ajax_referer( 'aimtell_admin', 'nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_send_json_error( array( 'message' => 'You do not have permission to change this.' ), 403 ); } $id = isset( $_POST['rssId'] ) ? sanitize_text_field( wp_unslash( $_POST['rssId'] ) ) : ''; update_option( 'aimtell_rss_id', $id, 'no' ); wp_send_json_success(); } add_action( 'wp_ajax_aimtell_set_rss', 'aimtell_ajax_set_rss' ); /** * Disconnect: forget the Aimtell site so the tracking script stops loading. * Subscribers and history stay on the Aimtell account. */ function aimtell_disconnect() { check_admin_referer( 'aimtell_disconnect' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'You do not have permission to change this.' ); } foreach ( array( 'aimtell_idSite', 'aimtell_domain', 'aimtell_webpushid', 'aimtell_uid', 'aimtell_rss_id', 'aimtell_account_email', 'aimtell_site_name' ) as $name ) { delete_option( $name ); } wp_safe_redirect( aimtell_admin_url( 'aimtell-web-push-settings', array( 'status' => 'disconnected' ) ) ); exit; } add_action( 'admin_post_aimtell_disconnect', 'aimtell_disconnect' ); function save_aimtell_settings() { check_admin_referer( 'save_aimtell_settings', 'aimtell_settings_nonce' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'You do not have permission to change this.' ); } $delay = isset( $_POST['aimtell_woocommerce_abandoned_browse_delay'] ) ? absint( $_POST['aimtell_woocommerce_abandoned_browse_delay'] ) : 10; $level = isset( $_POST['aimtell_woocommerce_logging_level'] ) ? absint( $_POST['aimtell_woocommerce_logging_level'] ) : 0; update_option( 'aimtell_woocommerce_tracking', ! empty( $_POST['aimtell_woocommerce_tracking'] ) ? 1 : 0 ); update_option( 'aimtell_woocommerce_abandoned_browse', ! empty( $_POST['aimtell_woocommerce_abandoned_browse'] ) ? 1 : 0 ); update_option( 'aimtell_woocommerce_abandoned_browse_delay', $delay > 0 ? $delay : 10 ); update_option( 'aimtell_woocommerce_logging_level', min( $level, 4 ) ); wp_safe_redirect( aimtell_admin_url( 'aimtell-web-push-settings', array( 'status' => 'saved' ) ) ); exit; } add_action( 'admin_post_save_aimtell_settings', 'save_aimtell_settings' ); function aimtell_log_download() { check_admin_referer( 'aimtell_log' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'You do not have permission to do this.' ); } $path = get_aimtell_log_path(); nocache_headers(); header( 'Content-Type: text/plain; charset=utf-8' ); header( 'Content-Disposition: attachment; filename="aimtell-' . gmdate( 'Y-m-d' ) . '.log"' ); if ( file_exists( $path ) ) { readfile( $path ); } exit; } add_action( 'admin_post_aimtell_log_download', 'aimtell_log_download' ); function aimtell_log_clear() { check_admin_referer( 'aimtell_log' ); if ( ! current_user_can( 'manage_options' ) ) { wp_die( 'You do not have permission to do this.' ); } $path = get_aimtell_log_path(); if ( file_exists( $path ) ) { file_put_contents( $path, '' ); } wp_safe_redirect( aimtell_admin_url( 'aimtell-web-push-settings', array( 'status' => 'log-cleared' ) ) ); exit; } add_action( 'admin_post_aimtell_log_clear', 'aimtell_log_clear' );