Created
April 12, 2017 16:08
-
-
Save mik-laj/4db3ebdea99a3f76400073b41896cc73 to your computer and use it in GitHub Desktop.
Przykład implementacji akcji i filtrów z Wordpressa w PHP
This file contains hidden or 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
<?php | |
global $actions; | |
$actions = []; | |
function add_action($action, $call, $priority = 10){ | |
global $actions; | |
if(!isset($actions[$action])){ | |
$actions[$action] = []; | |
} | |
$actions[$action][] = compact('call', 'priority'); | |
} | |
function do_action($action){ | |
global $actions; | |
if(!isset($actions[$action])){ | |
return; | |
} | |
$to_execute = $actions[$action]; | |
// Sort by priority | |
usort($to_execute, function($item1,$item2) { | |
if ($item1['priority'] == $item2['priority']){ | |
return 0; | |
} | |
return ($item1['priority'] > $item2['priority']) ? 1 : -1; | |
}); | |
// var_dump($to_execute); | |
// Execute all callbacks | |
foreach ($to_execute as $item) { | |
call_user_func($item['call']); | |
} | |
} | |
// Core actions | |
add_action('display_head', function(){ | |
echo '<title>Testowy tytuł</title>'; | |
}); | |
add_action('display_head', function(){ | |
echo '<meta charset="utf-8">'; | |
}); | |
add_action('display_content', function(){ | |
?> | |
<h1>Lorem</h1> | |
<p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime aliquam ea, fugiat ipsum voluptas, saepe temporibus laboriosam labore. Incidunt odio aspernatur quia excepturi iste, architecto nostrum sequi dolorem corporis reprehenderit?</p> | |
<?php | |
}); | |
// Wtyczka 1 - dodaje container wokół tresci | |
add_action('display_head', function() { | |
?> | |
<style> | |
.container{ | |
width: 800px; | |
margin: 0 auto; | |
} | |
</style> | |
<?php | |
}); | |
add_action('display_content', function(){ | |
echo '<div class="container">'; | |
}, 9); | |
add_action('display_content', function(){ | |
echo '</div>'; | |
}, 11); | |
add_action('display_content', function(){ | |
global $actions; | |
?> | |
<pre><?php var_dump($actions); ?></pre> | |
<?php | |
}); | |
?> | |
<?php // Twoja templatka ?> | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<?php do_action('display_head'); ?> | |
</head> | |
<body> | |
<?php do_action('display_content'); ?> | |
</body> | |
</html> |
This file contains hidden or 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
<?php | |
global $filters; | |
$filters = []; | |
function apply_filters($filter, $data){ | |
global $filters; | |
if(!isset($filters[$filter])){ | |
return ; | |
} | |
$to_execute = $filters[$filter]; | |
// TODO Add sort by priority; | |
$result = $data; | |
foreach ($to_execute as $item) { | |
$result = call_user_func($item['call'], $data); | |
} | |
return $result; | |
} | |
function add_filter($filter, $call){ | |
// TODO Add support priority | |
global $filters; | |
if(!isset($filters[$filter])){ | |
$filters[$filter] = []; | |
} | |
$filters[$filter][] = compact('call'); | |
} | |
// Mechanizm pobierania wpisów z bazy danych i inne | |
global $posts; | |
$posts = []; | |
$posts[1] = ['title' => 'Tytuł', 'content' => '<p>Polska tresc</p>']; | |
function get_post($id){ | |
global $posts; | |
$filtered_id = apply_filters('get_post_id', $id); | |
return $posts[$filtered_id]; | |
} | |
// Wtyczka do obsługi stron wielojezycznych | |
$posts[1001] = ['title' => 'English', 'content' => '<p>Angielska tresc</p>']; | |
add_filter('get_post_id', function($id){ | |
$lang = isset($_GET['lang']) ? $_GET['lang'] : 'pl'; | |
if($lang === 'pl'){ | |
return $id; | |
} | |
if($id < 1000){ | |
return $id + 1000; | |
} | |
return $id; | |
}); | |
?> | |
<?php // Pobieranie wpisu o id = 1 ?> | |
<pre><?php var_dump(get_post(1)); ?></pre> | |
<?php // Opcja zmiany jezyka ?> | |
<a href="?lang=pl">Strona polska</a> | |
<a href="?lang=en">Strona angielska</a> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment