Search Text from Content and replace by php script as PLUGINs - php

I am trying to build a wordpress-like plugin for my own CMS using str_replace. So when user saves something like '[do_something]' into page content, it will be replaced by a prebuild function.
example:
<?php function do_something() {
// search database and display something
}
<?php
echo str_replace("[do_something]","<?php do_something(); ?>",$page_content);
?>
However using echo it returns
"<?php do_something(); ?>"
as text instead of php script and fails to execute.
Any suggestion?
UPDATED
So finally, I have come up with this:
function plugin($page_content){
$plugins = array(
'[function1]' => 'function1();',
'[function2]' => 'fucntion2();'
);
foreach($plugins as $plugin => $function) {
if (strpos($page_content, $plugin) !== false) {
eval($function);
break;
}
}
}
:)

Try this:
<?php
eval(str_replace('[do_something]', 'do_something();', $page_content));
?>
But please remember that using eval especially with user's input is a very bad practise.

Related

function as function input php

I want to create my own template for building my next web projects.
But my problem is that I am trying to create my own template system with my own function, also to learn more about coding on my on aswell.
I'm trying to improve my code to be me clean/smooth as I code.
But now I ran into this problem
a-function-file.php
<?php
$Website_info = array("SiteTitle"=>"CLiCK", "BaseUrl"=>"http://localhost/CLick/");
//css styles her
$website_styles = array(
array("src"=>"libs/css/bootstrap.min.css", "type"=>"text/css"),
array("src"=>"libs/themes/click.css", "type"=>"text/css")
);
//javascripts her
$website_scripts = array(
array("src"=>"libs/js/bootstrap.min.js", "type"=>"text/javascript")
);
$website_navigation_top_links = array(
array("link"=>"index.php","name"=>"Home")
);
function Click_styles()
{
global $website_styles;
$styleOutput = "";
foreach ($website_styles as $key => $CssStyle):
$styleOutput .= "<link href='".$CssStyle["src"]."' rel='stylesheet' type='".$CssStyle["type"]."'>\n";
endforeach;
return $styleOutput;
}
//my function to create html codes within head tags
function Click_header()
{
//Getting website info into function
global $Website_info;
global $website_styles;
$ImpotedStyles = Click_styles();
//creating the html (can maybe be created more clean later)
$Header_output = "<title>".$Website_info["SiteTitle"]."</title>\n";
$Header_output .= "<base href='".$Website_info["BaseUrl"]."' />\n";
$Header_output = $Header_output.$ImpotedStyles;
// return the complied output (as HTML)
return $Header_output;
}
?>
So far so good, because this works if I write
<?php echo Click_header();?>
But I want to use the function like this with, by passing it a function as an argument
<?php
//the function that doesnt work
function printHTML($ThisShouldBeAFunctionNotAVar, $Description="none") {
echo $ThisShouldBeAFunctionNotAVar
}
?>
<?php
//how I want to use the function
printHTML(Click_header(), "The website header");
//and maybe if I had a footer I could display the return of that function too
printHTML(Click_foter(), "a smart footer function");
?>
I hope you can help me with this or get a better understanding for maybe something smarter
I fount this solution on my own
<?php
function printHTML($CustomFunction,$Description="")
{
$function = ($CustomFunction;
echo $function();
}
?>

Displaying admin notice dynamically

I would like show the admin notice while post text is being edited (before Save post). I suppose admin_notice hook wont work in this (it doesn't for me). Any idea how to show some error message to alert the user that Post content is not going to be accepted ?
I prefer inline display like admin notice instead of popups that could be blocked.
Following doesn't work
function secure_oembed_filter($html, $url, $attr, $post_ID) {
if (strlen($html) > 0 && strpos($html, "http://")) {
//wp_die("Unsecure link in embeds", "Unsecured post");
} else {
add_action('admin_notices', array($this, 'show_error'));
}
return $html;
}
private function show_error () {
echo '<div class="error"><p>This is an error</p></div>';
}
In that case wp_remote_get() will work fine.
Create a notice.php file and uploaded it on my server.
Then I have added these code into my plugin and it works for me.
<?php
$remote_data = wp_remote_get( 'http://example.com/notice.php' );
$remote_body = wp_remote_retrieve_datat( $remote_data );
?>
<div class="notice">
<?php echo $remote_body ?>
</div>
Hope that helps.

Problem return a function inside of a shortcode in Wordpress

I have some trouble coding a themes shortcode. I want the code to display a div with a view counter function, and then a link with the shotcodes content as the url.
The view_count(); function works fine when called inside theme files, and I actually managed to make it show, but then it displayed before the_content(); of the post (the gray bar), when I wanted it within content under an element.
(1) Here's what I've got:
function video_block( $atts, $content = null ) {
return '<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="' . $content . '">Link</a></div>';
}
(2) Here's the code that displays both on top of page:
function video_block( $atts, $content = null ) { ?>
<div class="video-block"><span class="ViewCount"><?php the_views(); ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>
<?php }
(3) This code displays the views above content, and the link in the correct place:
function video_block( $atts, $content = null ) {
$views = the_views();
return '<div class="video-block"><span class="ViewCount"><?php $views; ?></span> <a class="dl-source" href="<?php echo $content; ?>">Link</a></div>';
}
I read somewhere in the Wordpress forums that you should return (instead of echo) values within functions, but that breaks it, displaying the view count, skips the html and spits out the $content.
Here is a link to the page in question: http://nvrt.me/4Qf1 (currently using code from block #2)
I'm running out of midnight oil. I would really appreciate it if someone could help me out.
EDIT:
Here is the code for the_views(); function. I can see that it's echoed, but when changed to return, it doesn't display it at all.
### Function: Display The Post Views
function the_views($display = true, $prefix = '', $postfix = '', $always = false) {
$post_views = intval(post_custom('views'));
$views_options = get_option('views_options');
if ($always || should_views_be_displayed($views_options)) {
$output = $prefix.str_replace('%VIEW_COUNT%', number_format_i18n($post_views), $views_options['template']).$postfix;
if($display) {
echo apply_filters('the_views', $output);
} else {
return apply_filters('the_views', $output);
}
}
elseif (!$display) {
return '';
}
}
Even though it is a recommended practice in Wordpress to have functions that return values, it is not always possible, especially when you are calling another function that writes it's output directly to the stream.
When the component writes it's output directly to the stream you need to code to accommodate that behavior unless you want to rewrite the who component (I wouldn't do that ;-) ).
In this instance the the_views() function actually offers you both options. If you look at the $display parameter and follow the code this function can behave both ways. If $display is set to True (it's default) it will echo the results of the function. If $display is set to False it will return the output.
So you have two options, both of which should work:
Option 1, Return a value
Note that when I call the_views() I pass it a false parameter like this: the_views(false)
<?php
function video_block( $atts, $content = null ) {
return "<div class=\"video-block\"><span class=\"ViewCount\">" . the_views(false) . "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}
?>
*Option 2: Echo your output *
Note that when I call the the_views() there are no parameters passed to it.
<?php
function video_block( $atts, $content = null ) {
echo "<div class=\"video-block\"><span class=\"ViewCount\">";
the_views();
echo "</span><a class=\"dl-source\" href=\"$content\">Link</a></div>";
}
?>
Oh, also, don't forget to escape your quotes when you are returning a string.
Couldn't add the code block as a comment on #BLewis response but working off what he said, I did something like this:
function my_shortcode() {
ob_start();
a_function_to_display();
$data = ob_get_clean();
return $data;
}
add_shortcode( 'shortcode_name', 'my_shortcode' );
I'd like to contribute a new answer here.
I was working on extending a plugin and found this was how the author did it and to me it makes much more sense.
If you're outputting a lot of HTML you should not use echo, it's bad practise, hard to read and a pain to keep the formatting of the output nice using echo. Even if you use it's counter parts, it's still less clean to have echo_r or print or var_dump than to use pure HTML.
Since PHP only runs inside the wrapper tags <?php and ?>, you should use this to your advantage.
But since we can't just do this here because you may lose your content order, you can use some object storing. Basically before you start your HTML block (probably at the beginning of your shortcode function would be good), just add ob_start(); to store your code. Then just before ending your function, you can return your data using $data = ob_get_clean(); and return $data; .
Good luck with more WordPress work.

Simple template var replacement, but with a twist

So I'm setting up a system that has a lot of emails, and variable replacement within it, so I'm writing a class to manage some variable replacement for templates stored in the database.
Here's a brief example:
// template is stored in db, so that's how this would get loaded in
$template = "Hello, %customer_name%, thank you for contacting %website_name%";
// The array of replacements is built manually and passed to the class
// with actual values being called from db
$replacements = array('%customer_name%'=>'Bob', '%website_name%'=>'Acme');
$rendered = str_replace(array_keys($replacements), $replacements, $template);
Now, that works well and good for single var replacements, basic stuff. However, there are some places where there should be a for loop, and I'm lost how to implement it.
The idea is there'd be a template like this:
"hello, %customer_name%, thank you for
requesting information on {products}"
Where, {products} would be an array passed to the template, which the is looped over for products requested, with a format like:
Our product %product_name% has a cost
of %product_price%. Learn more at
%product_url%.
So an example rendered version of this would be:
"hello, bob, thank you for requesting
information on:
Our product WidgetA has a cost of $1.
Learn more at example/A
Our product WidgetB has a cost of $2.
Learn more at example/B
Our product WidgetC has a cost of $3.
Learn more at example/C.
What's the best way to accomplish this?
Well, I really dont see the point in a template engine that uses repalcements/regex
PHP Is already a template engine, when you write <?php echo $var?> its just like doing <{$var}> or {$var}
Think of it this way, PHP Already translates <?php echo '<b>hello</b>'?> into <b>hello</b> by its engine, so why make it do everything 2 times over.
The way i would implement a template engine is like so
Firstly create a template class
class Template
{
var $vars = array();
function __set($key,$val)
{
$this->vars[$key] = $val;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : false;
}
function output($tpl = false)
{
if($tpl === false)
{
die('No template file selected in Template::output(...)');
}
if(!file_exists(($dir = 'templates/' . $tpl . '.php')))
{
die(sprintf('Tpl file does not exists (%s)',$dir));
}
new TemplateLoader($dir,$this->vars);
return true;
}
}
This is what you use in your login such as index.php, you will set data just like an stdClass just google it if your unsure. and when you run the output command it sends the data and tpl to the next class below.
And then create a standalone class to compile the tpl file within.
class TemplateLoader
{
private $vars = array();
private $_vars = array(); //hold vars set within the tpl file
function __construct($file,$variables)
{
$this->vars = $variables;
//Start the capture;
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean(); //Clean it
//Return here if you wish
echo $contents;
}
function __get($key)
{
return isset($this->vars[$key]) ? $this->vars[$key] : (isset($this->_vars[$key]) ? $this->_vars[$key] : false) : false;
}
function __set($key,$val)
{
$this->_vars[$key] = $val;
return true;
}
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
}
The reason we keep this seperate is so it has its own space to run in, you just load your tpl file as an include in your constructor so it only can be loaded once, then when the file is included it has access to all the data and methods within TemplateLoader.
Index.php
<?php
require_once 'includes/Template.php';
require_once 'includes/TemplateLoader.php';
$Template = new Template();
$Template->foo = 'somestring';
$Template->bar = array('some' => 'array');
$Template->zed = new stdClass(); // Showing Objects
$Template->output('index'); // loads templates/index.php
?>
Now here we dont really want to mix html with this page because by seperating the php and the view / templates you making sure all your php has completed because when you send html or use html it stops certain aspects of your script from running.
templates/index.php
header
<h1><?php $this->foo;?></h1>
<ul>
<?php foreach($this->bar as $this->_foo):?>
<li><?php echo $this->_foo; ?></li>
<?php endforeach; ?>
</ul>
<p>Testing Objects</p>
<?php $this->sidebar = $this->foo->show_sidebar ? $this->foo->show_sidebar : false;?>
<?php if($this->sidebar):?>
Showing my sidebar.
<?php endif;?>
footer
Now here we can see that were mixing html with php but this is ok because in ehre you should only use basic stuff such as Foreach,For etc. and Variables.
NOTE: IN the TemplateLoader Class you can add a function like..
function bold($key)
{
return '<strong>' . $this->$key . '</string>';
}
This will allow you to increase your actions in your templates so bold,italic,atuoloop,css_secure,stripslashs..
You still have all the normal tools such as stripslashes/htmlentites etc.
Heres a small example of the bold.
$this->bold('foo'); //Returns <strong>somestring</string>
You can add lots of tools into the TempalteLoader class such as inc() to load other tpl files, you can develop a helper system so you can go $this->helpers->jquery->googleSource
If you have any more questions feel free to ask me.
----------
An example of storing in your database.
<?php
if(false != ($data = mysql_query('SELECT * FROM tpl_catch where item_name = \'index\' AND item_save_time > '.time() - 3600 .' LIMIT 1 ORDER BY item_save_time DESC')))
{
if(myslq_num_rows($data) > 0)
{
$row = mysql_fetch_assc($data);
die($row[0]['item_content']);
}else
{
//Compile it with the sample code in first section (index.php)
//Followed by inserting it into the database
then print out the content.
}
}
?>
If you wish to store your tpl files including PHP then that's not a problem, within Template where you passing in the tpl file name just search db instead of the filesystem
$products = array('...');
function parse_products($matches)
{
global $products;
$str = '';
foreach($products as $product) {
$str .= str_replace('%product_name%', $product, $matches[1]); // $matches[1] is whatever is between {products} and {/products}
}
return $str;
}
$str = preg_replace_callback('#\{products}(.*)\{/products}#s', 'parse_products', $str);
The idea is to find string between {products} and {products}, pass it to some function, do whatever you need to do with it, iterating over $products array.
Whatever the function returns replaces whole "{products}[anything here]{/products}".
The input string would look like that:
Requested products: {products}%product_name%{/products}

What is the best way to include a php file as a template?

I have simple template that's html mostly and then pulls some stuff out of SQL via PHP and I want to include this template in three different spots of another php file. What is the best way to do this? Can I include it and then print the contents?
Example of template:
Price: <?php echo $price ?>
and, for example, I have another php file that will show the template file only if the date is more than two days after a date in SQL.
The best way is to pass everything in an associative array.
class Template {
public function render($_page, $_data) {
extract($_data);
include($_page);
}
}
To build the template:
$data = array('title' => 'My Page', 'text' => 'My Paragraph');
$Template = new Template();
$Template->render('/path/to/file.php', $data);
Your template page could be something like this:
<h1><?php echo $title; ?></h1>
<p><?php echo $text; ?></p>
Extract is a really nifty function that can unpack an associative array into the local namespace so that you can just do stuff like echo $title;.
Edit: Added underscores to prevent name conflicts just in case you extract something containing a variable '$page' or '$data'.
Put your data in an array/object and pass it to the following function as a second argument:
function template_contents($file, $model) {
if (!is_file($file)) {
throw new Exception("Template not found");
}
ob_start();
include $file;
$contents = ob_get_contents();
ob_end_clean();
return $contents;
}
Then:
Price: <?php echo template_contents("/path/to/file.php", $model); ?>

Categories