__( 'Creating screenshot...', 'admin-screenshots' ), 'success' => __( 'Success!', 'admin-screenshots' ), /* translators: %s is the screenshot ID number. */ 'screenshotSaved' => __( 'Screenshot %s was saved in your Media Library and can be accessed directly at:', 'admin-screenshots' ), 'close' => __( 'CLOSE', 'admin-screenshots' ), 'closeAndCopy' => __( 'CLOSE & COPY URL', 'admin-screenshots' ), 'screenshotPage' => __( 'SCREENSHOT THIS PAGE', 'admin-screenshots' ), 'saveFailed' => __( 'Screenshot could not be saved.', 'admin-screenshots' ), 'nonce' => wp_create_nonce( 'admin_screenshots_nonce' ), ) ); } } add_action('admin_enqueue_scripts', 'admin_screenshots_styles' ); /* --- ADD .CSS TO FRONT AREA -------------------------------------------------------------- */ if (!function_exists('admin_screenshots_front_styles')) { function admin_screenshots_front_styles() { wp_register_style('adminScreenshotsFrontStyle', plugins_url('/assets/css/admin-screenshots-front.css', __FILE__),'', '1.0.5' ); wp_enqueue_style('adminScreenshotsFrontStyle'); } } add_action('wp_enqueue_scripts', 'admin_screenshots_front_styles' ); /* --- ADD THE SCREENSHOT BUTTON TO THE TOOLBAR -------------------------------------------------------------- */ if (!function_exists('admin_screenshots_toolbar_button')) { function admin_screenshots_toolbar_button() { global $wp_admin_bar; $args = array( 'id' => 'admin-screenshots-button', 'title' => __( 'SCREENSHOT THIS PAGE', 'admin-screenshots' ), 'href' => '#' ); $wp_admin_bar->add_node($args); } } add_action('admin_bar_menu', 'admin_screenshots_toolbar_button', 999); /* --- THE FUNCTION THAT CREATES A CANVAS AND SAVES IT AS AN IMAGE -------------------------------------------------------------- */ if (!function_exists('admin_screenshots_save_canvas')) { function admin_screenshots_save_canvas() { check_ajax_referer( 'admin_screenshots_nonce', 'nonce' ); if ( ! function_exists( 'request_filesystem_credentials' ) ) { require_once ABSPATH . 'wp-admin/includes/file.php'; } $credentials = request_filesystem_credentials( site_url() ); if ( ! WP_Filesystem( $credentials ) ) { wp_die( esc_html(__( 'Could not initialize filesystem API', 'admin-screenshots' ) ) ); } global $wp_filesystem; if ( isset( $_POST['image'] ) ) { $upload_dir = wp_upload_dir(); // Array $upload_dir_path = $upload_dir['path']; // /serverstuff/app/public/wp-content/uploads $upload_dir_base = $upload_dir['basedir']; // /serverstuff/app/public/wp-content/uploads $upload_path = $upload_dir['basedir'] . '/admin-screenshots'; // /serverstuff/app/public/wp-content/uploads/admin-screenshots $upload_dir_url = $upload_dir['baseurl'] . '/admin-screenshots'; // http://mysite.local/wp-content/uploads if (!file_exists($upload_path)) { wp_mkdir_p($upload_path); } $image_data = sanitize_text_field(wp_unslash($_POST['image'])); $timestamp = time(); $img = $timestamp . '.png'; $file = $upload_path . '/' . $img; $fullpath = $upload_dir['url'] . '/admin-screenshots/' . $img; // $success = file_put_contents( $file, base64_decode( str_replace( 'data:image/png;base64,', '', $image_data ) ) ); // Previous line is old, outdated version. Never version follows. $success = $wp_filesystem->put_contents( $file, base64_decode( str_replace( 'data:image/png;base64,', '', $image_data ) ), FS_CHMOD_FILE ); if ( $success ) { $wp_filetype = wp_check_filetype( $img, null ); $attachment = array( 'post_mime_type' => $wp_filetype['type'], 'post_title' => 'Screenshot '.$timestamp.' (generated by Admin Screenshots)', 'post_content' => '', 'post_status' => 'inherit' ); $attachment_id = wp_insert_attachment( $attachment, $file ); require_once( ABSPATH . 'wp-admin/includes/image.php' ); $attachment_data = wp_generate_attachment_metadata( $attachment_id, $file ); wp_update_attachment_metadata( $attachment_id, $attachment_data ); echo esc_attr($upload_dir_url) . ',' . esc_attr($timestamp); // Sending this to the JS function in screenshotThis() } else { esc_html_e('Failed to save image.','admin-screenshots'); } } wp_die(); } } add_action( 'wp_ajax_save_canvas', 'admin_screenshots_save_canvas' ); add_action( 'wp_ajax_nopriv_save_canvas', 'admin_screenshots_save_canvas' );