/**
 * Remove WP List Table row actions in the Media Library List Table
 * if the attachment is protected and the user is not permitted to access it
 *
 * @since 0.7
 *
 * @uses mgjp_mv_check_user_permitted() returns true if user is permitted to access
 *                                            specified attachment
 * @param $actions array Array of row actions available for specific attachment
 * @param $post object WP_Post object of currently rendering attachment
 * @return array Return row actions untouched if user permitted to access attachment
 * @return array Empty array if no access permitted
 */
function mgjp_mv_modify_media_library_row_actions($actions, $post)
{
    // check if current user is permitted to access the post
    if (!mgjp_mv_check_user_permitted($post->ID)) {
        return array(esc_html__('You do not have permission to access this attachment', 'media-vault'));
    }
    return $actions;
}
/**
 * Add shortcode handler to print download link(s) for files
 * in the wp uploads folder
 *
 * @since 0.5
 *
 * @param $atts array Array of parameters passed to the shortcode through the editor
 *                    acceptable parameters include:
 *                    'ids' int || string list of attachment ids (int), comma separated
 *                    'sizes' string list of desired file sizes per id if attachment type is image, comma separated
 *                    'thumb' bool true if thumb is desired in link list
 *                    'thumb_size' string desired size the thumb should display as
 */
function mgjp_mv_download_links_list_shortcode_handler($atts)
{
    extract(shortcode_atts(array('ids' => null, 'sizes' => 'full', 'thumb' => false, 'thumb_size' => 'thumbnail'), $atts));
    if (empty($ids)) {
        return;
    }
    $ids = explode(',', str_replace(' ', '', $ids));
    $sizes = explode(',', str_replace(' ', '', $sizes));
    foreach ($ids as $key => $id) {
        if (!absint($id) || !mgjp_mv_check_user_permitted($id)) {
            continue;
        }
        $size = $sizes[0];
        if (isset($sizes[$key])) {
            $size = $sizes[$key];
        }
        $file = mgjp_mv_get_attachment_download_url($id, $size);
        if (is_wp_error($file)) {
            continue;
        }
        $files[] = array($id, $file);
    }
    if (!isset($files)) {
        return;
    }
    ob_start();
    if (!isset($files[1])) {
        ?>

    <a href="<?php 
        echo esc_attr($files[0][1]);
        ?>
" download="<?php 
        echo esc_attr(basename($files[0][0]));
        ?>
" title="<?php 
        echo esc_html__('Click to Download:', 'media-vault'), ' ', get_the_title($files[0][0]);
        ?>
">
      <?php 
        echo get_the_title($files[0][0]);
        ?>
    </a>

  <?php 
    } else {
        ?>

    <ul>
      <?php 
        foreach ($files as $file) {
            ?>

        <li>
          <a href="<?php 
            echo esc_attr($file[1]);
            ?>
" download="<?php 
            echo esc_attr(basename($file[1]));
            ?>
" title="<?php 
            echo esc_html__('Click to Download:', 'media-vault'), ' ', get_the_title($file[0]);
            ?>
">
            <?php 
            echo get_the_title($file[0]);
            ?>
          </a>
        </li>

      <?php 
        }
        ?>
    </ul>

  <?php 
    }
    return ob_get_clean();
}
/**
 * Replace requested image with a Media Vault place-holder
 * if the user is not permitted to view them
 *
 * @since 0.8
 *
 * @param $img mixed false based on wp-includes/media.php or array if other filter has affected it
 * @param $attachment_id int ID of the attachment whose image is being requested
 * @param $size string name-id of the dimensions of the requested image
 * @return mixed return the $url if the image is not protected
 * @return array [0] string URL to the Media Vault replacement image of the requested size
 *               [1] string width of the Media Vault replacement image
 *               [2] string height of the Media Vault replacement image
 *               [3] bool whether the url is for a resized image or not
 */
function mgjp_mv_replace_protected_image($img, $attachment_id, $size)
{
    $ir = get_option('mgjp_mv_ir');
    if (!isset($ir['is_on']) || !$ir['is_on']) {
        return $img;
    }
    $upload_dir = wp_upload_dir();
    if (isset($img[0]) && 0 !== strpos(ltrim($img[0], $upload_dir['baseurl']), mgjp_mv_upload_dir('/', true))) {
        return $img;
    }
    if (mgjp_mv_check_user_permitted($attachment_id)) {
        return $img;
    }
    if (isset($ir['id']) && !mgjp_mv_is_protected($ir['id'])) {
        remove_filter('image_downsize', 'mgjp_mv_replace_protected_image', 999, 3);
        $placeholder = wp_get_attachment_image_src($ir['id'], $size);
        add_filter('image_downsize', 'mgjp_mv_replace_protected_image', 999, 3);
        return $placeholder;
    } else {
        list($width, $height) = image_constrain_size_for_editor(1024, 1024, $size);
        return array(plugins_url('imgs/media-vault-ir.jpg', __FILE__), $width, $height, false);
    }
}