Beispiel #1
0
 function maybe_send_email_notifications()
 {
     global $wpdb;
     /** @var wpdb $wpdb */
     if (!($this->conf->options['send_email_notifications'] || $this->conf->options['send_authors_email_notifications'])) {
         return;
     }
     //Find links that have been detected as broken since the last sent notification.
     $last_notification = date('Y-m-d H:i:s', $this->conf->options['last_notification_sent']);
     $where = $wpdb->prepare('( first_failure >= %s )', $last_notification);
     $links = blc_get_links(array('s_filter' => 'broken', 'where_expr' => $where, 'load_instances' => true, 'load_containers' => true, 'load_wrapped_objects' => $this->conf->options['send_authors_email_notifications'], 'max_results' => 0));
     if (empty($links)) {
         return;
     }
     //Send the admin notification
     $admin_email = get_option('admin_email');
     if ($this->conf->options['send_email_notifications'] && !empty($admin_email)) {
         $this->send_admin_notification($links, $admin_email);
     }
     //Send notifications to post authors
     if ($this->conf->options['send_authors_email_notifications']) {
         $this->send_authors_notifications($links);
     }
     $this->conf->options['last_notification_sent'] = time();
     $this->conf->save_options();
 }
 static function sync_link_data()
 {
     $max_results = get_option('mainwp_child_blc_max_number_of_links', 50);
     $params = array(array('load_instances' => true));
     if (!empty($max_results)) {
         $params['max_results'] = $max_results;
     }
     $links = blc_get_links($params);
     $get_fields = array('link_id', 'url', 'being_checked', 'last_check', 'last_check_attempt', 'check_count', 'http_code', 'request_duration', 'timeout', 'redirect_count', 'final_url', 'broken', 'first_failure', 'last_success', 'may_recheck', 'false_positive', 'dismissed', 'status_text', 'status_code', 'log');
     $return = "";
     $site_id = $_POST['site_id'];
     $blc_option = get_option('wsblc_options');
     if (is_string($blc_option) && !empty($blc_option)) {
         $blc_option = json_decode($blc_option, true);
     }
     if (is_array($links)) {
         foreach ($links as $link) {
             $lnk = new stdClass();
             foreach ($get_fields as $field) {
                 $lnk->{$field} = $link->{$field};
             }
             if (!empty($link->post_date)) {
                 $lnk->post_date = $link->post_date;
             }
             $days_broken = 0;
             if ($link->broken) {
                 //Add a highlight to broken links that appear to be permanently broken
                 $days_broken = intval((time() - $link->first_failure) / (3600 * 24));
                 if ($days_broken >= $blc_option['failure_duration_threshold']) {
                     $lnk->permanently_broken = 1;
                     if ($blc_option['highlight_permanent_failures']) {
                         $lnk->permanently_broken_highlight = 1;
                     }
                 }
             }
             $lnk->days_broken = $days_broken;
             if (!empty($link->_instances)) {
                 $instance = reset($link->_instances);
                 $lnk->link_text = $instance->ui_get_link_text();
                 $lnk->count_instance = count($link->_instances);
                 $container = $instance->get_container();
                 /** @var blcContainer $container */
                 $lnk->container = $container;
                 if (!empty($container)) {
                     $lnk->container_type = $container->container_type;
                     $lnk->container_id = $container->container_id;
                     $lnk->source_data = MainWPChildLinksChecker::Instance()->ui_get_source($container, $instance->container_field);
                 }
                 $can_edit_text = false;
                 $can_edit_url = false;
                 $editable_link_texts = $non_editable_link_texts = array();
                 $instances = $link->_instances;
                 foreach ($instances as $instance) {
                     if ($instance->is_link_text_editable()) {
                         $can_edit_text = true;
                         $editable_link_texts[$instance->link_text] = true;
                     } else {
                         $non_editable_link_texts[$instance->link_text] = true;
                     }
                     if ($instance->is_url_editable()) {
                         $can_edit_url = true;
                     }
                 }
                 $link_texts = $can_edit_text ? $editable_link_texts : $non_editable_link_texts;
                 $data_link_text = '';
                 if (count($link_texts) === 1) {
                     //All instances have the same text - use it.
                     $link_text = key($link_texts);
                     $data_link_text = esc_attr($link_text);
                 }
                 $lnk->data_link_text = $data_link_text;
                 $lnk->can_edit_url = $can_edit_url;
                 $lnk->can_edit_text = $can_edit_text;
             } else {
                 $lnk->link_text = "";
                 $lnk->count_instance = 0;
             }
             $lnk->site_id = $site_id;
             $return[] = $lnk;
         }
     } else {
         return "";
     }
     return $return;
 }
 /**
  * Retrieve a list of links found in this container.
  *
  * @access public 
  *
  * @return array of blcLink
  */
 function get_links()
 {
     $params = array('s_container_type' => $this->container_type, 's_container_id' => $this->container_id);
     return blc_get_links($params);
 }
 function send_email_notifications()
 {
     global $wpdb;
     //Find links that have been detected as broken since the last sent notification.
     $last_notification = date('Y-m-d H:i:s', $this->conf->options['last_notification_sent']);
     $where = $wpdb->prepare('( first_failure >= %s )', $last_notification);
     $links = blc_get_links(array('s_filter' => 'broken', 'where_expr' => $where, 'load_instances' => true, 'max_results' => 0));
     if (empty($links)) {
         return;
     }
     $cnt = count($links);
     //Prepare email message
     $subject = sprintf(__("[%s] Broken links detected", 'broken-link-checker'), html_entity_decode(get_option('blogname'), ENT_QUOTES));
     $body = sprintf(_n("Broken Link Checker has detected %d new broken link on your site.", "Broken Link Checker has detected %d new broken links on your site.", $cnt, 'broken-link-checker'), $cnt);
     $body .= "<br>";
     $max_displayed_links = 5;
     if ($cnt > $max_displayed_links) {
         $line = sprintf(_n("Here's a list of the first %d broken links:", "Here's a list of the first %d broken links:", $max_displayed_links, 'broken-link-checker'), $max_displayed_links);
     } else {
         $line = __("Here's a list of the new broken links: ", 'broken-link-checker');
     }
     $body .= "<p>{$line}</p>";
     //Show up to $max_displayed_links broken link instances right in the email.
     $displayed = 0;
     foreach ($links as $link) {
         $instances = $link->get_instances();
         foreach ($instances as $instance) {
             $pieces = array(sprintf(__('Link text : %s', 'broken-link-checker'), $instance->ui_get_link_text('email')), sprintf(__('Link URL : <a href="%s">%s</a>', 'broken-link-checker'), htmlentities($link->url), blcUtility::truncate($link->url, 70, '')), sprintf(__('Source : %s', 'broken-link-checker'), $instance->ui_get_source('email')));
             $link_entry = implode("<br>", $pieces);
             $body .= "{$link_entry}<br><br>";
             $displayed++;
             if ($displayed >= $max_displayed_links) {
                 break 2;
                 //Exit both foreach loops
             }
         }
     }
     //Add a link to the "Broken Links" tab.
     $body .= __("You can see all broken links here:", 'broken-link-checker') . "<br>";
     $link_page = admin_url('tools.php?page=view-broken-links');
     $body .= sprintf('<a href="%1$s">%1$s</a>', $link_page);
     //Need to override the default 'text/plain' content type to send a HTML email.
     add_filter('wp_mail_content_type', array(&$this, 'override_mail_content_type'));
     //Send the notification
     $rez = wp_mail(get_option('admin_email'), $subject, $body);
     if ($rez) {
         $this->conf->options['last_notification_sent'] = time();
         $this->conf->save_options();
     }
     //Remove the override so that it doesn't interfere with other plugins that might
     //want to send normal plaintext emails.
     remove_filter('wp_mail_content_type', array(&$this, 'override_mail_content_type'));
 }