id = $id; // Id $this->name = $name; // Name $this->label = isset( $atts['label'] ) && !empty( $atts['label'] ) ? $atts['label'] : ''; // Label $this->default = isset( $atts['default'] ) && !empty( $atts['default'] ) ? $atts['default'] : ''; // Default $this->value = $value; // Value } /** * Output admin form field * * @since 1.0.0 * @return string Field output */ public function field() { $html = ''; if ( empty( $this->name ) ) { return $html; } // Wrapper around field to avoid changing other uploaders values if more upload fields in same widget $html .= '
'; if ( $this->label ) { $html .= ''; $html .= '
'; } // If value NULL (only first time before save) set default value if passed $value = NULL === $this->value && '' !== $this->default ? $this->default : $this->value; // Hidden input to store attachment id $html .= ' 0 */ $img_preview = ''; if ( ! empty( $this->value ) && intval( $this->value ) > 0 ) { // Attachment id is saved as value $img_preview = wp_get_attachment_image( $this->value, 'medium' ); } elseif( ! empty( $this->value ) ) { // Url is saved as value $img_preview = ''; } elseif ( NULL === $this->value ) { // Value is empty but only first time before save if ( !empty( $this->default ) ) { $img_preview = ''; } } // Image preview $html .= '

' . $img_preview . '

'; // Select button $html .= ''; // Clear button $html .= ''; // Default value if ( !empty( $this->default ) ) { $html .= ''; } // Close field wrapper $html .= '
'; return $html; } /** * Add script * * @since 1.0.0 */ public static function admin_scripts( $hook ) { if ( 'widgets.php' !== $hook ) { return; } // Default scripts wp_enqueue_media(); // Uploader picker init wp_enqueue_script( 'predic-widget-uploader-field', PREDIC_WIDGET_ASSETS_URL . '/js/fields/uploader-field.js', array( 'jquery' ), self::$version, true ); wp_localize_script( 'predic-widget-uploader-field', 'predic_widget_uploader_field', array( 'uploader_title' => esc_html__( 'Upload or select image', 'business-consultant-finder' ), 'button_text' => esc_html__( 'Select image', 'business-consultant-finder' ) )); } }