| 1915 | |
| 1916 | // These filters get run: |
| 1917 | // media_send_to_editor |
| 1918 | // And for images only: |
| 1919 | // image_send_to_editor |
| 1920 | // image_add_caption_shortcode (unless there is no caption, or if captions are disabled with disable_captions) |
| 1921 | // get_image_tag |
| 1922 | // get_image_tag_class |
| 1923 | // These callbacks are run by core against these filters: |
| 1924 | // add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 ); |
| 1925 | // add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 ); |
| 1926 | |
| 1927 | function do_i_need_php_to_send_images_to_the_editor() { |
| 1928 | if ( 10 === has_filter( 'media_send_to_editor', 'image_media_send_to_editor' ) ) |
| 1929 | remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10 ); |
| 1930 | else |
| 1931 | return true; |
| 1932 | |
| 1933 | $return = has_filter( 'media_send_to_editor' ); |
| 1934 | add_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 ); |
| 1935 | if ( $return ) |
| 1936 | return true; |
| 1937 | |
| 1938 | if ( 20 === has_filter( 'image_send_to_editor', 'image_add_caption' ) ) |
| 1939 | remove_filter( 'image_send_to_editor', 'image_add_caption', 20 ); |
| 1940 | else |
| 1941 | return true; |
| 1942 | |
| 1943 | $return = has_filter( 'image_send_to_editor' ); |
| 1944 | add_filter( 'image_send_to_editor', 'image_add_caption', 20, 8 ); |
| 1945 | if ( $return ) |
| 1946 | return true; |
| 1947 | |
| 1948 | foreach ( array( 'image_add_caption_shortcode', 'get_image_tag', 'get_image_tag_class' ) as $filter ) { |
| 1949 | if ( has_filter( $filter ) ) |
| 1950 | return true; |
| 1951 | } |
| 1952 | |
| 1953 | return false; |
| 1954 | } |
| 1955 | |
| 1956 | // Does media_send_to_editor have anything hooked into it except for image_media_send_to_editor? |
| 1957 | function do_i_need_php_to_send_non_images_to_the_editor() { |
| 1958 | $priority = has_filter( 'media_send_to_editor', 'image_media_send_to_editor' ); |
| 1959 | if ( false !== $priority ) |
| 1960 | remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', $priority ); |
| 1961 | |
| 1962 | $return = has_filter( 'media_send_to_editor' ); |
| 1963 | |
| 1964 | if ( false !== $priority ) |
| 1965 | add_filter( 'media_send_to_editor', 'image_media_send_to_editor', $priority ); |
| 1966 | |
| 1967 | return $return; |
| 1968 | } |
| 1969 | |
| 1970 | function wp_ajax_send_to_editor() { |
| 1971 | // @todo cap check like edit_posts or upload_files |
| 1972 | |
| 1973 | check_ajax_referer( 'media_send_to_editor' ); |
| 1974 | |
| 1975 | $attachment = stripslashes_deep( $_POST['attachment'] ); |
| 1976 | // keys must include 'id' |
| 1977 | // keys may include 'image-align', 'image-size', 'image-alt', 'url', |
| 1978 | // and 'caption' for images, 'title' for non-images |
| 1979 | |
| 1980 | $id = intval( $attachment['id'] ); |
| 1981 | |
| 1982 | if ( ! $post = get_post( $id ) ) |
| 1983 | wp_send_json_error(); |
| 1984 | |
| 1985 | if ( ! current_user_can( 'edit_post', $id ) ) |
| 1986 | wp_send_json_error(); |
| 1987 | |
| 1988 | if ( 'attachment' != $post->post_type ) |
| 1989 | wp_send_json_error(); |
| 1990 | |
| 1991 | $html = isset( $attachment['title'] ) ? $attachment['title'] : ''; |
| 1992 | if ( ! empty( $attachment['url'] ) ) { |
| 1993 | $rel = ''; |
| 1994 | if ( strpos($attachment['url'], 'attachment_id') || get_attachment_link( $id ) == $attachment['url'] ) |
| 1995 | $rel = ' rel="attachment wp-att-' . $id . '"'; |
| 1996 | $html = '<a href="' . esc_url( $attachment['url'] ) . '"' . $rel . '>' . $html . '</a>'; |
| 1997 | } |
| 1998 | |
| 1999 | remove_filter( 'media_send_to_editor', 'image_media_send_to_editor', 10, 3 ); |
| 2000 | |
| 2001 | if ( 'image' === substr( $post->post_mime_type, 0, 5 ) ) { |
| 2002 | $url = $attachment['url']; |
| 2003 | $align = isset( $attachment['image-align'] ) ? $attachment['image-align'] : 'none'; |
| 2004 | $size = isset( $attachment['image-size'] ) ? $attachment['image-size'] : 'medium'; |
| 2005 | $alt = isset( $attachment['image-alt'] ) ? $attachment['image-alt'] : ''; |
| 2006 | $caption = isset( $attachment['caption'] ) ? $attachment['caption'] : ''; |
| 2007 | $title = ''; // We no longer insert title tags into <img> tags, as they are redundant. |
| 2008 | $html = get_image_send_to_editor( $id, $caption, $title, $align, $url, (bool) $rel, $size, $alt ); |
| 2009 | } |
| 2010 | |
| 2011 | $html = apply_filters( 'media_send_to_editor', $html, $id, $attachment ); |
| 2012 | |
| 2013 | wp_send_json_success( $html ); |
| 2014 | } |
| 2015 | |
| 2016 | // Only needs to run for non-images inserted from Embed from URL |
| 2017 | // You must check disable_captions to see if captions are disabled, when handling images from JS |
| 2018 | // |
| 2019 | // Only needs to be run if the {$type}_send_to_editor_url hooks are used, |
| 2020 | // where $type is file, audio, or video |
| 2021 | |
| 2022 | function wp_ajax_send_to_editor_url() { |
| 2023 | check_ajax_referer( 'media_send_to_editor' ); |
| 2024 | |
| 2025 | if ( ! $src = stripslashes( $_POST['src'] ) ) |
| 2026 | wp_send_json_error(); |
| 2027 | |
| 2028 | if ( ! strpos( $src, '://' ) ) |
| 2029 | $src = 'http://' . $src; |
| 2030 | |
| 2031 | if ( ! $src = esc_url_raw( $src ) ) |
| 2032 | wp_send_json_error(); |
| 2033 | |
| 2034 | if ( ! $title = trim( stripslashes( $_POST['title'] ) ) ) |
| 2035 | $title = wp_basename( $src ); |
| 2036 | |
| 2037 | $html = ''; |
| 2038 | if ( $title ) |
| 2039 | $html = '<a href="' . esc_url( $src ) . '">' . $title . '</a>'; |
| 2040 | |
| 2041 | // Figure out what filter to run: |
| 2042 | $type = 'file'; |
| 2043 | if ( ( $ext = preg_replace( '/^.+?\.([^.]+)$/', '$1', $src ) ) && ( $ext_type = wp_ext2type( $ext ) ) |
| 2044 | && ( 'audio' == $ext_type || 'video' == $ext_type ) ) |
| 2045 | $type = $ext_type; |
| 2046 | |
| 2047 | $html = apply_filters( $type . '_send_to_editor_url', $html, $src, $title ); |
| 2048 | |
| 2049 | wp_send_json_success( $html ); |
| 2050 | } |