'alphagf_settings_style', 'src' => ALPHAGF_ASSETS . '/css/alphagf_addons_admin.css', 'version' => $this->_version, 'enqueue' => array( array( 'admin_page' => array('form_settings'), 'tab' => 'notification' ) ) ) ); return array_merge(parent::styles(), $styles); } /** * Return the scripts which should be enqueued. * * @return array */ public function scripts() { $scripts = array( array( 'handle' => 'alphagf_attachment', 'src' => $this->get_base_url() . '/js/alphagf_attachment.js', 'version' => $this->_version, 'deps' => array('jquery'), 'callback' => array($this, 'enqueue_media_library'), 'enqueue' => array( array( 'admin_page' => array('form_settings'), 'tab' => 'notification', ), ) ) ); return array_merge(parent::scripts(), $scripts); } /** * The attachment picker relies on wp.media, which isn't guaranteed to be loaded on this screen. */ public function enqueue_media_library() { wp_enqueue_media(); } public function send_attachment_with_notification($notification, $form, $lead) { $attachment_ids = $this->parse_attachment_ids(rgar($notification, 'alphagf_attachment_ids')); $wp_upload_dir = wp_upload_dir(); foreach ($attachment_ids as $attachment_id) { // Prefer the real file path; fall back to mapping the URL for setups that filter the attached file. $path = get_attached_file($attachment_id); if (! $path || ! file_exists($path)) { $attachment = wp_get_attachment_url($attachment_id); $path = $attachment ? str_replace($wp_upload_dir['baseurl'], $wp_upload_dir['basedir'], $attachment) : ''; } $path = apply_filters('alphagf_notification_attachment_path', $path, $attachment_id, $form, $lead); if ($path) { $notification['attachments'] = (array) rgar($notification, 'attachments'); $notification['attachments'][] = $path; } } return $notification; } /** * Turn a comma separated list of attachment IDs into a clean array of positive integers. */ private function parse_attachment_ids($raw) { return array_values(array_filter(array_map('absint', explode(',', (string) $raw)))); } public function add_attachment_button($fields, $notification, $form) { $file_ids = rgpost('alphagf_attachment_ids'); if (! $file_ids) { $attachment_ids = $this->parse_attachment_ids(rgar($notification, 'alphagf_attachment_ids')); } else { $attachment_ids = $this->parse_attachment_ids(sanitize_text_field(wp_unslash($file_ids))); } $attachments = ''; $attachments .= '
' . esc_html__('Add Attachment', 'alpha-addons-for-gravity-forms') . '
'; $fields[] = array( 'title' => esc_html__('Attachments', 'alpha-addons-for-gravity-forms'), 'fields' => array( array( 'name' => 'alphagf_attachments', 'type' => 'html', 'html' => $attachments ) ), ); return $fields; } public function save_attachment_ids($notification, $form) { $notification['alphagf_attachment_ids'] = implode(',', $this->parse_attachment_ids(sanitize_text_field(wp_unslash((string) rgpost('alphagf_attachment_ids'))))); return $notification; } public function get_attachment_meta($attachment_id) { $attachment = get_post($attachment_id); if (! $attachment || 'attachment' !== $attachment->post_type) { return null; } $image = wp_get_attachment_image_src($attachment_id, 'medium', true); $image = !empty($image) ? $image[0] : null; if (is_null($image)) { $image = wp_mime_type_icon($attachment->post_mime_type); } return (object) apply_filters('alphagf_notification_attachment_meta', array( 'id' => $attachment_id, 'mime_file' => $image, 'mime' => $attachment->post_mime_type, 'title' => $attachment->post_title ), $attachment_id, $attachment); } }