", $num_cells);
}
}
public function uploadIconPostsMetaBox() {
?>
_is_static to
* true, so we can test for it later.
*/
public function check_excerpt($theExcerpt) {
$this->_is_excerpt = true;
return $theExcerpt;
}
/**
* This function is set as a filter on post content, and adds the attachment
* list if needed.
*
* @param string $post_content - Content of the current post
* @return string - Modified post content
*/
public function attach_to_post($post_content) {
global $post;
$dont_show_on_cats = ($this->_settings['wam_dont_show_on_cat_page'] == 'true');
$this_post = (get_post_meta($post->ID, '_wam_show_attachments', true) == 'true');
$dont_show_on_excerpts = (($this->_settings['wam_dont_show_on_excerpts'] == 'true') && !is_feed());
$show_on_home_page = (($this->_settings['wam']['home_page'] == 'show') || is_feed());
/**
* If we show on all posts or this post AND if we show on categories or
* this isn't a category AND if we show on excerpt or this isn't an
* excerpt
*/
if ( ($this->_settings['wam_list_on_posts'] == 'all' || ($this->_settings['wam_list_on_posts'] == 'some' && $this_post)) &&
(!$dont_show_on_cats || !is_category()) &&
( !$dont_show_on_excerpts || !$this->_is_excerpt ) &&
( 'true' == $this->_settings['wam']['show_on_rss'] || !is_feed() ) &&
( $show_on_home_page || !is_home() ) ) {
$post_content .= $this->_get_attachments();
}
return $post_content;
}
public function attach_to_rss($post_content) {
$this->_is_excerpt = false;
return $this->attach_to_post($post_content);
}
/**
* Finds the attachments that belong to the current post, and creates an
* unordered list of them
*
* @return string - unordered list of attachments
*/
private function _get_attachments() {
global $wpdb, $post;
$dont_show_linked = (get_option('wam_dont_show_files_already_linked') == 'true');
$attachment_str = '';
$query = "SELECT `id`,`guid`, `post_content`, `post_title` FROM {$wpdb->posts} WHERE (post_status = 'attachment' || post_type = 'attachment') AND post_parent = '{$post->ID}'";
$attachments = $wpdb->get_results($query);
if (count($attachments) > 0) {
$attachment_list = array();
foreach ( $attachments as $attachment ) {
if (!$dont_show_linked || (!$this->_isFileLinked($post->post_content, $attachment) )) {
$link = $this->_get_attachment_link($attachment);
$post_content = empty($attachment->post_content)? '':"
';
}
}
return $attachment_str;
}
private function _isFileLinked($content, $file) {
$pathinfo = pathinfo($file->guid);
$filename = preg_quote($pathinfo['dirname'].'/'.basename($pathinfo['filename'], '.'.$pathinfo['basename']), '/');
$ext = preg_quote($pathinfo['extension']);
return (
preg_match("/src=['\"]{$filename}(-\d*x\d*)?\.{$ext}['\"]/", $content) ||
preg_match("/class=['\"][^'\"]*wp-image-{$file->id}[^'\"]*['\"]/", $content)
);
}
/**
* Returns a link to a file, including an icon if needed.
*
* @param object $attachment - wp-attachment object
* @return string - link to attachment
*/
private function _get_attachment_link($attachment) {
$icon = '';
if ($this->_settings['wam_show_file_icons'] == 'true') {
$ext = $this->_get_extension($attachment->guid);
$img = $this->_settings['wam_default_icon'];
foreach( $this->_settings['icons'] as $cur_icon ) {
$cur_icon['exts'] = preg_split('/\s*,\s*/', $cur_icon['exts']);
if ( in_array($ext, $cur_icon['exts']) ) {
$img = $cur_icon['icon'];
break;
}
}
if (!empty($img)) {
$icon_url = path_join($this->_get_icon_url(),$img);
$img_size = getimagesize(path_join($this->_icon_dir,$img));
$img_alt = esc_html($ext);
$icon = " ";
}
}
return sprintf("%s{$attachment->post_title}", $icon);
}
/**
* This attaches to init, and was needed so we could use wp_redirect. It
* checks to see if an icon is has been requested to be removed. Then it
* removes it, and redirects back to the options page, and gives a succes or
* error message
*/
public function handle_actions() {
if (isset($_GET['page']) && $_GET['page'] == 'attachment_manager') {
if ( isset($_GET['action']) ) {
if ('remove' == $_GET['action']) {
$this->_settings['icons'] = array_diff($this->_settings['icons'], array(preg_replace('/[^\w-]/', '', $_GET['icon'])));
update_option('icons', $this->_settings['icons']);
if (is_writable($this->_icon_dir.'/'.$_GET['icon']) && @unlink($this->_icon_dir.'/'.$_GET['icon'])) {
wp_redirect('options-general.php?page=attachment_manager&remove=true');
} else {
wp_redirect('options-general.php?page=attachment_manager&remove=false');
}
}
exit;
} elseif (isset($_FILES['wam_add_icon'])) {
$filename = basename($_FILES['wam_add_icon']['name']);
$file = path_join($this->_icon_dir, $filename);
$icon_clean_name = preg_replace('/[^\w-]/', '', $filename);
$this->_settings['icons'][$icon_clean_name] = array('exts'=>'', 'icon'=>$filename);
update_option('icons', $this->_settings['icons']);
if (move_uploaded_file($_FILES['wam_add_icon']['tmp_name'], $file)) {
wp_redirect('options-general.php?page=attachment_manager&upload=true');
} else {
wp_redirect('options-general.php?page=attachment_manager&upload=false');
}
}
}
}
/**
* Used to find the file extension of any file (given a filename.ext,
* path/to/filename.ext, or even http://example.com/path/to/filename.ext
*
* @param string $file_name
* @return string - lowercase file extension
*/
private function _get_extension($file_name) {
$file = pathinfo($file_name);
return strtolower($file['extension']);
}
/**
* Either returns the icon file types from the plugin options, or returns the
* default (jpg, jpeg, gif, and png)
*
* @return array - icon file types
*/
private function _get_icon_filetypes() {
$icon_file_types = get_option('icon_file_types');
if ($icon_file_types == false || empty($icon_file_types)) {
$icon_file_types = 'jpg, jpeg, gif, png';
update_option('icon_file_types', $icon_file_types);
} elseif (is_array($icon_file_types)) {
$icon_file_types = implode(',', $icon_file_types);
update_option('icon_file_types', $icon_file_types);
}
return $icon_file_types;
}
/**
* Displays the "show attachments" checkbox if wam_list_on_posts is set to some
*/
public function post_form() {
if (get_option('wam_list_on_posts') == 'some') {
global $post;
$checked = (get_post_meta($post->ID, '_wam_show_attachments', true) == 'true')? ' checked="checked"':'';
echo "
";
}
}
/**
* Adds or removes the show_attachments meta from the post
*
* @param int $pid - Post ID
*/
public function handle_save_post($pid) {
if (isset($_POST['wam_show_attachments']) && strtolower($_POST['wam_show_attachments']) == 'true') {
add_post_meta($pid, '_wam_show_attachments', 'true', true);
} else {
delete_post_meta($pid, '_wam_show_attachments');
}
}
/**
* This is used to set some default values when the plugin is activated.
*/
public function on_activate() {
if (get_option('wam_list_on_posts') === false) {
update_option('wam_list_on_posts', 'all');
}
if (get_option('wam_show_file_icons') === false) {
update_option('wam_show_file_icons', 'true');
}
if (get_option('wam_dont_show_on_excerpts') === false) {
update_option('wam_dont_show_on_excerpts', 'true');
}
update_option('wam_list_on_posts', 'all');
update_option('wam_show_file_icons', 'true');
update_option('wam_dont_show_on_excerpts', 'true');
}
}
// Instantiate our class
$wpAttachmentManager = wpAttachmentManager::getInstance();