Skip to content

Commit

Permalink
Add maybe to callable function name
Browse files Browse the repository at this point in the history
  • Loading branch information
frozzare committed Sep 8, 2015
1 parent e1c852f commit e589737
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 38 deletions.
58 changes: 29 additions & 29 deletions src/includes/lib/utilities.php
Original file line number Diff line number Diff line change
Expand Up @@ -209,35 +209,6 @@ function papi_get_cache_key( $key, $suffix ) {
return sprintf( '%s_%s', $key, $suffix );
}

/**
* Papi get callable value if is it callable.
*
* @param mixed $callable
*
* @return mixed
*/
function papi_get_callable_value( $callable ) {
if ( is_callable( $callable ) ) {
$ob_level = ob_get_level();

ob_start();

try {
call_user_func( $callable );
} catch ( Exception $e ) {
while ( ob_get_level() > $ob_level ) {
ob_end_clean();
}

return $callable;
}

return ltrim( ob_get_clean() );
}

return $callable;
}

/**
* Get namespace name and/or class name from page type file.
*
Expand Down Expand Up @@ -503,6 +474,35 @@ function papi_is_metod( $method ) {
return $_SERVER ['REQUEST_METHOD'] === strtoupper( $method );
}

/**
* Papi get callable value if is it callable.
*
* @param mixed $callable
*
* @return mixed
*/
function papi_maybe_get_callable_value( $callable ) {
if ( is_callable( $callable ) ) {
$ob_level = ob_get_level();

ob_start();

try {
call_user_func( $callable );
} catch ( Exception $e ) {
while ( ob_get_level() > $ob_level ) {
ob_end_clean();
}

return $callable;
}

return ltrim( ob_get_clean() );
}

return $callable;
}

/**
* Replace '\n' with '<br />'.
*
Expand Down
2 changes: 1 addition & 1 deletion src/includes/properties/class-papi-property-html.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public function html() {
papi_render_html_tag( 'div', [
'data-papi-rule' => $this->html_name(),
'class' => 'property-html',
papi_get_callable_value( $settings->html )
papi_maybe_get_callable_value( $settings->html )
] );
}
}
4 changes: 2 additions & 2 deletions src/includes/properties/class-papi-property.php
Original file line number Diff line number Diff line change
Expand Up @@ -182,15 +182,15 @@ protected function render_property_html() {
papi_render_html_tag( 'div', [
'class' => 'papi-before-html',
'data-property' => $this->get_option( 'type' ),
papi_get_callable_value( $this->get_option( 'before_html' ) )
papi_maybe_get_callable_value( $this->get_option( 'before_html' ) )
] );

$this->html();

papi_render_html_tag( 'div', [
'class' => 'papi-after-html',
'data-property' => $this->get_option( 'type' ),
papi_get_callable_value( $this->get_option( 'after_html' ) )
papi_maybe_get_callable_value( $this->get_option( 'after_html' ) )
] );
}

Expand Down
12 changes: 6 additions & 6 deletions tests/includes/lib/utilities-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -133,10 +133,10 @@ public function test_papi_get_cache_key() {
unset( $post );
}

public function test_papi_get_callable_value() {
$this->assertEquals( 'Hello', papi_get_callable_value( 'say_hello_stub' ) );
$this->assertEquals( 'file', papi_get_callable_value( 'file' ) );
$this->assertEquals( false, papi_get_callable_value( false ) );
public function test_papi_maybe_get_callable_value() {
$this->assertEquals( 'Hello', papi_maybe_get_callable_value( 'say_hello_stub' ) );
$this->assertEquals( 'file', papi_maybe_get_callable_value( 'file' ) );
$this->assertEquals( false, papi_maybe_get_callable_value( false ) );
}

public function test_papi_get_class_name() {
Expand Down Expand Up @@ -265,7 +265,7 @@ public function test_papi_html_tag() {

$this->assertEquals( '<label>Hello</label>', papi_html_tag( 'label', 'Hello' ) );

$this->assertEquals( '<label>Hello</label>', papi_html_tag( 'label', papi_get_callable_value( 'say_hello_stub' ) ) );
$this->assertEquals( '<label>Hello</label>', papi_html_tag( 'label', papi_maybe_get_callable_value( 'say_hello_stub' ) ) );
}

public function test_papi_render_html_tag() {
Expand Down Expand Up @@ -306,7 +306,7 @@ public function test_papi_render_html_tag() {

papi_render_html_tag( 'label', [
'for' => (object) [],
papi_get_callable_value( 'say_hello_stub' )
papi_maybe_get_callable_value( 'say_hello_stub' )
] );

$this->expectOutputRegex( '/\<label for\=\"\{\}\"\>Hello\<\/label\>/' );
Expand Down

0 comments on commit e589737

Please sign in to comment.