Skip to content

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
  • Loading branch information
zerosonesfun authored Sep 21, 2023
1 parent d9ea744 commit 0205e0a
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 0 deletions.
24 changes: 24 additions & 0 deletions includes/wpbm-admin-page.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
// Function to create the admin page
function wpbm_bookmarklet_page() {
$plugin_url = plugins_url('', __FILE__);
$site_url = site_url();

$bookmarklet_code = "
javascript:(function(){var t=encodeURIComponent(document.title),d=document.querySelector('meta[name=\"description\"]'),u=encodeURIComponent(window.location.href),i=document.querySelector('meta[property=\"og:image\"]');d=d?encodeURIComponent(d.content):'No description available';i=i?encodeURIComponent(i.content):'No image available';var r=\"$site_url/wp-admin/post-new.php?post_type=post\";r+=\"&title=\"+t+\"&content=\"+d+\"&wpbm_url=\"+u+\"&wpbm_og_image=\"+i;window.location.href=r;})();
";

?>
<div class="wrap">
<h1><?php echo esc_html__('Bookmarklet', 'wpbm'); ?></h1>
<p><?php echo esc_html__('Copy the code below and paste it as a new browser bookmark\'s URL.', 'wpbm'); ?></p>
<textarea rows="5" cols="50" id="bookmarklet-code"><?php echo esc_textarea($bookmarklet_code); ?></textarea>
</div>
<?php
}

// Add the admin menu page
function wpbm_add_admin_menu() {
add_management_page('Bookmarklet', 'Bookmarklet', 'manage_options', 'wpbm-bookmarklet', 'wpbm_bookmarklet_page');
}
add_action('admin_menu', 'wpbm_add_admin_menu');
53 changes: 53 additions & 0 deletions includes/wpbm-handle-bookmarklet.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
<?php
function wpbm_set_featured_image($post_id, $og_image_url) {
// Need to require these files
if (!function_exists('media_handle_upload')) {
require_once(ABSPATH . "wp-admin" . '/includes/image.php');
require_once(ABSPATH . "wp-admin" . '/includes/file.php');
require_once(ABSPATH . "wp-admin" . '/includes/media.php');
}

// Upload file to WordPress library
$tmp = download_url($og_image_url);
$file_array = array(
'name' => basename($og_image_url),
'tmp_name' => $tmp
);

// Check for download errors
if (is_wp_error($tmp)) {
@unlink($file_array['tmp_name']);
return $tmp;
}

// Handle the media upload
$id = media_handle_sideload($file_array, $post_id);

// Check for handle sideload errors.
if (is_wp_error($id)) {
@unlink($file_array['tmp_name']);
return $id;
}

// Set the featured image for the post
set_post_thumbnail($post_id, $id);
}

function wpbm_handle_bookmarklet() {
if (!is_user_logged_in()) {
wp_redirect(wp_login_url(get_permalink()));
exit;
}

$post_id = wp_insert_post();

// Set the featured image for the new post
wpbm_set_featured_image($post_id, $og_image);

// Redirect to the new post page
wp_redirect(admin_url('post.php?post=' . $post_id . '&action=edit'));
exit;
}

add_action('wp_ajax_wpbm_handle_bookmarklet', 'wpbm_handle_bookmarklet');
add_action('wp_ajax_nopriv_wpbm_handle_bookmarklet', 'wpbm_handle_bookmarklet');
24 changes: 24 additions & 0 deletions includes/wpbm-post.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?php
function wpbm_modify_post_title($title) {
if (isset($_GET['title'])) {
$title = sanitize_text_field($_GET['title']);
}

return $title;
}
add_filter('default_title', 'wpbm_modify_post_title');

function wpbm_modify_post_content($content) {
if (isset($_GET['content']) && isset($_GET['wpbm_url']) && isset($_GET['wpbm_og_image'])) {
$description = sanitize_text_field($_GET['content']);
$url = filter_var($_GET['wpbm_url'], FILTER_SANITIZE_URL);
$og_image = filter_var($_GET['wpbm_og_image'], FILTER_SANITIZE_URL);

// Create the post content
$content = '<img src="' . $og_image . '" />';
$content .= '<p>' . $description . '</p>';
$content .= '<p><a href="' . $url . '">Read more</a></p>';
}
return $content;
}
add_filter('default_content', 'wpbm_modify_post_content');
14 changes: 14 additions & 0 deletions wp-bookmark.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/*
Plugin Name: WP Bookmark
Description: A bookmarking plugin for WordPress.
Version: 0.1
Author: Billy Wilcosky
Author URI: https://wilcosky.com
License: GPL-2.0+
*/

// Include necessary files
include(plugin_dir_path(__FILE__) . 'includes/wpbm-admin-page.php');
include(plugin_dir_path(__FILE__) . 'includes/wpbm-post.php');
include(plugin_dir_path(__FILE__) . 'includes/wpbm-handle-bookmarklet.php');

0 comments on commit 0205e0a

Please sign in to comment.