public function get_term_images($num = 0, $size_name = 'thumbnail', $term_id, $check_dupes = true, $force_regen = false, $meta_pre = 'og', $tag_pre = 'og') { if ($this->p->debug->enabled) { $this->p->debug->args(array('num' => $num, 'size_name' => $size_name, 'term_id' => $term_id, 'check_dupes' => $check_dupes, 'force_regen' => $force_regen, 'meta_pre' => $meta_pre, 'tag_pre' => $tag_pre)); } $meta_ret = array(); $meta_image = SucomUtil::meta_image_tags($tag_pre); if (empty($term_id)) { return $meta_ret; } foreach (apply_filters($this->p->cf['lca'] . '_term_image_ids', array(), $term_id) as $pid) { if ($pid > 0) { list($meta_image[$tag_pre . ':image'], $meta_image[$tag_pre . ':image:width'], $meta_image[$tag_pre . ':image:height'], $meta_image[$tag_pre . ':image:cropped'], $meta_image[$tag_pre . ':image:id']) = $this->p->media->get_attachment_image_src($pid, $size_name, $check_dupes, $force_regen); if (!empty($meta_image[$tag_pre . ':image']) && $this->p->util->push_max($meta_ret, $meta_image, $num)) { return $meta_ret; } } } return $meta_ret; }
public function get_content_images($num = 0, $size_name = 'thumbnail', $post_id = 0, $check_dupes = true, $content = '') { if ($this->p->debug->enabled) { $this->p->debug->args(array('num' => $num, 'size_name' => $size_name, 'post_id' => $post_id, 'check_dupes' => $check_dupes, 'content' => strlen($content) . ' chars')); } $og_ret = array(); $og_image = SucomUtil::meta_image_tags('og'); $size_info = $this->get_size_info($size_name); // allow custom content to be passed as argument if (empty($content)) { $content = $this->p->webpage->get_content($post_id, false); } // use_post = false if (empty($content)) { if ($this->p->debug->enabled) { $this->p->debug->log('exiting early: empty post content'); } return $og_ret; } // img attributes in order of preference // data_tags_preg provides a filter hook for 3rd party modules like ngg to return image information if (preg_match_all('/<(' . $this->data_tags_preg . '[^>]*? ' . $this->data_attr_preg . '=[\'"]([0-9]+)[\'"]|' . '(img)[^>]*? (data-share-src|src)=[\'"]([^\'"]+)[\'"])[^>]*>/s', $content, $match, PREG_SET_ORDER)) { if ($this->p->debug->enabled) { $this->p->debug->log(count($match) . ' x matching <' . $this->data_tags_preg . '/> html tag(s) found'); } foreach ($match as $img_num => $img_arr) { $tag_value = $img_arr[0]; if (empty($img_arr[5])) { $tag_name = $img_arr[2]; // img $attr_name = $img_arr[3]; // data-wp-pid $attr_value = $img_arr[4]; // id } else { $tag_name = $img_arr[5]; // img $attr_name = $img_arr[6]; // data-share-src|src $attr_value = $img_arr[7]; // url } if ($this->p->debug->enabled) { $this->p->debug->log('match ' . $img_num . ': ' . $tag_name . ' ' . $attr_name . '="' . $attr_value . '"'); } switch ($attr_name) { case 'data-wp-pid': list($og_image['og:image'], $og_image['og:image:width'], $og_image['og:image:height'], $og_image['og:image:cropped'], $og_image['og:image:id']) = $this->get_attachment_image_src($attr_value, $size_name, false); break; // filter hook for 3rd party modules to return image information // filter hook for 3rd party modules to return image information case preg_match('/^data-[a-z]+-pid$/', $attr_name) ? true : false: $filter_name = $this->p->cf['lca'] . '_get_content_' . $tag_name . '_' . preg_replace('/-/', '_', $attr_name); list($og_image['og:image'], $og_image['og:image:width'], $og_image['og:image:height'], $og_image['og:image:cropped'], $og_image['og:image:id']) = apply_filters($filter_name, array(null, null, null, null), $attr_value, $size_name, false); break; default: // prevent duplicates by silently ignoring ngg images (already processed by the ngg module) if ($this->p->is_avail['media']['ngg'] === true && !empty($this->p->mods['media']['ngg']) && (strpos($tag_value, " class='ngg-") !== false || preg_match('/^' . $this->p->mods['media']['ngg']->img_src_preg . '$/', $attr_value))) { break; } // stop here // recognize gravatar images in the content if (preg_match('/^https?:\\/\\/([^\\.]+\\.)?gravatar\\.com\\/avatar\\/[a-zA-Z0-9]+/', $attr_value, $match)) { $og_image['og:image'] = $match[0] . '?s=' . $size_info['width'] . '&d=404&r=G'; $og_image['og:image:width'] = $size_info['width']; $og_image['og:image:height'] = $size_info['width']; // square image break; // stop here } $og_image = array('og:image' => $attr_value, 'og:image:width' => -1, 'og:image:height' => -1); // try and get the width and height from the image attributes if (!empty($og_image['og:image'])) { if (preg_match('/ width=[\'"]?([0-9]+)[\'"]?/i', $tag_value, $match)) { $og_image['og:image:width'] = $match[1]; } if (preg_match('/ height=[\'"]?([0-9]+)[\'"]?/i', $tag_value, $match)) { $og_image['og:image:height'] = $match[1]; } } $is_sufficient_width = $og_image['og:image:width'] >= $size_info['width'] ? true : false; $is_sufficient_height = $og_image['og:image:height'] >= $size_info['height'] ? true : false; // make sure the image width and height are large enough if ($attr_name == 'data-share-src' || $attr_name == 'src' && empty($this->p->options['plugin_ignore_small_img']) || $attr_name == 'src' && $size_info['crop'] === 1 && $is_sufficient_width && $is_sufficient_height || $attr_name == 'src' && $size_info['crop'] !== 1 && ($is_sufficient_width || $is_sufficient_height)) { // data-share-src attribute used and/or image size is acceptable // check for relative urls, just in case $og_image['og:image'] = $this->p->util->fix_relative_url($og_image['og:image']); } else { if ($this->p->debug->enabled) { $this->p->debug->log('image rejected: width / height attributes missing or too small for ' . $size_name); if (is_admin()) { $this->p->notice->err('Image ' . $og_image['og:image'] . ' rejected - width / height missing or too small for ' . $size_name . '.'); } } $og_image = array(); } break; } if (!empty($og_image['og:image']) && ($check_dupes === false || $this->p->util->is_uniq_url($og_image['og:image'], $size_name))) { if ($this->p->util->push_max($og_ret, $og_image, $num)) { return $og_ret; } } } return $og_ret; } if ($this->p->debug->enabled) { $this->p->debug->log('no matching <' . $this->data_tags_preg . '/> html tag(s) found'); } return $og_ret; }
public function get_content_images($num = 0, $size_name = 'thumbnail', $post_id = 0, $check_dupes = true, $content = '') { if ($this->p->debug->enabled) { $this->p->debug->args(array('num' => $num, 'size_name' => $size_name, 'post_id' => $post_id, 'check_dupes' => $check_dupes, 'content' => strlen($content) . ' chars')); } $og_ret = array(); // allow custom content to be passed as argument if (empty($content)) { $content_provided = false; $content = $this->p->webpage->get_content($post_id, false); // use_post = false } else { $content_provided = true; } if (empty($content)) { if ($this->p->debug->enabled) { $this->p->debug->log('exiting early: empty post content'); } return $og_ret; } $og_image = SucomUtil::meta_image_tags('og'); $size_info = $this->get_size_info($size_name); $img_preg = $this->default_img_preg; // allow the html_tag and pid_attr regex to be modified foreach (array('html_tag', 'pid_attr') as $type) { $filter_name = $this->p->cf['lca'] . '_content_image_preg_' . $type; if (has_filter($filter_name)) { $img_preg[$type] = apply_filters($filter_name, $this->default_img_preg[$type]); if ($this->p->debug->enabled) { $this->p->debug->log('filtered image preg ' . $type . ' = \'' . $img_preg[$type] . '\''); } } } // img attributes in order of preference if (preg_match_all('/<((' . $img_preg['html_tag'] . ')[^>]*? (' . $img_preg['pid_attr'] . ')=[\'"]([0-9]+)[\'"]|' . '(img)[^>]*? (data-share-src|src)=[\'"]([^\'"]+)[\'"])[^>]*>/s', $content, $all_matches, PREG_SET_ORDER)) { if ($this->p->debug->enabled) { $this->p->debug->log(count($all_matches) . ' x matching <' . $img_preg['html_tag'] . '/> html tag(s) found'); } foreach ($all_matches as $img_num => $img_arr) { $tag_value = $img_arr[0]; if (empty($img_arr[5])) { $tag_name = $img_arr[2]; // img $attr_name = $img_arr[3]; // data-wp-pid $attr_value = $img_arr[4]; // id } else { $tag_name = $img_arr[5]; // img $attr_name = $img_arr[6]; // data-share-src|src $attr_value = $img_arr[7]; // url } if ($this->p->debug->enabled) { $this->p->debug->log('match ' . $img_num . ': ' . $tag_name . ' ' . $attr_name . '="' . $attr_value . '"'); } switch ($attr_name) { // wordpress media library image id case 'data-wp-pid': list($og_image['og:image'], $og_image['og:image:width'], $og_image['og:image:height'], $og_image['og:image:cropped'], $og_image['og:image:id']) = $this->get_attachment_image_src($attr_value, $size_name, false); break; // check for other data attributes like 'data-ngg-pid' // check for other data attributes like 'data-ngg-pid' case preg_match('/^' . $img_preg['pid_attr'] . '$/', $attr_name) ? true : false: // build a filter hook for 3rd party modules to return image information $filter_name = $this->p->cf['lca'] . '_get_content_' . $tag_name . '_' . preg_replace('/-/', '_', $attr_name); list($og_image['og:image'], $og_image['og:image:width'], $og_image['og:image:height'], $og_image['og:image:cropped'], $og_image['og:image:id']) = apply_filters($filter_name, array(null, null, null, null, null), $attr_value, $size_name, false); break; default: // prevent duplicates by silently ignoring ngg images (already processed by the ngg module) if ($this->p->is_avail['media']['ngg'] === true && !empty($this->p->mods['media']['ngg']) && (preg_match('/ class=[\'"]ngg[_-]/', $tag_value) || preg_match('/^(' . $img_preg['ngg_src'] . ')$/', $attr_value))) { break; } // stop here // recognize gravatar images in the content if (preg_match('/^https?:\\/\\/([^\\.]+\\.)?gravatar\\.com\\/avatar\\/[a-zA-Z0-9]+/', $attr_value, $match)) { $og_image['og:image'] = $match[0] . '?s=' . $size_info['width'] . '&d=404&r=G'; $og_image['og:image:width'] = $size_info['width']; $og_image['og:image:height'] = $size_info['width']; // square image break; // stop here } // check for image ID in class for old content w/o the data-wp-pid attribute if (preg_match('/class="[^"]+ wp-image-([0-9]+)/', $tag_value, $match)) { list($og_image['og:image'], $og_image['og:image:width'], $og_image['og:image:height'], $og_image['og:image:cropped'], $og_image['og:image:id']) = $this->get_attachment_image_src($match[1], $size_name, false); break; // stop here } else { $og_image = array('og:image' => $attr_value, 'og:image:width' => -1, 'og:image:height' => -1); } // try and get the width and height from the image attributes if (!empty($og_image['og:image'])) { if (preg_match('/ width=[\'"]?([0-9]+)[\'"]?/i', $tag_value, $match)) { $og_image['og:image:width'] = $match[1]; } if (preg_match('/ height=[\'"]?([0-9]+)[\'"]?/i', $tag_value, $match)) { $og_image['og:image:height'] = $match[1]; } } $is_sufficient_width = $og_image['og:image:width'] >= $size_info['width'] ? true : false; $is_sufficient_height = $og_image['og:image:height'] >= $size_info['height'] ? true : false; $accept_img_size = apply_filters($this->p->cf['lca'] . '_content_accept_img_size', empty($this->p->options['plugin_ignore_small_img']) ? true : false, $og_image['og:image'], $og_image['og:image:width'], $og_image['og:image:height'], $size_name, $post_id); // make sure the image width and height are large enough if ($attr_name == 'src' && $accept_img_size || $attr_name == 'src' && $size_info['crop'] && ($is_sufficient_width && $is_sufficient_height) || $attr_name == 'src' && !$size_info['crop'] && ($is_sufficient_width || $is_sufficient_height) || $attr_name == 'data-share-src') { // data-share-src attribute used and/or image size is acceptable // check for relative urls, just in case $og_image['og:image'] = $this->p->util->fix_relative_url($og_image['og:image']); } else { if ($this->p->debug->enabled) { $this->p->debug->log('content image rejected: ' . 'width / height missing or too small for ' . $size_name); } if (is_admin()) { $short = $this->p->cf['plugin'][$this->p->cf['lca']]['short']; $size_label = $this->p->util->get_image_size_label($size_name); $msg_id = 'content_' . $og_image['og:image'] . '_' . $size_name . '_rejected'; if (!$content_provided) { $data_wp_pid_msg = ' ' . sprintf(__('%1$s includes an additional \'data-wp-pid\' attribute for images from the Media Library to supplement the width / height information — if this image was selected from the Media Library before %2$s was first activated, try removing and adding the image back to your content.', 'nextgen-facebook'), $short, $short); } else { $data_wp_pid_msg = ''; } $this->p->notice->err(sprintf(__('Content image %1$s has been ignored — the image width / height attributes are missing or too small for the %2$s image dimensions.', 'nextgen-facebook'), $og_image['og:image'], $size_label . ' (' . $size_name . ')') . $data_wp_pid_msg, false, true, $msg_id, true); } $og_image = array(); } break; } if (!empty($og_image['og:image']) && ($check_dupes === false || $this->p->util->is_uniq_url($og_image['og:image'], $size_name))) { if ($this->p->util->push_max($og_ret, $og_image, $num)) { return $og_ret; } } } return $og_ret; } if ($this->p->debug->enabled) { $this->p->debug->log('no matching <' . $img_preg['html_tag'] . '/> html tag(s) found'); } return $og_ret; }
public function get_meta_image($num = 0, $size_name = 'thumbnail', $id, $check_dupes = true, $force_regen = false, $meta_pre = 'og', $tag_pre = 'og') { if ($this->p->debug->enabled) { $this->p->debug->args(array('num' => $num, 'size_name' => $size_name, 'id' => $id, 'check_dupes' => $check_dupes, 'force_regen' => $force_regen, 'meta_pre' => $meta_pre, 'tag_pre' => $tag_pre), get_class($this)); } $meta_ret = array(); $meta_image = SucomUtil::meta_image_tags($tag_pre); if (empty($id)) { return $meta_ret; } foreach (array_unique(array($meta_pre, 'og')) as $prefix) { $pid = $this->get_options($id, $prefix . '_img_id'); $pre = $this->get_options($id, $prefix . '_img_id_pre'); $url = $this->get_options($id, $prefix . '_img_url'); if ($pid > 0) { $pid = $pre === 'ngg' ? 'ngg-' . $pid : $pid; if ($this->p->debug->enabled) { $this->p->debug->log('using custom ' . $prefix . ' image id = "' . $pid . '"', get_class($this)); } // log extended class name list($meta_image[$tag_pre . ':image'], $meta_image[$tag_pre . ':image:width'], $meta_image[$tag_pre . ':image:height'], $meta_image[$tag_pre . ':image:cropped'], $meta_image[$tag_pre . ':image:id']) = $this->p->media->get_attachment_image_src($pid, $size_name, $check_dupes, $force_regen); } if (empty($meta_image[$tag_pre . ':image']) && !empty($url)) { if ($this->p->debug->enabled) { $this->p->debug->log('using custom ' . $prefix . ' image url = "' . $url . '"', get_class($this)); } // log extended class name list($meta_image[$tag_pre . ':image'], $meta_image[$tag_pre . ':image:width'], $meta_image[$tag_pre . ':image:height'], $meta_image[$tag_pre . ':image:cropped'], $meta_image[$tag_pre . ':image:id']) = array($url, -1, -1, -1, -1); } if (!empty($meta_image[$tag_pre . ':image']) && $this->p->util->push_max($meta_ret, $meta_image, $num)) { return $meta_ret; } } return $meta_ret; }