Changeset 1064687
- Timestamp:
- 01/10/2015 02:46:37 PM (11 years ago)
- Location:
- enzymes/trunk
- Files:
-
- 6 edited
-
enzymes.php (modified) (1 diff)
-
src/Enzymes3.php (modified) (29 diffs)
-
src/EnzymesCapabilities.php (modified) (1 diff)
-
src/EnzymesOptions.php (modified) (3 diffs)
-
src/EnzymesPlugin.php (modified) (3 diffs)
-
tests/test-Enzymes3.php (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
-
enzymes/trunk/enzymes.php
r1063085 r1064687 10 10 */ 11 11 12 12 13 define('ENZYMES_FILENAME', __FILE__); 13 require 'src/EnzymesPlugin.php';14 require_once 'src/EnzymesPlugin.php'; 14 15 15 16 $enzymesPlugin = new EnzymesPlugin(); 17 18 19 20 // --------------------------------------------------------------------------------------------------------------------- 21 // The following code will allow old version 2 enzymes sequences to work exactly as they did. 22 require_once 'enzymes.2/enzymes.php'; 23 // --------------------------------------------------------------------------------------------------------------------- -
enzymes/trunk/src/Enzymes3.php
r1063307 r1064687 7 7 class Enzymes3 8 8 { 9 const PREFIX = 'enzymes.'; 9 /** 10 * When calling the engine directly, for forcing the global post, use one of the following: 11 * - EnzymesPlugin::engine()->metabolize($content); 12 * - EnzymesPlugin::engine()->metabolize($content, null); 13 * - EnzymesPlugin::engine()->metabolize($content, Enzymes3::GLOBAL_POST); 14 */ 15 const GLOBAL_POST = null; 16 17 /** 18 * When calling the engine directly, for forcing no post at all, use one of the following: 19 * - EnzymesPlugin::engine()->metabolize($content, Enzymes3::NO_POST); 20 */ 21 const NO_POST = -1; 22 23 /** 24 * When calling the engine directly, ID of the user to consider the author after forcing no post. 25 */ 26 const NO_POST_AUTHOR = 1; 10 27 11 28 /** … … 33 50 /** 34 51 * The post which the content belongs to. 52 * It can be null if the developer forced no post with ->metabolize($content, Enzymes3::NO_POST). 35 53 * 36 54 * @var WP_Post 37 55 */ 38 protected $ current_post;56 protected $injection_post; 39 57 40 58 /** … … 187 205 'injection' => '(?<injection>{[$sequence]})', 188 206 ); 189 $result = array();207 $result = array(); 190 208 foreach ($grammar as $symbol => $rule) { 191 $regex = new Ando_Regex($rule);209 $regex = new Ando_Regex($rule); 192 210 $result[$symbol] = $regex->interpolate($result); 193 211 } … … 201 219 function init_e_injection() 202 220 { 203 $before = new Ando_Regex('(?<before>.*?)');221 $before = new Ando_Regex('(?<before>.*?)'); 204 222 $could_be_injection = new Ando_Regex('\{\[(?<could_be_sequence>.*?)\]\}'); 205 $after = new Ando_Regex('(?<after>.*)');206 $content = new Ando_Regex('^$before$could_be_injection$after$', '@@s');223 $after = new Ando_Regex('(?<after>.*)'); 224 $content = new Ando_Regex('^$before$could_be_injection$after$', '@@s'); 207 225 $content->interpolate(array( 208 'before' => $before,209 'could_be_injection' => $could_be_injection,210 'after' => $after,211 ));226 'before' => $before, 227 'could_be_injection' => $could_be_injection, 228 'after' => $after, 229 )); 212 230 $this->e_injection = $content; 213 231 } … … 222 240 $sequence_valid = new Ando_Regex(Ando_Regex::option_same_name() . '^(?:\|$enzyme)+$', '@@'); 223 241 $sequence_valid->interpolate(array( 224 'enzyme' => $this->grammar['enzyme'],225 ));242 'enzyme' => $this->grammar['enzyme'], 243 )); 226 244 $this->e_sequence_valid = $sequence_valid; 227 245 } … … 233 251 function init_e_sequence_start() 234 252 { 235 $rest = new Ando_Regex('(?:\|(?<rest>.+))');253 $rest = new Ando_Regex('(?:\|(?<rest>.+))'); 236 254 $sequence_start = new Ando_Regex(Ando_Regex::option_same_name() . '^$enzyme$rest?$', '@@'); 237 255 $sequence_start->interpolate(array( 238 'enzyme' => $this->grammar['enzyme'],239 'rest' => $rest,240 ));256 'enzyme' => $this->grammar['enzyme'], 257 'rest' => $rest, 258 )); 241 259 $this->e_sequence_start = $sequence_start; 242 260 } … … 250 268 $maybe_quoted = new Ando_Regex('(?<before_string>.*?)$string|(?<anything_else>.+)', '@@s'); 251 269 $maybe_quoted->interpolate(array( 252 'string' => $this->grammar['string'],253 ));270 'string' => $this->grammar['string'], 271 )); 254 272 $this->e_string = $maybe_quoted; 255 273 } … … 268 286 $this->e_comment = new Ando_Regex('\/\*.*?\*\/', '@@s'); 269 287 // for some reason WP introduces some C2 (hex) chars when writing a post... 270 $this->e_unbreakable_space = new Ando_Regex('\xC2\xA0', '@@');271 $this->e_all_spaces = new Ando_Regex('(?:\s|\xC2\xA0)+', '@@');288 $this->e_unbreakable_space = new Ando_Regex('\xC2\xA0', '@@'); 289 $this->e_all_spaces = new Ando_Regex('(?:\s|\xC2\xA0)+', '@@'); 272 290 $this->e_escaped_injection_delimiter = new Ando_Regex('\\\\([{[\]}])', '@@'); 273 $this->e_escaped_string_delimiter = new Ando_Regex('\\\\=', '@@');291 $this->e_escaped_string_delimiter = new Ando_Regex('\\\\=', '@@'); 274 292 } 275 293 … … 279 297 $this->init_grammar(); 280 298 $this->init_expressions(); 281 $this->options = new EnzymesOptions( self::PREFIX);299 $this->options = new EnzymesOptions(); 282 300 } 283 301 … … 293 311 array_shift($keys); 294 312 $default = array_fill_keys($keys, ''); 295 $hash = array_merge($default, $hash);313 $hash = array_merge($default, $hash); 296 314 } 297 315 … … 334 352 switch (true) { 335 353 case ($post == ''): 336 $result = $this-> current_post;354 $result = $this->injection_post; 337 355 break; 338 356 case ($post[0] == '@'): 339 $result = get_page_by_path($slug, OBJECT, 'post'); 357 // We can't use the following API call because we want all post types. 358 //$result = get_page_by_path($slug, OBJECT, 'post'); 359 global $wpdb; 360 $post_id = $wpdb->get_var("SELECT `ID` FROM $wpdb->posts WHERE `post_name` = '$slug' LIMIT 1"); 361 $result = get_post($post_id); 340 362 break; 341 363 case (is_numeric($post)): … … 475 497 function wp_author( $post_object ) 476 498 { 477 $id = $post_object->post_author;499 $id = $post_object->post_author; 478 500 $result = get_user_by('id', $id); 479 501 return $result; … … 481 503 482 504 /** 505 * True if the current post's author can exercise the capability. 506 * 507 * @param string $capability 508 * 509 * @return bool 510 */ 511 protected 512 function injection_author_can( $capability ) 513 { 514 if ( is_null($this->injection_post) ) { 515 $result = user_can(self::NO_POST_AUTHOR, $capability); 516 } else { 517 $result = author_can($this->injection_post, $capability); 518 } 519 return $result; 520 } 521 522 /** 483 523 * True if the post belongs to the current post's author. 484 524 * … … 488 528 */ 489 529 protected 490 function belongs_to_current_author( $post_object ) 491 { 492 $result = $this->current_post->post_author == $post_object->post_author; 530 function injection_author_owns( $post_object ) 531 { 532 if ( is_null($this->injection_post) ) { 533 $result = self::NO_POST_AUTHOR == $post_object->post_author; 534 } else { 535 $result = $this->injection_post->post_author == $post_object->post_author; 536 } 493 537 return $result; 494 538 } … … 507 551 { 508 552 if ( author_can($post_object, EnzymesCapabilities::create_dynamic_custom_fields) && 509 ($this-> belongs_to_current_author($post_object) ||553 ($this->injection_author_owns($post_object) || 510 554 author_can($post_object, EnzymesCapabilities::share_dynamic_custom_fields) && 511 author_can($this->current_post,EnzymesCapabilities::use_others_custom_fields))555 $this->injection_author_can(EnzymesCapabilities::use_others_custom_fields)) 512 556 ) { 513 557 list($result,) = $this->safe_eval($code, $arguments); … … 536 580 preg_match($expression, $post_item, $matches); 537 581 $post_object = $this->wp_post($matches); 538 $code = $this->wp_post_field($post_object, $matches); 582 if ( ! $post_object instanceof WP_Post ) { 583 return null; 584 } 585 $code = $this->wp_post_field($post_object, $matches); 539 586 $arguments = $num_args > 0 540 587 ? $this->catalyzed->pop($num_args) 541 588 : array(); 542 $result = $this->execute_code($code, $arguments, $post_object);589 $result = $this->execute_code($code, $arguments, $post_object); 543 590 return $result; 544 591 } … … 561 608 preg_match($expression, $author_item, $matches); 562 609 $post_object = $this->wp_post($matches); 610 if ( ! $post_object instanceof WP_Post ) { 611 return null; 612 } 563 613 $user_object = $this->wp_author($post_object); 564 $code = $this->wp_user_field($user_object, $matches);565 $arguments = $num_args > 0614 $code = $this->wp_user_field($user_object, $matches); 615 $arguments = $num_args > 0 566 616 ? $this->catalyzed->pop($num_args) 567 617 : array(); 568 $result = $this->execute_code($code, $arguments, $post_object);618 $result = $this->execute_code($code, $arguments, $post_object); 569 619 return $result; 570 620 } … … 592 642 break; 593 643 case (strpos($execution, 'hash(') === 0 && $num_args > 0): 594 $result = array();644 $result = array(); 595 645 $arguments = $this->catalyzed->pop(2 * $num_args); 596 646 for ($i = 0, $i_top = 2 * $num_args; $i < $i_top; $i += 2) { 597 $key = $arguments[$i];598 $value = $arguments[$i + 1];647 $key = $arguments[$i]; 648 $value = $arguments[$i + 1]; 599 649 $result[$key] = $value; 600 650 } … … 625 675 { 626 676 if ( author_can($post_object, EnzymesCapabilities::create_dynamic_custom_fields) && 627 ($this-> belongs_to_current_author($post_object) ||677 ($this->injection_author_owns($post_object) || 628 678 author_can($post_object, EnzymesCapabilities::share_dynamic_custom_fields) && 629 author_can($this->current_post,EnzymesCapabilities::use_others_custom_fields))679 $this->injection_author_can(EnzymesCapabilities::use_others_custom_fields)) 630 680 ) { 631 681 list(, $output) = $this->safe_eval(" ?>$code<?php "); 632 682 } elseif ( author_can($post_object, EnzymesCapabilities::create_static_custom_fields) && 633 ($this-> belongs_to_current_author($post_object) ||683 ($this->injection_author_owns($post_object) || 634 684 author_can($post_object, EnzymesCapabilities::share_static_custom_fields) && 635 author_can($this->current_post,EnzymesCapabilities::use_others_custom_fields))685 $this->injection_author_can(EnzymesCapabilities::use_others_custom_fields)) 636 686 ) { 637 687 $output = $code; … … 658 708 ->expression(true); 659 709 preg_match($expression, $post_item, $matches); 660 $code = $this->wp_post_field($post_object, $matches);710 $code = $this->wp_post_field($post_object, $matches); 661 711 $output = $this->transclude_code($code, $post_object); 662 712 return $output; … … 680 730 preg_match($expression, $author_item, $matches); 681 731 $user_object = $this->wp_author($post_object); 682 $code = $this->wp_user_field($user_object, $matches);683 $output = $this->transclude_code($code, $post_object);732 $code = $this->wp_user_field($user_object, $matches); 733 $output = $this->transclude_code($code, $post_object); 684 734 return $output; 685 735 } … … 698 748 { 699 749 $this->debug_print('transcluding post_attr'); 700 $same_author = $this-> belongs_to_current_author($post_object);750 $same_author = $this->injection_author_owns($post_object); 701 751 if ( $same_author && author_can($post_object, EnzymesCapabilities::use_own_attributes) || 702 ! $same_author && author_can($this->current_post, EnzymesCapabilities::use_others_attributes) 752 ! $same_author && 753 $this->injection_author_can(EnzymesCapabilities::use_others_attributes) 703 754 ) { 704 755 $expression = $this->grammar['post_attr']->wrapper_set('@@') … … 725 776 { 726 777 $this->debug_print('transcluding author_attr'); 727 $same_author = $this-> belongs_to_current_author($post_object);778 $same_author = $this->injection_author_owns($post_object); 728 779 if ( $same_author && author_can($post_object, EnzymesCapabilities::use_own_attributes) || 729 ! $same_author && author_can($this->current_post, EnzymesCapabilities::use_others_attributes) 780 ! $same_author && 781 $this->injection_author_can(EnzymesCapabilities::use_others_attributes) 730 782 ) { 731 783 $expression = $this->grammar['author_attr']->wrapper_set('@@') … … 733 785 preg_match($expression, $author_attr, $matches); 734 786 $user_object = $this->wp_author($post_object); 735 $result = $this->wp_user_attribute($user_object, $matches);787 $result = $this->wp_user_attribute($user_object, $matches); 736 788 } else { 737 789 $result = null; … … 757 809 /* @var $author_attr string */ 758 810 $post_object = $this->wp_post($matches); 811 if ( ! $post_object instanceof WP_Post ) { 812 return null; 813 } 759 814 switch (true) { 760 815 case ($post_item != ''): … … 795 850 ? $before_string 796 851 : $anything_else; 797 $result = preg_replace($this->e_all_spaces, '', $outside) .798 preg_replace($this->e_unbreakable_space, ' ', $string); // normal spaces are meaningful in $string852 $result = preg_replace($this->e_all_spaces, '', $outside) . 853 preg_replace($this->e_unbreakable_space, ' ', $string); // normal spaces are meaningful in $string 799 854 return $result; 800 855 } … … 824 879 } 825 880 881 protected 882 function default_version() 883 { 884 // The injection_post at this point of the execution can be null only in version 3 because 885 // in version 2 it follows a different path, i.e. it would go straight into the Enzymes class 886 // by means of a call to the global metabolize() function. -- 887 if ( is_null($this->injection_post) ) { 888 return 3; 889 } 890 // By looking at these dates we can only assume a default version, because another one 891 // could have been forced by the user right into an injection. -- 892 $result = $this->injection_post->post_modified_gmt <= EnzymesPlugin::activated_on() 893 ? 2 894 : 3; 895 return $result; 896 } 897 898 /** 899 * @param string $sequence 900 * 901 * @return false|string 902 */ 903 protected 904 function sequence_version( &$sequence ) 905 { 906 $result = $this->default_version(); 907 $forced_2_prefix = '=enzymes.2=|'; 908 $forced_3_prefix = '=enzymes.3=|'; 909 switch (true) { 910 case (0 === strpos($sequence, $forced_2_prefix)): 911 $sequence = substr($sequence, strlen($forced_2_prefix)); 912 $result = 2; 913 break; 914 case (0 === strpos($sequence, $forced_3_prefix)): 915 $sequence = substr($sequence, strlen($forced_3_prefix)); 916 $result = 3; 917 break; 918 } 919 return $result; 920 } 921 826 922 /** 827 923 * Process the enzymes in the matched sequence. … … 834 930 function process( $could_be_sequence ) 835 931 { 836 $sequence = $this->clean_up($could_be_sequence);932 $sequence = $this->clean_up($could_be_sequence); 837 933 $there_are_only_chained_enzymes = preg_match($this->e_sequence_valid, '|' . $sequence); 838 // $this->debug_print($this->e_sequence_valid . '');839 934 if ( ! $there_are_only_chained_enzymes ) { 840 $result = '{[' . $could_be_sequence . ']}'; // skip this injection like if it had been escaped... 935 $result = '{[' . $could_be_sequence . ']}'; // skip this injection AS IS 936 } elseif ( $this->sequence_version($sequence) == 2 ) { 937 $result = '{[' . $sequence . ']}'; // skip this injection 938 // after stripping out the forced version from $sequence, it any 841 939 } else { 842 940 $this->catalyzed = new EnzymesSequence(); 843 $rest = $sequence;941 $rest = $sequence; 844 942 while (preg_match($this->e_sequence_start, $rest, $matches)) { 845 943 $this->default_empty($matches, 'execution', 'transclusion', 'literal', 'str_literal', 'number', 'rest'); … … 890 988 891 989 /** 990 * @param $args 991 * 992 * @return array 993 */ 994 protected 995 function get_injection_post( $args ) 996 { 997 list(, $post) = array_pad($args, 2, null); 998 if ( $post instanceof WP_Post ) { 999 return array(true, $post); 1000 } 1001 if ( $post == self::NO_POST ) { 1002 return array(true, null); 1003 } 1004 // Some filters of ours do not pass the 2nd argument, while others pass a post ID, but 1005 // 'wp_title' pass a string separator, so we fix this occurrence. 1006 $post_id = current_filter() == 'wp_title' ? null : $post; 1007 $post = get_post($post_id); 1008 if ( is_null($post) ) { 1009 // Consider this an error, because the developer didn't force no post. 1010 return array(false, null); 1011 } 1012 return array(true, $post); 1013 } 1014 1015 /** 892 1016 * Process the injected sequences in the content we are filtering. 893 1017 * 894 * @param string $content 895 * @param null|WP_Post $default_post 1018 * @param string $content 896 1019 * 897 1020 * @return array|null|string 898 1021 */ 899 1022 public 900 function metabolize( $content, $default_post = null ) 901 { 902 $this->current_post = is_object($default_post) 903 ? $default_post 904 : get_post(); 905 if ( is_null($this->current_post) ) { 1023 function metabolize( $content ) 1024 { 1025 $args = func_get_args(); 1026 list($continue, $this->injection_post) = $this->get_injection_post($args); 1027 if ( ! $continue ) { 906 1028 return $content; 907 1029 } 908 if ( ! author_can($this->current_post,EnzymesCapabilities::inject) ) {1030 if ( ! $this->injection_author_can(EnzymesCapabilities::inject) ) { 909 1031 return $content; 910 1032 } -
enzymes/trunk/src/EnzymesCapabilities.php
r1063285 r1064687 3 3 class EnzymesCapabilities 4 4 { 5 const PREFIX = 'enzymes.'; 5 6 6 7 //@formatter:off -
enzymes/trunk/src/EnzymesOptions.php
r1063306 r1064687 3 3 class EnzymesOptions 4 4 { 5 /** 6 * @var string 7 */ 8 protected $prefix; 9 10 /** 11 * @param string $prefix 12 */ 13 public 14 function __construct( $prefix = null ) 15 { 16 $this->prefix = $prefix; 17 } 5 const PREFIX = 'Enzymes3.'; 18 6 19 7 /** … … 27 15 function name( $username = null ) 28 16 { 29 $result = $this->prefix. ($username17 $result = self::PREFIX . ($username 30 18 ? $username 31 19 : 'global_options'); … … 56 44 { 57 45 global $wpdb; 58 $wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE '{$this->prefix}%'"); 46 $prefix = self::PREFIX; 47 $wpdb->query("DELETE FROM `$wpdb->options` WHERE `option_name` LIKE '{$prefix}%'"); 59 48 } 60 49 -
enzymes/trunk/src/EnzymesPlugin.php
r1063306 r1064687 120 120 121 121 foreach ($wp_roles->roles as $name => $role) { 122 if ( 0 === strpos($name, Enzymes 3::PREFIX) ) {122 if ( 0 === strpos($name, EnzymesCapabilities::PREFIX) ) { 123 123 remove_role($name); 124 124 } … … 126 126 127 127 foreach (EnzymesCapabilities::all() as $cap) { 128 if ( 0 === strpos($cap, Enzymes 3::PREFIX) ) {128 if ( 0 === strpos($cap, EnzymesCapabilities::PREFIX) ) { 129 129 $wp_roles->remove_cap('administrator', $cap); 130 130 } … … 132 132 } 133 133 134 static public 135 function activated_on() { 136 $options = self::$options->keysGet(array('activated_on')); 137 return $options['activated_on']; 138 } 139 134 140 } -
enzymes/trunk/tests/test-Enzymes3.php
r1063285 r1064687 168 168 169 169 $target_post_id = $this->factory->post->create(array('post_title' => 'This is the target post.')); 170 $target = get_post($target_post_id);171 170 172 171 $enzymes = new Enzymes3(); … … 178 177 179 178 // this must return the target post (default) 180 $enzymes->metabolize('This post has a {[ fake ]} injection.', $target );179 $enzymes->metabolize('This post has a {[ fake ]} injection.', $target_post_id); 181 180 $result = $this->call_method('wp_post', array(array()), $enzymes); 182 181 $this->assertEquals($target_post_id, $result->ID); 183 182 184 183 // this must return the target post (numeric) 185 $enzymes->metabolize('This post has a {[ fake ]} injection.', $target );184 $enzymes->metabolize('This post has a {[ fake ]} injection.', $target_post_id); 186 185 $result = $this->call_method('wp_post', array(array('post' => $post->ID)), $enzymes); 187 186 $this->assertEquals($post->ID, $result->ID); 188 187 189 188 // this must return the target post (slug) 190 $enzymes->metabolize('This post has a {[ fake ]} injection.', $target );189 $enzymes->metabolize('This post has a {[ fake ]} injection.', $target_post_id); 191 190 $result = $this->call_method('wp_post', array( 192 191 array(
Note: See TracChangeset
for help on using the changeset viewer.