Skip to content

Instantly share code, notes, and snippets.

@nmedia82
Created January 31, 2022 13:15
Show Gist options
  • Save nmedia82/87f8179995887c2b50671b43750c29d1 to your computer and use it in GitHub Desktop.
Save nmedia82/87f8179995887c2b50671b43750c29d1 to your computer and use it in GitHub Desktop.
File Manager Link Create
add_action('wp_ajax_wpfm_create_link', function(){
// wp_send_json_success($_POST);
$current_user = wpfm_get_current_user();
if( ! $current_user ) {
$resp = array('status' => 'error',
'message' => __("User object not found", 'wpfm'));
wp_send_json($resp);
}
$group_ids = isset($_REQUEST['shortcode_groups']) ? explode(",", sanitize_text_field($_REQUEST['shortcode_groups']) ) : null;
if($group_ids){
// removing space
$group_ids = array_map('trim', $group_ids);
}
// wp_send_json($group_ids);
$wpfm_link_id = wpfm_create_post_file(
$current_user->ID,
sanitize_text_field($_REQUEST['link_name']),
$_REQUEST['links'],
sanitize_text_field($_REQUEST['parent_id']),
$group_ids);
update_post_meta($wpfm_link_id, 'wpfm_node_type', 'file');
update_post_meta($wpfm_link_id, 'wpfm_dir_path', '/var/www/devteam.com/wp1/wp-content/plugins/wp-file-manager/images/file-icon.png');
$wpfm_dir = new WPFM_File( $wpfm_link_id );
$resp = ['new_dir' => $wpfm_dir,
'dir_id'=>$wpfm_link_id,
'message'=>__("Link(s) created successfully", 'wpfm')];
wp_send_json_success($resp);
});
add_action('ffmwp_after_create_directory_button', function($wpfm_bp_group_id, $shortcode_groups){
?>
<button class="ffmwp-click-to-reveal-link" id="wpfm-create-link-btn">Add Link</button>
<div class="ffmwp-click-to-reveal-link-block" style="display:none">
<form id="ffmwp-create-link-form">
<input type="hidden" name="action" value="wpfm_create_link">
<input type="hidden" name="wpfm_bp_group_id" value="<?php echo esc_attr($wpfm_bp_group_id);?>">
<input type="hidden" name="shortcode_groups" value="<?php echo esc_attr($shortcode_groups);?>">
<div class="ffmwp-uploadarea-form-content">
<label class="ffmwp-inputs" for="wpfm-link"><?php _e( "Title", "ffmwp" ); ?></label>
<input type="text" id="wpfm-link" required name="link_name">
<label class="ffmwp-inputs"for="wpfm-links"><?php _e( "Links", "ffmwp" ); ?></label>
<textarea id="wpfm-links" name="links"></textarea>
<button class="ffmwp-uploadarea-btn" id="wpfm-dir-created-btn"><?php _e( "Create", "ffmwp" ); ?></button>
<button class="ffmwp-uploadarea-btn ffmwp-uploadarea-cancel-link-btn wpfm-cancle-btn"><?php _e( "Cancel", "ffmwp" ); ?></button>
</div>
</form>
</div>
<script type="text/javascript">
jQuery(document).on('click', '.ffmwp-click-to-reveal-link, .ffmwp-uploadarea-cancel-link-btn ', function(e) {
e.preventDefault();
jQuery(`.ffmwp-click-to-reveal-link-block`).toggle();
});
jQuery('#ffmwp-create-link-form').on('submit', function(e){
e.preventDefault();
var data = jQuery(this).serialize();
var wp_nonce_value = jQuery('#wpfm_ajax_nonce').val();
data += `&wpfm_ajax_nonce=${wp_nonce_value}&parent_id=${FFMWP.current_directory}`;
jQuery.post(ffmwp_vars.ajaxurl, data, function(resp) {
// console.log(resp);
if(resp.success) {
var {message} = resp.data;
FFMWP.alert(message, 'success');
// var updated_files = [new_dir, ...FFMWP.current_files];
// FFMWP.reload_current_dir(updated_files);
}else{
FFMWP.alert(resp.data, 'error');
// window.location.reload(false);
}
}, 'json');
})
</script>
<?php
}, 9, 2);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment