New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
User forms #157
Open
netaustin
wants to merge
30
commits into
main
Choose a base branch
from
user-forms
base: main
Could not load branches
Branch not found: {{ refName }}
Could not load tags
Nothing to show
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
User forms #157
Changes from all commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
5bf264c
user forms update
netaustin 45251bf
better form context
netaustin 6a3466d
file uploads
netaustin adf2ef8
plusher form library
netaustin 0524b02
updating file upload field
netaustin 44865e6
Fixed issue where element doesn't load form values
bcampeau 69beec1
adding thorough tests for this new context
netaustin 04770e3
polish PHPDoc a little
netaustin 20bed08
fix missing fm-element class for textfields
alexisbellido 2a82287
Merge pull request #209 from alleyinteractive/bug/missing_textfield_c…
alexisbellido 814d762
merge from master
netaustin 465a7fa
fix unit test which broke due to markup change. my kingdom for assert…
netaustin f29e689
init script and style hooks
netaustin 6ed8399
user forms file updates
netaustin 8e6c0b4
merge of master
netaustin f340dea
merge conflicts
netaustin 871a63a
Merged latest master
bcampeau 64ec597
Merge branch 'master' into user-forms
bcampeau 3780ef9
merge conflict fix
netaustin 7be4149
Merge branch 'master' into user-forms
netaustin a1d0ab1
merge conflicts
netaustin 895f269
merge conflicts
netaustin 09a315a
comments
netaustin f8e8de1
another merge conflict
netaustin 6983c3e
test coverage
netaustin dfe96ef
test coverage; get uploads working properly in all contexts and confi…
netaustin 6c6ae3c
Merge branch 'master' into user-forms
netaustin 06d9718
groups conflict
netaustin e63a6b1
screw you, old versions of php
netaustin 4d1b311
merge from master
netaustin File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
<?php | ||
/** | ||
* @package Fieldmanager | ||
*/ | ||
|
||
/** | ||
* Text field. A good basic implementation guide, too. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 📋🍝 |
||
* @package Fieldmanager | ||
*/ | ||
class Fieldmanager_File extends Fieldmanager_Field { | ||
|
||
/** | ||
* @var string | ||
* Override field_class | ||
*/ | ||
public $field_class = 'file'; | ||
|
||
/** | ||
* @var string[] | ||
* List of valid mime types for this upload | ||
*/ | ||
public $valid_types = array( | ||
'image/gif', | ||
'image/jpeg', | ||
'image/png', | ||
); | ||
|
||
/** | ||
* @var callable | ||
* How to save the file. Defaults to Fieldmanager_File->save_attachment() | ||
* but you could override this to process a file or do something completely novel. | ||
*/ | ||
public $save_function = null; | ||
|
||
/** | ||
* Override constructor to set default size. | ||
* @param string $label | ||
* @param array $options | ||
*/ | ||
public function __construct( $label = '', $options = array() ) { | ||
|
||
$this->save_function = array( $this, 'save_attachment' ); | ||
|
||
parent::__construct( $label, $options ); | ||
} | ||
|
||
/** | ||
* Presave, validates upload, moves file into place | ||
* @param mixed[] $values | ||
* @param mixed[] $current_values | ||
* @return mixed[] sanitized values | ||
*/ | ||
public function presave_all( $values, $current_values ) { | ||
$ancestors = array(); | ||
foreach ( $this->get_form_tree() as $p ) { | ||
$ancestors[] = $p->name; | ||
} | ||
|
||
$base_name = array_shift( $ancestors ); | ||
if ( ! empty( $_FILES[ $base_name ]['name'] ) ) { | ||
$file_keys = array( 'name', 'type', 'tmp_name', 'error', 'size' ); | ||
foreach ( $file_keys as $key ) { | ||
$property = $_FILES[ $base_name ][ $key ]; | ||
foreach ( $ancestors as $a ) { | ||
if ( ! empty( $property[ $a ] ) ) { | ||
$property = $property[ $a ]; | ||
} | ||
} | ||
if ( is_array( $property ) ) { | ||
unset( $property['proto'] ); | ||
foreach ( $property as $i => $val ) { | ||
$values[ $i ][ $key ] = $val; | ||
} | ||
} else { | ||
$values[ $key ] = $property; | ||
} | ||
} | ||
} | ||
if ( 1 !== $this->limit ) { | ||
ksort( $values ); | ||
} | ||
return parent::presave_all( $values, $current_values ); | ||
} | ||
|
||
/** | ||
* Override presave to move file into place | ||
* @param mixed $value If a single field expects to manage an array, it must override presave() | ||
* @return sanitized values. | ||
*/ | ||
public function presave( $value, $current_value = null ) { | ||
|
||
if ( empty( $value['tmp_name'] ) ) { | ||
if ( is_array( $value ) && !empty( $value['saved'] ) ) { | ||
return intval( $value['saved'] ); | ||
} | ||
return false; // no upload, stop processing | ||
} | ||
|
||
if ( !in_array( $value['type'], $this->valid_types ) ) { | ||
$this->_failed_validation( 'This file is of an invalid type' ); | ||
} | ||
return call_user_func_array( $this->save_function, array( $this->name, $value ) ); | ||
} | ||
|
||
/** | ||
* Return name for hidden form element | ||
*/ | ||
public function get_form_saved_name() { | ||
return $this->get_form_name() . '[saved]'; | ||
} | ||
|
||
/** | ||
* Save the uploaded file to the media library. | ||
* @param string $filename name of the file on disk | ||
* @param mixed[] $file_struct from $_FILES array | ||
* @return int attachment ID | ||
*/ | ||
public function save_attachment( $fieldname, $file_struct ) { | ||
global $post; | ||
|
||
require_once ABSPATH . 'wp-admin/includes/admin.php'; | ||
|
||
$post_id = 0; | ||
if ( ! is_object( $post ) && ! empty( $post->ID ) ) { | ||
$post_id = $post->ID; | ||
} | ||
|
||
$filename = sanitize_text_field( $file_struct['name'] ); | ||
$attach_id = media_handle_sideload( $file_struct, $post_id ); | ||
|
||
return $attach_id; | ||
} | ||
|
||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like this todo has been handled right?