-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Created PageBuilder and EditorShortcodes coders in Shortcodes extension If PageBuilder is disabled, shortcodes attributes decode works Fixes ThemeFuse/Unyson#469
- Loading branch information
moldcraft
committed
Jul 10, 2015
1 parent
185ecb6
commit 3c8461e
Showing
9 changed files
with
307 additions
and
6 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
<?php if (!defined('FW')) die('Forbidden'); | ||
|
||
/** | ||
* @param array $attributes Encoded attributes | ||
* @param $shortcode_tag 'button', 'section', etc. | ||
* @param $post_id | ||
* @return array|WP_Error | ||
* @since 1.3.0 | ||
*/ | ||
function fw_ext_shortcodes_decode_attr(array $attributes, $shortcode_tag, $post_id) { | ||
/** | ||
* @var FW_Extension_Shortcodes $shortcodes_ext | ||
*/ | ||
$shortcodes_ext = fw_ext('shortcodes'); | ||
|
||
foreach ($shortcodes_ext->get_attr_coder() as $coder) { | ||
if ($coder->can_decode($attributes, $shortcode_tag, $post_id)) { | ||
return $coder->decode($attributes, $shortcode_tag, $post_id); | ||
} | ||
} | ||
|
||
return new WP_Error('coder_not_found', 'No decoder found'); | ||
} |
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
99 changes: 99 additions & 0 deletions
99
includes/coder/class-fw-ext-shortcodes-attr-coder-json.php
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,99 @@ | ||
<?php if (!defined('FW')) die('Forbidden'); | ||
|
||
class FW_Ext_Shortcodes_Attr_Coder_JSON implements FW_Ext_Shortcodes_Attr_Coder { | ||
/** | ||
* @return string | ||
*/ | ||
public function get_id() { | ||
return 'json'; | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return array|WP_Error | ||
*/ | ||
public function encode(array $attributes, $shortcode_tag, $post_id) { | ||
$encoded = array(); | ||
$array_keys = array(); // remember which keys contains json encoded arrays | ||
|
||
foreach ($attributes as $key => $value) { | ||
// the WordPress shortcode parser doesn't work when | ||
// using attributes with dashes | ||
$key = str_replace('-', '_', $key); | ||
|
||
if (is_array($value)) { | ||
$value = json_encode($value); | ||
$array_keys[$key] = $key; | ||
} | ||
|
||
$encoded[$key] = $this->encode_value($value); | ||
} | ||
|
||
if (!empty($array_keys)) { | ||
$encoded['_array_keys'] = $this->encode_value(json_encode($array_keys)); | ||
} | ||
|
||
$encoded['_made_with_builder'] = 'true'; | ||
|
||
return $encoded; | ||
} | ||
|
||
private function encode_value($value) { | ||
/** | ||
* Replace '[' and ']' to fix http://bit.ly/1HoHVhl | ||
* Replace new lines to fix http://bit.ly/1J887Om | ||
* | ||
* http://www.degraeve.com/reference/specialcharacters.php | ||
*/ | ||
return str_replace( | ||
array('[', ']', "\r\n"), | ||
array('[', ']', '
'), | ||
htmlentities($value, ENT_QUOTES, 'UTF-8') | ||
); | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return array|WP_Error | ||
*/ | ||
public function decode(array $attributes, $shortcode_tag, $post_id) { | ||
if (!$this->can_decode($attributes, $shortcode_tag, $post_id)) { | ||
return $attributes; | ||
} | ||
|
||
unset($attributes['_made_with_builder']); | ||
|
||
$array_keys = array(); | ||
if (isset($attributes['_array_keys'])) { | ||
$array_keys = json_decode($this->decode_value($attributes['_array_keys']), true); | ||
unset($attributes['_array_keys']); | ||
} | ||
|
||
$decoded = array(); | ||
foreach ($attributes as $key => $value) { | ||
$decoded[$key] = isset($array_keys[$key]) | ||
? json_decode($this->decode_value($value), true) | ||
: $this->decode_value($value); | ||
} | ||
|
||
return $decoded; | ||
} | ||
|
||
private function decode_value($encoded_value) { | ||
return html_entity_decode($encoded_value, ENT_QUOTES, 'UTF-8'); | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return bool | ||
*/ | ||
public function can_decode(array $attributes, $shortcode_tag, $post_id) { | ||
return isset($attributes['_made_with_builder']); // https://github.com/ThemeFuse/Unyson/issues/469 | ||
} | ||
} |
67 changes: 67 additions & 0 deletions
67
includes/coder/class-fw-ext-shortcodes-attr-coder-post-meta.php
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,67 @@ | ||
<?php if (!defined('FW')) die('Forbidden'); | ||
|
||
class FW_Ext_Shortcodes_Attr_Coder_Post_Meta implements FW_Ext_Shortcodes_Attr_Coder { | ||
private $meta_key = 'fw-shortcode-settings'; | ||
private $meta_key_defaults = 'fw-shortcode-default-values'; | ||
|
||
/** | ||
* @return string | ||
*/ | ||
public function get_id() { | ||
return 'post_meta'; | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return array|WP_Error | ||
*/ | ||
public function encode(array $attributes, $shortcode_tag, $post_id) { | ||
/** | ||
* Has no portable encode functionality, | ||
* because it is depended on some $_POST data | ||
* and it is used when shortcodes are mixed with content (edit-shortcodes extension) | ||
* @see FW_Extension_Editor_Shortcodes::_action_admin_save_shortcodes | ||
*/ | ||
return new WP_Error('not_portable', 'Post Meta encode is not portable'); | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return array|WP_Error | ||
*/ | ||
public function decode(array $attributes, $shortcode_tag, $post_id) { | ||
if ( ! $this->can_decode($attributes, $shortcode_tag, $post_id) ) { | ||
return new WP_Error('cannot_decode', 'Cannot decode'); | ||
} | ||
|
||
$option_values = json_decode( get_post_meta( $post_id, $this->meta_key, true ), true ); | ||
$default_values = json_decode( get_post_meta( $post_id, $this->meta_key_defaults, true ), true ); | ||
|
||
$id = $attributes['fw_shortcode_id']; | ||
$attributes = $default_values[ $shortcode_tag ]; | ||
|
||
if ( is_array( $option_values ) and false === empty( $option_values ) ) { | ||
if ( preg_match( '/^[A-Za-z0-9]+$/', $id ) ) { | ||
if ( isset( $option_values[ $shortcode_tag ][ $id ] ) ) { | ||
$attributes = $option_values[ $shortcode_tag ][ $id ]; | ||
} | ||
} | ||
} | ||
|
||
return $attributes; | ||
} | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return bool | ||
*/ | ||
public function can_decode(array $attributes, $shortcode_tag, $post_id) { | ||
return isset($attributes['fw_shortcode_id']); // https://github.com/ThemeFuse/Unyson/issues/469 | ||
} | ||
} |
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,32 @@ | ||
<?php if (!defined('FW')) die('Forbidden'); | ||
|
||
interface FW_Ext_Shortcodes_Attr_Coder { | ||
/** | ||
* @return string | ||
*/ | ||
public function get_id(); | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return array|WP_Error | ||
*/ | ||
public function encode(array $attributes, $shortcode_tag, $post_id); | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return array|WP_Error | ||
*/ | ||
public function decode(array $attributes, $shortcode_tag, $post_id); | ||
|
||
/** | ||
* @param array $attributes | ||
* @param string $shortcode_tag | ||
* @param int $post_id | ||
* @return bool | ||
*/ | ||
public function can_decode(array $attributes, $shortcode_tag, $post_id); | ||
} |
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 |
---|---|---|
|
@@ -5,6 +5,5 @@ | |
/** | ||
* @var array $atts | ||
*/ | ||
|
||
?> | ||
<?php echo do_shortcode( $atts['text'] ); ?> |