Plugin Directory

Changeset 3821494


Ignore:
Timestamp:
11/24/2026 09:01:36 PM (less than one hour ago)
Author:
mailmunch
Message:

Release 3.3.2

Location:
mailchimp-forms-by-mailmunch
Files:
18 edited
1 copied

Legend:

Unmodified
Added
Removed
  • mailchimp-forms-by-mailmunch/tags/3.3.2/admin/class-mailchimp-mailmunch-admin.php

    r3741096 r3821494  
    338338                }
    339339                return $step;
     340        }
     341
     342        /**
     343         * MM-3766: opens MailMunch for an admin without the MailMunch token ever being in a page.
     344         * Hooked to admin_post_<prefix>_sso; links come from Mailchimp_Mailmunch_Api::ssoAdminUrl() and the
     345         * dashboard forms rewritten by Mailchimp_Mailmunch_Api::withoutToken().
     346         */
     347        public function sso_redirect() {
     348                if ( ! current_user_can( 'manage_options' ) ) {
     349                        wp_die( esc_html__( 'Sorry, you are not allowed to do that.' ), 403 );
     350                }
     351                check_admin_referer( MAILCHIMP_MAILMUNCH_PREFIX . '_sso' );
     352
     353                $next_url = isset( $_REQUEST['next_url'] ) ? (string) wp_unslash( $_REQUEST['next_url'] ) : '';
     354                $is_path  = '' !== $next_url && '/' === $next_url[0] && 0 !== strpos( $next_url, '//' ) && false === strpos( $next_url, '\\' );
     355                $is_https = 0 === strpos( $next_url, 'https://' );
     356                if ( ! $is_path && ! $is_https ) {
     357                        wp_die( esc_html__( 'That link is not valid.' ), 400 );
     358                }
     359                $source = isset( $_REQUEST['source'] ) ? sanitize_key( wp_unslash( $_REQUEST['source'] ) ) : 'mailchimp';
     360
     361                $this->initiate_api();
     362                $url = $this->mailmunch_api->getSsoLink( $next_url, $source ? $source : 'mailchimp' );
     363                if ( is_wp_error( $url ) ) {
     364                        wp_die( esc_html( $url->get_error_message() ), 502 );
     365                }
     366
     367                nocache_headers();
     368                wp_redirect( $url ); // MailMunch's own host, checked in getSsoLink().
     369                exit;
     370        }
     371
     372        /**
     373         * MM-3766: the Connect button used to carry the MailMunch token in the page. It now points here; this
     374         * forwards the pop-up to the same MailMunch address as before, so the sign-in flow is unchanged.
     375         */
     376        public function connect_redirect() {
     377                if ( ! current_user_can( 'manage_options' ) ) {
     378                        wp_die( esc_html__( 'Sorry, you are not allowed to do that.' ), 403 );
     379                }
     380                check_admin_referer( MAILCHIMP_MAILMUNCH_PREFIX . '_connect' );
     381                $this->initiate_api();
     382                nocache_headers();
     383                wp_redirect( MAILCHIMP_MAILMUNCH_HOME_URL . '/wordpress/mailchimp/new?token=' . rawurlencode( $this->mailmunch_api->getUserToken() ) );
     384                exit;
    340385        }
    341386
  • mailchimp-forms-by-mailmunch/tags/3.3.2/admin/partials/mailchimp-mailmunch-connect.php

    r3189361 r3821494  
    2929              <img id="loader" src="<?php echo plugins_url( 'img/loader.gif', dirname(__FILE__) ) ?>" />
    3030
    31               <a id="connect-mailchimp" href="<?php echo MAILCHIMP_MAILMUNCH_HOME_URL ?>/wordpress/mailchimp/new?token=<?php echo $this->mailmunch_api->getUserToken(); ?>">
     31              <a id="connect-mailchimp" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'action', MAILCHIMP_MAILMUNCH_PREFIX . '_connect', admin_url( 'admin-post.php' ) ), MAILCHIMP_MAILMUNCH_PREFIX . '_connect' ) ); ?>">
    3232                <img src="<?php echo plugins_url( 'img/mailchimp_logo.png', dirname(__FILE__) ) ?>" />
    3333                <span class="button button-primary">Connect to MailChimp</span>
  • mailchimp-forms-by-mailmunch/tags/3.3.2/includes/class-mailchimp-mailmunch-post-type.php

    r2210463 r3821494  
    4242        'query_var'          => true,
    4343        'rewrite'            => array('with_front' => false, 'slug' => MAILCHIMP_MAILMUNCH_POST_TYPE ),
    44         'capability_type'    => 'post',
     44        // MM-3766: only administrators (manage_options) may see, create or edit landing pages.
     45        // Ordinary post permissions let Contributors and Authors in, and the page shows raw
     46        // MailMunch HTML, which WordPress only trusts administrators with.
     47        'capability_type'    => array( 'mailchimp_mailmunch_landing_page', 'mailchimp_mailmunch_landing_pages' ),
     48        'map_meta_cap'       => false,
     49        'capabilities'       => array(
     50            'edit_post'              => 'manage_options',
     51            'read_post'              => 'read',
     52            'delete_post'            => 'manage_options',
     53            'edit_posts'             => 'manage_options',
     54            'edit_others_posts'      => 'manage_options',
     55            'edit_private_posts'     => 'manage_options',
     56            'edit_published_posts'   => 'manage_options',
     57            'publish_posts'          => 'manage_options',
     58            'read_private_posts'     => 'manage_options',
     59            'delete_posts'           => 'manage_options',
     60            'delete_private_posts'   => 'manage_options',
     61            'delete_published_posts' => 'manage_options',
     62            'delete_others_posts'    => 'manage_options',
     63            'create_posts'           => 'manage_options',
     64        ),
    4565        'has_archive'        => false,
    4666        'hierarchical'       => false,
    … …  
    83103 
    84104  public function get_landing_pages_dropdown($post) {
     105    if (!current_user_can('manage_options')) { return; }
    85106    $this->initiate_api();
    86107    $landing_pages = $this->mailmunch_api->getLandingPages();
     108    if (is_wp_error($landing_pages) || !is_array($landing_pages)) {
     109      echo '<p>' . esc_html__('Unable to load your landing pages from MailMunch. Please try again later.') . '</p>';
     110      return;
     111    }
    87112    $html = '<input type="hidden" name="mailmunch_landing_page_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    88113    $key = '_mailmunch_landing_page_id';
    89114    $selected_landing_page_id = get_post_meta( $post->ID, $key, true );
    90115    $selected_landing_page_id = intval($selected_landing_page_id);
    91     if (!$selected_landing_page_id) $selected_landing_page_id = $landing_pages[0]->id;
     116    if (!$selected_landing_page_id && !empty($landing_pages)) $selected_landing_page_id = intval($landing_pages[0]->id);
     117    $site_path = "/sites/". intval(get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")). "/landing_pages";
    92118    $html .= '<p>Create or choose the landing page that you want to activate here.</p>';
    93     $html .= '<input id="mailmunch_landing_page_id" name="mailmunch_landing_page_id" type="hidden" value="'. $selected_landing_page_id .'" />';
     119    $html .= '<input id="mailmunch_landing_page_id" name="mailmunch_landing_page_id" type="hidden" value="'. esc_attr($selected_landing_page_id) .'" />';
    94120    foreach ($landing_pages as $key => $landing_page) {
    95       $html .= '<div class="mailmunch-landing-page'. ($selected_landing_page_id == $landing_page->id ? ' active' : '') .'" data-landing-page-id="'. $landing_page->id .'">';
     121      $landing_page_id = intval($landing_page->id);
     122      $html .= '<div class="mailmunch-landing-page'. ($selected_landing_page_id == $landing_page_id ? ' active' : '') .'" data-landing-page-id="'. esc_attr($landing_page_id) .'">';
    96123      $html .= '<div class="page-image">';
    97       $html .= '<img src="'. $landing_page->preview_url .'" />';
     124      $html .= '<img src="'. esc_url($landing_page->preview_url) .'" />';
    98125      $html .= '<div class="page-hover">';
    99       $html .= '<a href="'. MAILCHIMP_MAILMUNCH_URL_SECURED. "/sso?token=". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token"). "&next_url=". urlencode(MAILCHIMP_MAILMUNCH_LANDING_PAGE_URL. "/sites/". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/landing_pages/". $landing_page->id ."/edit") .'" target="_blank" class="mailmunch-edit-page button button-primary button-large">Edit Page</a>';
     126      // MM-3766: no MailMunch token in the page; the link asks for a one-use sign-in link when clicked.
     127      $html .= '<a href="'. esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl($site_path. "/". $landing_page_id ."/edit")) .'" target="_blank" class="mailmunch-edit-page button button-primary button-large">Edit Page</a>';
    100128      $html .= '</div>';
    101129      $html .= '</div>';
    102       $html .= '<div class="page-name">'. ($landing_page->name ? $landing_page->name : "Untitled") .'</div>';
     130      $html .= '<div class="page-name">'. esc_html($landing_page->name ? $landing_page->name : "Untitled") .'</div>';
    103131      $html .= '</div>';
    104132    }
    105     $html .= '<a href="'.MAILCHIMP_MAILMUNCH_URL_SECURED. "/sso?token=". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token"). "&next_url=". urlencode(MAILCHIMP_MAILMUNCH_LANDING_PAGE_URL. "/sites/". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/landing_pages/new") .'" class="mailmunch-landing-page new-landing-page" target="_blank">';
     133    $html .= '<a href="'. esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl($site_path. "/new")) .'" class="mailmunch-landing-page new-landing-page" target="_blank">';
    106134    $html .= '<div class="page-image">';
    107135    $html .= '<span class="dashicons dashicons-plus-alt"></span>';
    … …  
    130158    }
    131159
    132     // is the user allowed to edit the post or page?
    133     if( ! current_user_can( 'edit_post', $post->ID )){
    134         return $post->ID;
     160    // MM-3766: landing pages are for administrators only.
     161    if ( ! current_user_can( 'manage_options' ) || ! current_user_can( 'edit_post', $post->ID ) ) {
     162        return $post->ID;
    135163    }
    136164
    137165    $key = '_mailmunch_landing_page_id';
    138     $value = $_POST['mailmunch_landing_page_id'];
    139     if( get_post_meta( $post->ID, $key, true ) ) { // if the custom field already has a value
    140       update_post_meta($post->ID, $key, $value);
    141     } else { // if the custom field doesn't have a value
    142       add_post_meta( $post->ID, $key, $value );
    143     }
     166    $value = isset( $_POST['mailmunch_landing_page_id'] ) ? absint( $_POST['mailmunch_landing_page_id'] ) : 0;
    144167    if( !$value ) { // delete if blank
    145168      delete_post_meta( $post->ID, $key );
    146     }
     169      return;
     170    }
     171
     172    // MM-3766: only a landing page of the connected MailMunch site may be shown here.
     173    $this->initiate_api();
     174    $allowed_ids = $this->mailmunch_api->getLandingPageIds();
     175    if ( is_wp_error( $allowed_ids ) || ! in_array( $value, $allowed_ids, true ) ) {
     176      set_transient( MAILCHIMP_MAILMUNCH_PREFIX . '_landing_page_refused_' . get_current_user_id(), is_wp_error( $allowed_ids ) ? 'unavailable' : 'not_yours', 60 );
     177      return $post->ID;
     178    }
     179
     180    update_post_meta( $post->ID, $key, $value );
     181  }
     182
     183  /**
     184   * MM-3766: says why a landing page choice was not saved.
     185   */
     186  public function landing_page_refused_notice() {
     187    $transient = MAILCHIMP_MAILMUNCH_PREFIX . '_landing_page_refused_' . get_current_user_id();
     188    $reason = get_transient( $transient );
     189    if ( ! $reason ) { return; }
     190    delete_transient( $transient );
     191    $message = ( 'unavailable' === $reason )
     192      ? 'The landing page was not changed: MailMunch could not be reached to check it. Please try again.'
     193      : 'The landing page was not changed: it does not belong to the MailMunch site connected to this WordPress site.';
     194    echo '<div class="notice notice-error is-dismissible"><p>' . esc_html( $message ) . '</p></div>';
    147195  }
    148196 
  • mailchimp-forms-by-mailmunch/tags/3.3.2/includes/class-mailchimp-mailmunch-sidebar-widget.php

    r2210463 r3821494  
    9191    </p>
    9292
    93     <p><a href="<?php echo MAILCHIMP_MAILMUNCH_URL_SECURED ?>/sso?token=<?php echo get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token") ?>&next_url=<?php echo urlencode("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar") ?>" target="_blank">Create New Sidebar Form</a></p>
     93    <p><?php if (current_user_can('manage_options')) { ?><a href="<?php echo esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar")) ?>" target="_blank">Create New Sidebar Form</a><?php } ?></p>
    9494    <?php
    9595    } else {
    9696    ?>
    97     <p>No sidebar forms found. <a href="<?php echo MAILCHIMP_MAILMUNCH_URL_SECURED ?>/sso?token=<?php echo get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token") ?>&next_url=<?php echo urlencode("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar") ?>" target="_blank">Create Your First One</a></p>
     97    <p>No sidebar forms found. <?php if (current_user_can('manage_options')) { ?><a href="<?php echo esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar")) ?>" target="_blank">Create Your First One</a><?php } ?></p>
    9898    <?php
    9999    }
  • mailchimp-forms-by-mailmunch/tags/3.3.2/includes/class-mailchimp-mailmunch.php

    r3745964 r3821494  
    2424define( 'MAILCHIMP_MAILMUNCH_POST_TYPE', 'mailmunch_page' );
    2525define( 'MAILCHIMP_MAILMUNCH_PLUGIN_DIRECTORY', 'mailchimp-forms-by-mailmunch' );
    26 define( 'MAILCHIMP_MAILMUNCH_VERSION', '3.3.1' );
     26define( 'MAILCHIMP_MAILMUNCH_VERSION', '3.3.2' );
    2727
    2828/**
    … …  
    216216
    217217                // Ajax calls
     218                $this->loader->add_action( 'admin_post_'. MAILCHIMP_MAILMUNCH_PREFIX. '_sso', $plugin_admin, 'sso_redirect' );
     219                $this->loader->add_action( 'admin_post_'. MAILCHIMP_MAILMUNCH_PREFIX. '_connect', $plugin_admin, 'connect_redirect' );
    218220                $this->loader->add_action( 'wp_ajax_sign_up', $plugin_admin, 'sign_up' );
    219221                $this->loader->add_action( 'wp_ajax_sign_in', $plugin_admin, 'sign_in' );
    … …  
    272274                $this->loader->add_action('add_meta_boxes', $plugin_post_type, 'post_meta_box', 10, 2);
    273275                $this->loader->add_action('save_post', $plugin_post_type, 'landing_page_save_meta', 10, 2 );
     276                $this->loader->add_action('admin_notices', $plugin_post_type, 'landing_page_refused_notice');
    274277               
    275278                $plugin_rewrite = new Mailchimp_Mailmunch_Rewrite();
  • mailchimp-forms-by-mailmunch/tags/3.3.2/includes/class-mailmunch-api.php

    r2077433 r3821494  
    3838
    3939    function setUserToken($userToken) {
     40      self::forgetLandingPageIds();
    4041      return update_option($this->getPrefix(). 'user_token', $userToken );
    4142    }
    … …  
    275276      }
    276277
    277       $body = str_replace('{{TOKEN}}', $this->getUserToken(), $request['body']);
    278       return $body;
     278      return self::withoutToken($request['body']);
    279279    }
    280280   
    … …  
    288288      }
    289289
    290       $body = str_replace('{{TOKEN}}', $this->getUserToken(), $request['body']);
    291       return $body;
     290      return self::withoutToken($request['body']);
    292291    }
    293292   
    … …  
    301300      }
    302301
    303       $body = str_replace('{{TOKEN}}', $this->getUserToken(), $request['body']);
    304       return $body;
     302      return self::withoutToken($request['body']);
     303    }
     304
     305    /**
     306     * MM-3766: a wp-admin link that opens MailMunch at $nextPath for the admin clicking it.
     307     * It carries no MailMunch token, only a WordPress nonce; the admin-post handler asks
     308     * MailMunch for a one-use, 60-second sign-in link when it is clicked.
     309     */
     310    static function ssoAdminUrl($nextPath) {
     311      $url = add_query_arg(array(
     312        'action' => MAILCHIMP_MAILMUNCH_PREFIX. '_sso',
     313        'next_url' => rawurlencode($nextPath),
     314      ), admin_url('admin-post.php'));
     315      return wp_nonce_url($url, MAILCHIMP_MAILMUNCH_PREFIX. '_sso');
     316    }
     317
     318    /**
     319     * MM-3766: the dashboard HTML from MailMunch has forms that post the user token to /sso.
     320     * Point them at the nonce-protected admin-post handler instead, and never put the token in.
     321     */
     322    static function withoutToken($html) {
     323      $html = preg_replace('#action="(?:https?:)?//[^"/]+/sso"#', 'action="'. esc_url(admin_url('admin-post.php')). '"', $html);
     324      $fields = '<input type="hidden" name="action" value="'. esc_attr(MAILCHIMP_MAILMUNCH_PREFIX. '_sso'). '" />'
     325        . '<input type="hidden" name="_wpnonce" value="'. esc_attr(wp_create_nonce(MAILCHIMP_MAILMUNCH_PREFIX. '_sso')). '" />';
     326      $html = preg_replace('#<input type="hidden" name="token" value="\{\{TOKEN\}\}"\s*/?>#', $fields, $html);
     327      // Whatever is left of the placeholder is blanked, so the token can never reach the page.
     328      $html = str_replace('{{TOKEN}}', '', $html);
     329      // Report links carry the GraphQL login token (utk); MailMunch adds a fresh one on redeem.
     330      $html = preg_replace('#(&amp;|&)utk=[^"&]*#', '', $html);
     331      $html = preg_replace('#\?utk=[^"&]*(&amp;|&)?#', '?', $html);
     332      return $html;
     333    }
     334
     335    /**
     336     * MM-3766: asks MailMunch (server to server, with the plugin token) for a one-use sign-in link.
     337     * Returns the URL, or WP_Error. Only URLs on the MailMunch host this plugin talks to are accepted.
     338     */
     339    function getSsoLink($nextPath, $source) {
     340      $this->requestType = 'post';
     341      $request = $this->ping('/wordpress/sso_link', array('next_url' => $nextPath, 'source' => $source));
     342      if (is_wp_error($request) || wp_remote_retrieve_response_code($request) != 200) {
     343        return new WP_Error('mailmunch_sso', 'Unable to open MailMunch. Please try again.');
     344      }
     345
     346      $body = json_decode(wp_remote_retrieve_body($request));
     347      if (empty($body) || empty($body->success) || empty($body->url) || !is_string($body->url)) {
     348        return new WP_Error('mailmunch_sso', 'Unable to open MailMunch. Please try again.');
     349      }
     350
     351      $parsed = wp_parse_url($body->url);
     352      $allowedHosts = array_map('strtolower', array_filter(array(
     353        wp_parse_url(MAILCHIMP_MAILMUNCH_URL, PHP_URL_HOST),
     354        wp_parse_url(MAILCHIMP_MAILMUNCH_URL_SECURED, PHP_URL_HOST),
     355      )));
     356      $securedScheme = wp_parse_url(MAILCHIMP_MAILMUNCH_URL_SECURED, PHP_URL_SCHEME);
     357      $allowedSchemes = ($securedScheme === 'https') ? array('https') : array('https', 'http');
     358      if (empty($parsed['host']) || empty($parsed['scheme'])
     359          || !in_array(strtolower($parsed['host']), $allowedHosts, true)
     360          || !in_array(strtolower($parsed['scheme']), $allowedSchemes, true)
     361          || (isset($parsed['path']) && $parsed['path'] !== '/wordpress/sso_redeem')) {
     362        return new WP_Error('mailmunch_sso', 'Unable to open MailMunch. Please try again.');
     363      }
     364
     365      return $body->url;
     366    }
     367
     368    /**
     369     * MM-3766: may this landing page id be shown on this WordPress site? Only the connected MailMunch site's
     370     * own pages. Pages saved before the fix may point at another account's page, so this runs at page view.
     371     * The site's list is cached for an hour; a miss refreshes it at most every 5 minutes (so a page made a
     372     * moment ago works). If MailMunch can't be asked, the page is shown as before rather than taken down.
     373     * Uses no Api instance: the constructor would create a site or user on a visitor's request.
     374     */
     375    static function landingPageAllowed($landingPageId) {
     376      $landingPageId = absint($landingPageId);
     377      if (!$landingPageId) { return false; }
     378      $siteId = absint(get_option(MAILCHIMP_MAILMUNCH_PREFIX. '_site_id'));
     379      $userToken = get_option(MAILCHIMP_MAILMUNCH_PREFIX. '_user_token');
     380      if (empty($siteId) || empty($userToken)) { return true; }
     381
     382      // The cached list belongs to one MailMunch site; after reconnecting to another site it no longer counts.
     383      $cacheKey = MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids';
     384      $cached = get_transient($cacheKey);
     385      $ids = (is_array($cached) && isset($cached['site_id'], $cached['ids']) && (int) $cached['site_id'] === $siteId) ? $cached['ids'] : null;
     386      if (is_array($ids) && in_array($landingPageId, $ids, true)) { return true; }
     387      if (is_array($ids) && (int) get_transient($cacheKey. '_checked') === $siteId) { return false; }
     388      // MailMunch didn't answer a moment ago: don't make every visitor wait for it again.
     389      if ((int) get_transient($cacheKey. '_unavailable') === $siteId) { return true; }
     390
     391      // Token in a header, not the address (MM-3733 stops accepting ?token= on this route later).
     392      $url = MAILCHIMP_MAILMUNCH_URL. '/sites/'. $siteId. '/landing_pages?version='. rawurlencode(MAILCHIMP_MAILMUNCH_VERSION);
     393      $response = wp_remote_get($url, array('headers' => array('Accept' => 'application/json', 'Authorization' => 'Bearer '. $userToken), 'timeout' => 5));
     394      $pages = (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200) ? null : json_decode(wp_remote_retrieve_body($response));
     395      if (!is_array($pages)) {
     396        set_transient($cacheKey. '_unavailable', $siteId, MINUTE_IN_SECONDS);
     397        return true;
     398      }
     399      $ids = array();
     400      foreach ($pages as $page) { if (isset($page->id)) { $ids[] = (int) $page->id; } }
     401      self::rememberLandingPageIds($ids);
     402      return in_array($landingPageId, $ids, true);
     403    }
     404
     405    static function rememberLandingPageIds($ids) {
     406      $siteId = absint(get_option(MAILCHIMP_MAILMUNCH_PREFIX. '_site_id'));
     407      set_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids', array('site_id' => $siteId, 'ids' => array_values(array_map('intval', $ids))), HOUR_IN_SECONDS);
     408      set_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids_checked', $siteId, 5 * MINUTE_IN_SECONDS);
     409      delete_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids_unavailable');
     410    }
     411
     412    static function forgetLandingPageIds() {
     413      foreach (array('', '_checked', '_unavailable') as $suffix) { delete_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids'. $suffix); }
     414    }
     415
     416    /**
     417     * MM-3766: ids of the landing pages that belong to the connected MailMunch site,
     418     * or WP_Error when MailMunch can't be asked.
     419     */
     420    function getLandingPageIds($siteId=null) {
     421      $pages = $this->getLandingPages($siteId);
     422      if (is_wp_error($pages) || !is_array($pages)) {
     423        return new WP_Error('mailmunch_landing_pages', 'Unable to check landing pages. Please try again later.');
     424      }
     425      $ids = array();
     426      foreach ($pages as $page) {
     427        if (isset($page->id)) { $ids[] = (int) $page->id; }
     428      }
     429      self::rememberLandingPageIds($ids);
     430      return $ids;
    305431    }
    306432
    … …  
    310436
    311437    function setSiteId($siteId) {
     438      self::forgetLandingPageIds();
    312439      return update_option($this->getPrefix(). 'site_id', $siteId);
    313440    }
    … …  
    403530    function signOutUser() {
    404531      delete_option($this->getPrefix(). 'user_token');
     532      self::forgetLandingPageIds();
    405533      delete_option($this->getPrefix(). 'site_id');
    406534      delete_option($this->getPrefix(). 'mailchimp_access_token');
  • mailchimp-forms-by-mailmunch/tags/3.3.2/mailchimp-mailmunch.php

    r3745964 r3821494  
    1717 * Plugin URI:        http://connect.mailchimp.com/integrations/mailmunch-email-list-builder
    1818 * Description:       The easiest way to convert your site visitors into email subscribers. Add beautiful Mailchimp opt-in forms to your WordPress site.
    19  * Version:           3.3.1
     19 * Version:           3.3.2
    2020 * Author:            MailMunch
    2121 * Author URI:        http://www.mailmunch.com
  • mailchimp-forms-by-mailmunch/tags/3.3.2/public/mailchimp-mailmunch-landing-page.php

    r1626000 r3821494  
    11<?php
    22$landing_page_id = get_post_meta( get_the_ID(), '_mailmunch_landing_page_id', true );
     3// MM-3766: only a page of the connected MailMunch site is shown (an Author could set any id before this fix).
     4if ( ! Mailchimp_Mailmunch_Api::landingPageAllowed( $landing_page_id ) ) {
     5  status_header( 404 );
     6  nocache_headers();
     7  header( 'X-MailMunch-Landing-Page: not-this-site' );
     8  include( get_query_template( '404' ) );
     9  die();
     10}
     11$landing_page_id = absint( $landing_page_id );
    312$response = wp_remote_get(MAILCHIMP_MAILMUNCH_PAGE_SERVICE_URL. '/'. $landing_page_id, array(
    413  'headers' => array(
  • mailchimp-forms-by-mailmunch/tags/3.3.2/readme.txt

    r3745964 r3821494  
    55Requires at least: 4.0
    66Tested up to: 7.0
    7 Stable tag: 3.3.1
     7Stable tag: 3.3.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    172172== Changelog ==
    173173
     174= 3.3.2 - Nov 22, 2026 =
     175* Security: Landing Pages are for administrators only, and wp-admin pages no longer contain the MailMunch login token; links into MailMunch now use a one-use sign-in link
     176* Security: a landing page can only be set to one of the connected MailMunch site's own pages
     177
    174178= 3.3.0 - Oct 12, 2026 =
    175179* Fixed security vulnerabilities and known issues
  • mailchimp-forms-by-mailmunch/trunk/admin/class-mailchimp-mailmunch-admin.php

    r3741096 r3821494  
    338338                }
    339339                return $step;
     340        }
     341
     342        /**
     343         * MM-3766: opens MailMunch for an admin without the MailMunch token ever being in a page.
     344         * Hooked to admin_post_<prefix>_sso; links come from Mailchimp_Mailmunch_Api::ssoAdminUrl() and the
     345         * dashboard forms rewritten by Mailchimp_Mailmunch_Api::withoutToken().
     346         */
     347        public function sso_redirect() {
     348                if ( ! current_user_can( 'manage_options' ) ) {
     349                        wp_die( esc_html__( 'Sorry, you are not allowed to do that.' ), 403 );
     350                }
     351                check_admin_referer( MAILCHIMP_MAILMUNCH_PREFIX . '_sso' );
     352
     353                $next_url = isset( $_REQUEST['next_url'] ) ? (string) wp_unslash( $_REQUEST['next_url'] ) : '';
     354                $is_path  = '' !== $next_url && '/' === $next_url[0] && 0 !== strpos( $next_url, '//' ) && false === strpos( $next_url, '\\' );
     355                $is_https = 0 === strpos( $next_url, 'https://' );
     356                if ( ! $is_path && ! $is_https ) {
     357                        wp_die( esc_html__( 'That link is not valid.' ), 400 );
     358                }
     359                $source = isset( $_REQUEST['source'] ) ? sanitize_key( wp_unslash( $_REQUEST['source'] ) ) : 'mailchimp';
     360
     361                $this->initiate_api();
     362                $url = $this->mailmunch_api->getSsoLink( $next_url, $source ? $source : 'mailchimp' );
     363                if ( is_wp_error( $url ) ) {
     364                        wp_die( esc_html( $url->get_error_message() ), 502 );
     365                }
     366
     367                nocache_headers();
     368                wp_redirect( $url ); // MailMunch's own host, checked in getSsoLink().
     369                exit;
     370        }
     371
     372        /**
     373         * MM-3766: the Connect button used to carry the MailMunch token in the page. It now points here; this
     374         * forwards the pop-up to the same MailMunch address as before, so the sign-in flow is unchanged.
     375         */
     376        public function connect_redirect() {
     377                if ( ! current_user_can( 'manage_options' ) ) {
     378                        wp_die( esc_html__( 'Sorry, you are not allowed to do that.' ), 403 );
     379                }
     380                check_admin_referer( MAILCHIMP_MAILMUNCH_PREFIX . '_connect' );
     381                $this->initiate_api();
     382                nocache_headers();
     383                wp_redirect( MAILCHIMP_MAILMUNCH_HOME_URL . '/wordpress/mailchimp/new?token=' . rawurlencode( $this->mailmunch_api->getUserToken() ) );
     384                exit;
    340385        }
    341386
  • mailchimp-forms-by-mailmunch/trunk/admin/partials/mailchimp-mailmunch-connect.php

    r3189361 r3821494  
    2929              <img id="loader" src="<?php echo plugins_url( 'img/loader.gif', dirname(__FILE__) ) ?>" />
    3030
    31               <a id="connect-mailchimp" href="<?php echo MAILCHIMP_MAILMUNCH_HOME_URL ?>/wordpress/mailchimp/new?token=<?php echo $this->mailmunch_api->getUserToken(); ?>">
     31              <a id="connect-mailchimp" href="<?php echo esc_url( wp_nonce_url( add_query_arg( 'action', MAILCHIMP_MAILMUNCH_PREFIX . '_connect', admin_url( 'admin-post.php' ) ), MAILCHIMP_MAILMUNCH_PREFIX . '_connect' ) ); ?>">
    3232                <img src="<?php echo plugins_url( 'img/mailchimp_logo.png', dirname(__FILE__) ) ?>" />
    3333                <span class="button button-primary">Connect to MailChimp</span>
  • mailchimp-forms-by-mailmunch/trunk/includes/class-mailchimp-mailmunch-post-type.php

    r2210463 r3821494  
    4242        'query_var'          => true,
    4343        'rewrite'            => array('with_front' => false, 'slug' => MAILCHIMP_MAILMUNCH_POST_TYPE ),
    44         'capability_type'    => 'post',
     44        // MM-3766: only administrators (manage_options) may see, create or edit landing pages.
     45        // Ordinary post permissions let Contributors and Authors in, and the page shows raw
     46        // MailMunch HTML, which WordPress only trusts administrators with.
     47        'capability_type'    => array( 'mailchimp_mailmunch_landing_page', 'mailchimp_mailmunch_landing_pages' ),
     48        'map_meta_cap'       => false,
     49        'capabilities'       => array(
     50            'edit_post'              => 'manage_options',
     51            'read_post'              => 'read',
     52            'delete_post'            => 'manage_options',
     53            'edit_posts'             => 'manage_options',
     54            'edit_others_posts'      => 'manage_options',
     55            'edit_private_posts'     => 'manage_options',
     56            'edit_published_posts'   => 'manage_options',
     57            'publish_posts'          => 'manage_options',
     58            'read_private_posts'     => 'manage_options',
     59            'delete_posts'           => 'manage_options',
     60            'delete_private_posts'   => 'manage_options',
     61            'delete_published_posts' => 'manage_options',
     62            'delete_others_posts'    => 'manage_options',
     63            'create_posts'           => 'manage_options',
     64        ),
    4565        'has_archive'        => false,
    4666        'hierarchical'       => false,
    … …  
    83103 
    84104  public function get_landing_pages_dropdown($post) {
     105    if (!current_user_can('manage_options')) { return; }
    85106    $this->initiate_api();
    86107    $landing_pages = $this->mailmunch_api->getLandingPages();
     108    if (is_wp_error($landing_pages) || !is_array($landing_pages)) {
     109      echo '<p>' . esc_html__('Unable to load your landing pages from MailMunch. Please try again later.') . '</p>';
     110      return;
     111    }
    87112    $html = '<input type="hidden" name="mailmunch_landing_page_noncename" value="' . wp_create_nonce( plugin_basename(__FILE__) ) . '" />';
    88113    $key = '_mailmunch_landing_page_id';
    89114    $selected_landing_page_id = get_post_meta( $post->ID, $key, true );
    90115    $selected_landing_page_id = intval($selected_landing_page_id);
    91     if (!$selected_landing_page_id) $selected_landing_page_id = $landing_pages[0]->id;
     116    if (!$selected_landing_page_id && !empty($landing_pages)) $selected_landing_page_id = intval($landing_pages[0]->id);
     117    $site_path = "/sites/". intval(get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")). "/landing_pages";
    92118    $html .= '<p>Create or choose the landing page that you want to activate here.</p>';
    93     $html .= '<input id="mailmunch_landing_page_id" name="mailmunch_landing_page_id" type="hidden" value="'. $selected_landing_page_id .'" />';
     119    $html .= '<input id="mailmunch_landing_page_id" name="mailmunch_landing_page_id" type="hidden" value="'. esc_attr($selected_landing_page_id) .'" />';
    94120    foreach ($landing_pages as $key => $landing_page) {
    95       $html .= '<div class="mailmunch-landing-page'. ($selected_landing_page_id == $landing_page->id ? ' active' : '') .'" data-landing-page-id="'. $landing_page->id .'">';
     121      $landing_page_id = intval($landing_page->id);
     122      $html .= '<div class="mailmunch-landing-page'. ($selected_landing_page_id == $landing_page_id ? ' active' : '') .'" data-landing-page-id="'. esc_attr($landing_page_id) .'">';
    96123      $html .= '<div class="page-image">';
    97       $html .= '<img src="'. $landing_page->preview_url .'" />';
     124      $html .= '<img src="'. esc_url($landing_page->preview_url) .'" />';
    98125      $html .= '<div class="page-hover">';
    99       $html .= '<a href="'. MAILCHIMP_MAILMUNCH_URL_SECURED. "/sso?token=". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token"). "&next_url=". urlencode(MAILCHIMP_MAILMUNCH_LANDING_PAGE_URL. "/sites/". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/landing_pages/". $landing_page->id ."/edit") .'" target="_blank" class="mailmunch-edit-page button button-primary button-large">Edit Page</a>';
     126      // MM-3766: no MailMunch token in the page; the link asks for a one-use sign-in link when clicked.
     127      $html .= '<a href="'. esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl($site_path. "/". $landing_page_id ."/edit")) .'" target="_blank" class="mailmunch-edit-page button button-primary button-large">Edit Page</a>';
    100128      $html .= '</div>';
    101129      $html .= '</div>';
    102       $html .= '<div class="page-name">'. ($landing_page->name ? $landing_page->name : "Untitled") .'</div>';
     130      $html .= '<div class="page-name">'. esc_html($landing_page->name ? $landing_page->name : "Untitled") .'</div>';
    103131      $html .= '</div>';
    104132    }
    105     $html .= '<a href="'.MAILCHIMP_MAILMUNCH_URL_SECURED. "/sso?token=". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token"). "&next_url=". urlencode(MAILCHIMP_MAILMUNCH_LANDING_PAGE_URL. "/sites/". get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/landing_pages/new") .'" class="mailmunch-landing-page new-landing-page" target="_blank">';
     133    $html .= '<a href="'. esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl($site_path. "/new")) .'" class="mailmunch-landing-page new-landing-page" target="_blank">';
    106134    $html .= '<div class="page-image">';
    107135    $html .= '<span class="dashicons dashicons-plus-alt"></span>';
    … …  
    130158    }
    131159
    132     // is the user allowed to edit the post or page?
    133     if( ! current_user_can( 'edit_post', $post->ID )){
    134         return $post->ID;
     160    // MM-3766: landing pages are for administrators only.
     161    if ( ! current_user_can( 'manage_options' ) || ! current_user_can( 'edit_post', $post->ID ) ) {
     162        return $post->ID;
    135163    }
    136164
    137165    $key = '_mailmunch_landing_page_id';
    138     $value = $_POST['mailmunch_landing_page_id'];
    139     if( get_post_meta( $post->ID, $key, true ) ) { // if the custom field already has a value
    140       update_post_meta($post->ID, $key, $value);
    141     } else { // if the custom field doesn't have a value
    142       add_post_meta( $post->ID, $key, $value );
    143     }
     166    $value = isset( $_POST['mailmunch_landing_page_id'] ) ? absint( $_POST['mailmunch_landing_page_id'] ) : 0;
    144167    if( !$value ) { // delete if blank
    145168      delete_post_meta( $post->ID, $key );
    146     }
     169      return;
     170    }
     171
     172    // MM-3766: only a landing page of the connected MailMunch site may be shown here.
     173    $this->initiate_api();
     174    $allowed_ids = $this->mailmunch_api->getLandingPageIds();
     175    if ( is_wp_error( $allowed_ids ) || ! in_array( $value, $allowed_ids, true ) ) {
     176      set_transient( MAILCHIMP_MAILMUNCH_PREFIX . '_landing_page_refused_' . get_current_user_id(), is_wp_error( $allowed_ids ) ? 'unavailable' : 'not_yours', 60 );
     177      return $post->ID;
     178    }
     179
     180    update_post_meta( $post->ID, $key, $value );
     181  }
     182
     183  /**
     184   * MM-3766: says why a landing page choice was not saved.
     185   */
     186  public function landing_page_refused_notice() {
     187    $transient = MAILCHIMP_MAILMUNCH_PREFIX . '_landing_page_refused_' . get_current_user_id();
     188    $reason = get_transient( $transient );
     189    if ( ! $reason ) { return; }
     190    delete_transient( $transient );
     191    $message = ( 'unavailable' === $reason )
     192      ? 'The landing page was not changed: MailMunch could not be reached to check it. Please try again.'
     193      : 'The landing page was not changed: it does not belong to the MailMunch site connected to this WordPress site.';
     194    echo '<div class="notice notice-error is-dismissible"><p>' . esc_html( $message ) . '</p></div>';
    147195  }
    148196 
  • mailchimp-forms-by-mailmunch/trunk/includes/class-mailchimp-mailmunch-sidebar-widget.php

    r2210463 r3821494  
    9191    </p>
    9292
    93     <p><a href="<?php echo MAILCHIMP_MAILMUNCH_URL_SECURED ?>/sso?token=<?php echo get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token") ?>&next_url=<?php echo urlencode("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar") ?>" target="_blank">Create New Sidebar Form</a></p>
     93    <p><?php if (current_user_can('manage_options')) { ?><a href="<?php echo esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar")) ?>" target="_blank">Create New Sidebar Form</a><?php } ?></p>
    9494    <?php
    9595    } else {
    9696    ?>
    97     <p>No sidebar forms found. <a href="<?php echo MAILCHIMP_MAILMUNCH_URL_SECURED ?>/sso?token=<?php echo get_option(MAILCHIMP_MAILMUNCH_PREFIX."_user_token") ?>&next_url=<?php echo urlencode("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar") ?>" target="_blank">Create Your First One</a></p>
     97    <p>No sidebar forms found. <?php if (current_user_can('manage_options')) { ?><a href="<?php echo esc_url(Mailchimp_Mailmunch_Api::ssoAdminUrl("/sites/".get_option(MAILCHIMP_MAILMUNCH_PREFIX."_site_id")."/widgets/new?wp_layout=1&widget_type=Sidebar")) ?>" target="_blank">Create Your First One</a><?php } ?></p>
    9898    <?php
    9999    }
  • mailchimp-forms-by-mailmunch/trunk/includes/class-mailchimp-mailmunch.php

    r3745964 r3821494  
    2424define( 'MAILCHIMP_MAILMUNCH_POST_TYPE', 'mailmunch_page' );
    2525define( 'MAILCHIMP_MAILMUNCH_PLUGIN_DIRECTORY', 'mailchimp-forms-by-mailmunch' );
    26 define( 'MAILCHIMP_MAILMUNCH_VERSION', '3.3.1' );
     26define( 'MAILCHIMP_MAILMUNCH_VERSION', '3.3.2' );
    2727
    2828/**
    … …  
    216216
    217217                // Ajax calls
     218                $this->loader->add_action( 'admin_post_'. MAILCHIMP_MAILMUNCH_PREFIX. '_sso', $plugin_admin, 'sso_redirect' );
     219                $this->loader->add_action( 'admin_post_'. MAILCHIMP_MAILMUNCH_PREFIX. '_connect', $plugin_admin, 'connect_redirect' );
    218220                $this->loader->add_action( 'wp_ajax_sign_up', $plugin_admin, 'sign_up' );
    219221                $this->loader->add_action( 'wp_ajax_sign_in', $plugin_admin, 'sign_in' );
    … …  
    272274                $this->loader->add_action('add_meta_boxes', $plugin_post_type, 'post_meta_box', 10, 2);
    273275                $this->loader->add_action('save_post', $plugin_post_type, 'landing_page_save_meta', 10, 2 );
     276                $this->loader->add_action('admin_notices', $plugin_post_type, 'landing_page_refused_notice');
    274277               
    275278                $plugin_rewrite = new Mailchimp_Mailmunch_Rewrite();
  • mailchimp-forms-by-mailmunch/trunk/includes/class-mailmunch-api.php

    r2077433 r3821494  
    3838
    3939    function setUserToken($userToken) {
     40      self::forgetLandingPageIds();
    4041      return update_option($this->getPrefix(). 'user_token', $userToken );
    4142    }
    … …  
    275276      }
    276277
    277       $body = str_replace('{{TOKEN}}', $this->getUserToken(), $request['body']);
    278       return $body;
     278      return self::withoutToken($request['body']);
    279279    }
    280280   
    … …  
    288288      }
    289289
    290       $body = str_replace('{{TOKEN}}', $this->getUserToken(), $request['body']);
    291       return $body;
     290      return self::withoutToken($request['body']);
    292291    }
    293292   
    … …  
    301300      }
    302301
    303       $body = str_replace('{{TOKEN}}', $this->getUserToken(), $request['body']);
    304       return $body;
     302      return self::withoutToken($request['body']);
     303    }
     304
     305    /**
     306     * MM-3766: a wp-admin link that opens MailMunch at $nextPath for the admin clicking it.
     307     * It carries no MailMunch token, only a WordPress nonce; the admin-post handler asks
     308     * MailMunch for a one-use, 60-second sign-in link when it is clicked.
     309     */
     310    static function ssoAdminUrl($nextPath) {
     311      $url = add_query_arg(array(
     312        'action' => MAILCHIMP_MAILMUNCH_PREFIX. '_sso',
     313        'next_url' => rawurlencode($nextPath),
     314      ), admin_url('admin-post.php'));
     315      return wp_nonce_url($url, MAILCHIMP_MAILMUNCH_PREFIX. '_sso');
     316    }
     317
     318    /**
     319     * MM-3766: the dashboard HTML from MailMunch has forms that post the user token to /sso.
     320     * Point them at the nonce-protected admin-post handler instead, and never put the token in.
     321     */
     322    static function withoutToken($html) {
     323      $html = preg_replace('#action="(?:https?:)?//[^"/]+/sso"#', 'action="'. esc_url(admin_url('admin-post.php')). '"', $html);
     324      $fields = '<input type="hidden" name="action" value="'. esc_attr(MAILCHIMP_MAILMUNCH_PREFIX. '_sso'). '" />'
     325        . '<input type="hidden" name="_wpnonce" value="'. esc_attr(wp_create_nonce(MAILCHIMP_MAILMUNCH_PREFIX. '_sso')). '" />';
     326      $html = preg_replace('#<input type="hidden" name="token" value="\{\{TOKEN\}\}"\s*/?>#', $fields, $html);
     327      // Whatever is left of the placeholder is blanked, so the token can never reach the page.
     328      $html = str_replace('{{TOKEN}}', '', $html);
     329      // Report links carry the GraphQL login token (utk); MailMunch adds a fresh one on redeem.
     330      $html = preg_replace('#(&amp;|&)utk=[^"&]*#', '', $html);
     331      $html = preg_replace('#\?utk=[^"&]*(&amp;|&)?#', '?', $html);
     332      return $html;
     333    }
     334
     335    /**
     336     * MM-3766: asks MailMunch (server to server, with the plugin token) for a one-use sign-in link.
     337     * Returns the URL, or WP_Error. Only URLs on the MailMunch host this plugin talks to are accepted.
     338     */
     339    function getSsoLink($nextPath, $source) {
     340      $this->requestType = 'post';
     341      $request = $this->ping('/wordpress/sso_link', array('next_url' => $nextPath, 'source' => $source));
     342      if (is_wp_error($request) || wp_remote_retrieve_response_code($request) != 200) {
     343        return new WP_Error('mailmunch_sso', 'Unable to open MailMunch. Please try again.');
     344      }
     345
     346      $body = json_decode(wp_remote_retrieve_body($request));
     347      if (empty($body) || empty($body->success) || empty($body->url) || !is_string($body->url)) {
     348        return new WP_Error('mailmunch_sso', 'Unable to open MailMunch. Please try again.');
     349      }
     350
     351      $parsed = wp_parse_url($body->url);
     352      $allowedHosts = array_map('strtolower', array_filter(array(
     353        wp_parse_url(MAILCHIMP_MAILMUNCH_URL, PHP_URL_HOST),
     354        wp_parse_url(MAILCHIMP_MAILMUNCH_URL_SECURED, PHP_URL_HOST),
     355      )));
     356      $securedScheme = wp_parse_url(MAILCHIMP_MAILMUNCH_URL_SECURED, PHP_URL_SCHEME);
     357      $allowedSchemes = ($securedScheme === 'https') ? array('https') : array('https', 'http');
     358      if (empty($parsed['host']) || empty($parsed['scheme'])
     359          || !in_array(strtolower($parsed['host']), $allowedHosts, true)
     360          || !in_array(strtolower($parsed['scheme']), $allowedSchemes, true)
     361          || (isset($parsed['path']) && $parsed['path'] !== '/wordpress/sso_redeem')) {
     362        return new WP_Error('mailmunch_sso', 'Unable to open MailMunch. Please try again.');
     363      }
     364
     365      return $body->url;
     366    }
     367
     368    /**
     369     * MM-3766: may this landing page id be shown on this WordPress site? Only the connected MailMunch site's
     370     * own pages. Pages saved before the fix may point at another account's page, so this runs at page view.
     371     * The site's list is cached for an hour; a miss refreshes it at most every 5 minutes (so a page made a
     372     * moment ago works). If MailMunch can't be asked, the page is shown as before rather than taken down.
     373     * Uses no Api instance: the constructor would create a site or user on a visitor's request.
     374     */
     375    static function landingPageAllowed($landingPageId) {
     376      $landingPageId = absint($landingPageId);
     377      if (!$landingPageId) { return false; }
     378      $siteId = absint(get_option(MAILCHIMP_MAILMUNCH_PREFIX. '_site_id'));
     379      $userToken = get_option(MAILCHIMP_MAILMUNCH_PREFIX. '_user_token');
     380      if (empty($siteId) || empty($userToken)) { return true; }
     381
     382      // The cached list belongs to one MailMunch site; after reconnecting to another site it no longer counts.
     383      $cacheKey = MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids';
     384      $cached = get_transient($cacheKey);
     385      $ids = (is_array($cached) && isset($cached['site_id'], $cached['ids']) && (int) $cached['site_id'] === $siteId) ? $cached['ids'] : null;
     386      if (is_array($ids) && in_array($landingPageId, $ids, true)) { return true; }
     387      if (is_array($ids) && (int) get_transient($cacheKey. '_checked') === $siteId) { return false; }
     388      // MailMunch didn't answer a moment ago: don't make every visitor wait for it again.
     389      if ((int) get_transient($cacheKey. '_unavailable') === $siteId) { return true; }
     390
     391      // Token in a header, not the address (MM-3733 stops accepting ?token= on this route later).
     392      $url = MAILCHIMP_MAILMUNCH_URL. '/sites/'. $siteId. '/landing_pages?version='. rawurlencode(MAILCHIMP_MAILMUNCH_VERSION);
     393      $response = wp_remote_get($url, array('headers' => array('Accept' => 'application/json', 'Authorization' => 'Bearer '. $userToken), 'timeout' => 5));
     394      $pages = (is_wp_error($response) || wp_remote_retrieve_response_code($response) != 200) ? null : json_decode(wp_remote_retrieve_body($response));
     395      if (!is_array($pages)) {
     396        set_transient($cacheKey. '_unavailable', $siteId, MINUTE_IN_SECONDS);
     397        return true;
     398      }
     399      $ids = array();
     400      foreach ($pages as $page) { if (isset($page->id)) { $ids[] = (int) $page->id; } }
     401      self::rememberLandingPageIds($ids);
     402      return in_array($landingPageId, $ids, true);
     403    }
     404
     405    static function rememberLandingPageIds($ids) {
     406      $siteId = absint(get_option(MAILCHIMP_MAILMUNCH_PREFIX. '_site_id'));
     407      set_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids', array('site_id' => $siteId, 'ids' => array_values(array_map('intval', $ids))), HOUR_IN_SECONDS);
     408      set_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids_checked', $siteId, 5 * MINUTE_IN_SECONDS);
     409      delete_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids_unavailable');
     410    }
     411
     412    static function forgetLandingPageIds() {
     413      foreach (array('', '_checked', '_unavailable') as $suffix) { delete_transient(MAILCHIMP_MAILMUNCH_PREFIX. '_landing_page_ids'. $suffix); }
     414    }
     415
     416    /**
     417     * MM-3766: ids of the landing pages that belong to the connected MailMunch site,
     418     * or WP_Error when MailMunch can't be asked.
     419     */
     420    function getLandingPageIds($siteId=null) {
     421      $pages = $this->getLandingPages($siteId);
     422      if (is_wp_error($pages) || !is_array($pages)) {
     423        return new WP_Error('mailmunch_landing_pages', 'Unable to check landing pages. Please try again later.');
     424      }
     425      $ids = array();
     426      foreach ($pages as $page) {
     427        if (isset($page->id)) { $ids[] = (int) $page->id; }
     428      }
     429      self::rememberLandingPageIds($ids);
     430      return $ids;
    305431    }
    306432
    … …  
    310436
    311437    function setSiteId($siteId) {
     438      self::forgetLandingPageIds();
    312439      return update_option($this->getPrefix(). 'site_id', $siteId);
    313440    }
    … …  
    403530    function signOutUser() {
    404531      delete_option($this->getPrefix(). 'user_token');
     532      self::forgetLandingPageIds();
    405533      delete_option($this->getPrefix(). 'site_id');
    406534      delete_option($this->getPrefix(). 'mailchimp_access_token');
  • mailchimp-forms-by-mailmunch/trunk/mailchimp-mailmunch.php

    r3745964 r3821494  
    1717 * Plugin URI:        http://connect.mailchimp.com/integrations/mailmunch-email-list-builder
    1818 * Description:       The easiest way to convert your site visitors into email subscribers. Add beautiful Mailchimp opt-in forms to your WordPress site.
    19  * Version:           3.3.1
     19 * Version:           3.3.2
    2020 * Author:            MailMunch
    2121 * Author URI:        http://www.mailmunch.com
  • mailchimp-forms-by-mailmunch/trunk/public/mailchimp-mailmunch-landing-page.php

    r1626000 r3821494  
    11<?php
    22$landing_page_id = get_post_meta( get_the_ID(), '_mailmunch_landing_page_id', true );
     3// MM-3766: only a page of the connected MailMunch site is shown (an Author could set any id before this fix).
     4if ( ! Mailchimp_Mailmunch_Api::landingPageAllowed( $landing_page_id ) ) {
     5  status_header( 404 );
     6  nocache_headers();
     7  header( 'X-MailMunch-Landing-Page: not-this-site' );
     8  include( get_query_template( '404' ) );
     9  die();
     10}
     11$landing_page_id = absint( $landing_page_id );
    312$response = wp_remote_get(MAILCHIMP_MAILMUNCH_PAGE_SERVICE_URL. '/'. $landing_page_id, array(
    413  'headers' => array(
  • mailchimp-forms-by-mailmunch/trunk/readme.txt

    r3745964 r3821494  
    55Requires at least: 4.0
    66Tested up to: 7.0
    7 Stable tag: 3.3.1
     7Stable tag: 3.3.2
    88License: GPLv2 or later
    99License URI: http://www.gnu.org/licenses/gpl-2.0.html
    … …  
    172172== Changelog ==
    173173
     174= 3.3.2 - Nov 22, 2026 =
     175* Security: Landing Pages are for administrators only, and wp-admin pages no longer contain the MailMunch login token; links into MailMunch now use a one-use sign-in link
     176* Security: a landing page can only be set to one of the connected MailMunch site's own pages
     177
    174178= 3.3.0 - Oct 12, 2026 =
    175179* Fixed security vulnerabilities and known issues
Note: See TracChangeset for help on using the changeset viewer.