function set_blog($blog_id) { if (is_numeric($blog_id)) { $blog_id = (int) $blog_id; } else { $blog = 'http://' . preg_replace('#^https?://#', '', $blog_id); if (!($parsed = parse_url($blog)) || empty($parsed['host'])) { fwrite(STDERR, "Error: can not determine blog_id from {$blog_id}\n"); exit; } if (empty($parsed['path'])) { $parsed['path'] = '/'; } if (!($blog = get_blog_info($parsed['host'], $parsed['path']))) { fwrite(STDERR, "Error: Could not find blog\n"); exit; } $blog_id = (int) $blog->blog_id; // Restore global $current_blog global $current_blog; $current_blog = $blog; } if (function_exists('is_multisite')) { if (is_multisite()) { switch_to_blog($blog_id); } } return $blog_id; }
public function check_gated($post_id) { if (in_array($post_id, $this->post_stack)) { // infinite loop detected // redirect to home page of site // send email to all admins about redirect loop $this->post_stack[$post_id] = $post_id; $to = array(); $blogusers = get_users('role=Administrator'); $domain = $_SERVER['HTTP_HOST']; $domain = preg_replace('/^www\\./', '', $domain); $domain = preg_replace('/^([^\\.]+\\.){2}.*$/', '\\1', $domain); $domain = preg_replace('/\\.$/', '', $domain); //print_r($blogusers); foreach ($blogusers as $user) { $to[] = $user->user_email; } //print_r($to); die; $subject = 'Infinite Redirect Loop on ' . get_blog_info('name') . ' Website'; $message = ''; $message .= 'An infinite redirect loop has been detected in gated content on the ' . get_blog_info('name') . ' website (' . get_site_url() . ')' . "\r\n\r\n"; $message .= 'The following is the redirect path that was followed when attempting to ' . 'redirect a user that was not permitted to view the first url in the list:' . "\r\n"; $ajax_url = admin_url('admin-ajax.php'); foreach ($this->post_stack as $id) { $post_type = get_post_type($id); if ($post_type != 'blunt-gated-content') { $link = get_permalink($id); } else { $title = get_post_meta($id, 'blunt_gated_content_file_title', true); $link = $ajax_url . '?action=blunt_gated_content&id=' . $post_id . '&title=' . str_replace(' ', '+', $title); } $message .= $link . "\r\n"; } $headers = 'From: wordpress@' . $domain; wp_mail(implode(',', $to), $subject, $message, $headers); $this->redirect(0); } $this->post_stack[$post_id] = $post_id; // see if conditions are met for $post_id // get all meta to spead up the process $post_type = get_post_type($post_id); $meta = get_post_meta($post_id); $default = '1'; if ($post_type != 'blunt-gated-content') { $default = '0'; } $gated_content = get_post_meta($post_id, 'blunt_gated_content', true); if ($gated_content === '') { $gated_content = $default; } $gated_content = intval($gated_content); if (!$gated_content) { // the content is not being gated return; } // it is gated content, check gates //echo 'check gates'; sie; if (!$this->check_gates($post_id)) { $this->redirect($post_id); } // getting here means that the visitor can view the content // return to calling function }