function hm_get_post_image($post = null, $w = 0, $h = 0, $crop = false, $id = null, $default = null) { if ($post === null) { global $post; } // stop images with no post_id slipping in if ($post->ID == 0 && !$id) { return; } $id = $id ? $id : hm_get_post_image_id($post); if ($id) { return wpthumb(get_attached_file($id), $w, $h, $crop, true, wpthumb_wm_get_options($id)); } $att_id = hm_get_post_attached_image_id($post); if ($att_id) { return wpthumb(get_attached_file($att_id), $w, $h, $crop, true, wpthumb_wm_get_options($id)); } //if there is no id, then try search the content for an image if ($return = wpthumb(hm_get_post_internal_image($post), $w, $h, $crop, true, wpthumb_wm_get_options($id))) { return $return; } if ($return = hm_get_post_external_image($post)) { return $return; } if ($default) { $file = $default === 'default' ? dirname(__FILE__) . '/includes/image-unavailable.png' : $default; return wpthumb($file, $w, $h, $crop, true); } }
/** * add_extra_media_buttons function. * * Adds the "Use as Post Thumbnail" button to the add media thickbox. * * @param array $form_fields * @param object $media * @return array $form_fields */ function hm_add_extra_media_buttons($form_fields, $media) { $buttons = get_option('custom_media_buttons'); if (!empty($_GET['button'])) { $button_id = $_GET['button']; } else { preg_match('/button=([A-z0-9_][^&]*)/', $_SERVER['HTTP_REFERER'], $matches); if (isset($matches[1])) { $button_id = $matches[1]; } } if (!isset($button_id) || !$button_id) { return $form_fields; } if (isset($button_id) && ($button = $buttons[$button_id])) { $crop = $button['crop'] == true ? 1 : 0; $attach_thumb_url = wp_get_attachment_image_src($media->ID, "width={$button['width']}&height={$button['height']}&crop={$crop}"); $onclick = "var win = window.dialogArguments || opener || parent || top;"; if ($button['type'] == 'file') { $onclick .= "win.insert_custom_file( '" . $button_id . "', " . $media->ID . ", '" . wp_get_attachment_url() . "', '" . $button['multiple'] . "', '" . $media->post_title . "' );"; $onclick .= "jQuery(this).replaceWith('<span style=\\'color: #07AA00; font-weight:bold;\\'>File Added</span>');"; } elseif ($button['insert_into_post']) { $onclick .= "win.insert_custom_image( '" . $button_id . "', " . $media->ID . ", '" . $attach_thumb_url[0] . "' );"; $onclick .= "jQuery(this).replaceWith('<span style=\\'color: #07AA00; font-weight:bold;\\'>Image Inserted</span>');"; } else { $onclick .= "win.save_custom_image( '" . $button_id . "', " . $media->ID . ", '" . $attach_thumb_url[0] . "', '" . $button['multiple'] . "' );"; $onclick .= "jQuery(this).replaceWith('<span style=\\'color: #07AA00; font-weight:bold;\\'>Image Added</span>');"; } $onclick .= "return false;"; $buttons_html = '<a class="button-primary" onclick="' . $onclick . '" href="">' . esc_attr($button['button_text']) . '</a>'; } if (!$button['hide_other_options']) { $send = '<input type="submit" class="button" name="send[' . $media->ID . ']" value="' . __('Insert into Post') . '" />'; } else { ?> <style type="text/css"> .slidetoggle tr.post_title, .slidetoggle tr.image_alt, .slidetoggle tr.post_excerpt, .slidetoggle tr.post_content, .slidetoggle tr.url, .slidetoggle tr.align, .slidetoggle tr.image-size, .media-upload-form p.savebutton.ml-submit { display: none !important; } </style> <?php } if (!empty($send)) { $send = '<input type="submit" class="button" name="send[' . $media->ID . ']" value="' . __('Insert into Post') . '" />'; } else { $send = false; } if (current_user_can('delete_post', $media->ID)) { if (!EMPTY_TRASH_DAYS) { $delete = '<a href=""' . wp_nonce_url('post.php?action=delete&post=' . $media->ID, 'delete-post_' . $media->ID) . '" id="del[' . $media->ID . ']" class="delete">' . __('Delete Permanently') . '</a>'; } elseif (!MEDIA_TRASH) { $delete = '<a href="#" class="del-link" onclick="document.getElementById( \'del_attachment_' . $media->ID . '\' ).style.display=\'block\'; return false;">' . __('Delete') . '</a> <div id="del_attachment_' . $media->ID . '" class="del-attachment" style="display:none;">' . sprintf(__('You are about to delete <strong>%s</strong>.'), $media->post_title) . ' <a href="' . wp_nonce_url('post.php?action=delete&post=' . $media->ID, 'delete-post_' . $media->ID) . '" id="del[' . $media->ID . ']" class="button">' . __('Continue') . '</a> <a href="#" class="button" onclick="this.parentNode.style.display=\'none\';return false;">' . __('Cancel') . '</a></div>'; } else { $delete = '<a href="' . wp_nonce_url('post.php?action=trash&post=' . $media->ID, 'trash-post_' . $media->ID) . '" id="del[' . $media->ID . ']" class="delete">' . __('Move to Trash') . '</a> <a href="' . wp_nonce_url('post.php?action=untrash&post=' . $media->ID, 'untrash-post_' . $media->ID) . '" id="undo[' . $media->ID . ']" class="undo hidden">' . __('Undo') . '</a>'; } } else { $delete = ''; } $thumbnail = ''; if (isset($type) && 'image' == $type && current_theme_supports('post-thumbnails') && hm_get_post_image_id($_GET['post_id']) != $media->ID) { $thumbnail = "<a class='wp-post-thumbnail' href='#' onclick='WPSetAsThumbnail(\"{$media->ID}\");return false;'>" . esc_html__("Use as thumbnail") . "</a>"; } // Create the buttons array $form_fields['buttons'] = array('tr' => "\t\t<tr class='submit'><td></td><td class='savesend'>{$send} {$thumbnail} {$buttons_html} {$delete}</td></tr>\n"); return $form_fields; }