Skip to content

Instantly share code, notes, and snippets.

@vilmosioo
vilmosioo / RegexJava.java
Created July 10, 2012 14:06
How to properly scan a file for a specific regular expression, using Scanner and Pattern objects (Java code)
Scanner scanner = null;
try{
// change filePath to your your source
scanner = new Scanner(new BufferedReader(new FileReader(filePath)));
Pattern p = Pattern.compile("--REGEX--"); // enter your regular expression
while (scanner.hasNext()) {
Matcher m = p.matcher(scanner.nextLine());
while (m.find()) {
String result = m.group();
// do something with your result
@vilmosioo
vilmosioo / MainController.java
Created December 5, 2012 18:11
Controller concept as a singleton object
/*
* Singleton pattern to ensure a single controller
*/
private static MainController controller = null;
public static synchronized MainController Current() {
if (controller == null) {
controller = new MainController();
}
return controller;
@vilmosioo
vilmosioo / Resources.java
Created December 5, 2012 18:13
Example of static resources file
public class Resources {
public static String FILENAME = "fileName";
public static Font STANDARD_FONT = new Font("DejaVu Sans", Font.PLAIN, 12);
public static class Colors {
public static Color Blue = new Color(0.3f, 0.5f, 1f);
public static Color Red = new Color(1f, 0.3f, 0.1f);
public static Color Green = new Color(0.3f, 1f, 0.5f);
}
}
@vilmosioo
vilmosioo / Repository.java
Created December 5, 2012 18:14
Repository concept
// It is recommended to use an interface when using a repository.
// This way you can change the implementation without affecting the usage
public interface IRepository {
public void connect();
}
public class Repository implements IRepository {
// get the instance of the controller
@vilmosioo
vilmosioo / MainController.java
Created December 5, 2012 18:15
Example of reflection usage in Java
protected void setModelProperty(String propertyName, Object newValue) {
for (AbstractModel model : registeredModels) {
try {
Method method = model.getClass().getMethod("set" + propertyName, new Class[] { newValue.getClass() });
method.invoke(model, newValue);
} catch (Exception ex) {
// TODO Handle exception.
}
}
}
@vilmosioo
vilmosioo / function.php
Last active October 13, 2015 15:38
A smarter way to get post thumbnail
<?php
function post_thumbdail($full){
if ( has_post_thumbnail() ) {
echo "<aside><a href='".get_permalink()."' title='".get_the_title()."' rel='canonical'>";
the_post_thumbnail($full);
echo "</a></aside>";
}else{
if( $full == 'full' ) return;
$attachments = get_posts( array(
@vilmosioo
vilmosioo / function.php
Created December 5, 2012 18:20
How to get related posts in WordPress
<?php
// get current post categories and tags
$categories = get_the_category($post->ID);
$tags = get_the_tags($post->ID);
if ($categories || $tags) {
$category_ids = array();
if($categories)
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$tag_ids = array();
if($tags)
@vilmosioo
vilmosioo / tumbleblog.php
Created December 6, 2012 00:10
Create your own Tumbleblog in WordPress! Example usage with my feeds
<?php // Get RSS Feed(s)
include_once(ABSPATH . WPINC . '/feed.php');
$feed = array();
$feed += add('http://vilmosioo.co.uk/feed/', 'wp');
$feed += add('http://pinterest.com/ioowilly/feed.rss', 'pinterest');
$feed += add('http://api.twitter.com/1/statuses/user_timeline.rss?screen_name=ioowilly', 'twitter');
$feed += add('http://www.goodreads.com/user/updates_rss/4165963?key=a5d32462cef6d00b08e095ba9b74d3e03c310841', 'goodreads');
krsort($feed);
@vilmosioo
vilmosioo / twitter.php
Last active December 11, 2015 14:48
Adding Twitter cards to a WordPress website. Add this to your header.php
<?php
if(is_singular()) { ?>
<meta name="twitter:card" content="summary">
<meta name="twitter:url" content="<?php echo get_permalink($post->ID); ?>">
<meta name="twitter:title" content="<?php echo $post->post_title;?>">
<meta name="twitter:description" content="<?php echo substr( $post->post_content, 0, 150 );?>">
<?php
// you will need to hardcode this or use a plugin that allows twitter info to be added to authors
$twitter = the_author_meta('twitter', $post->post_author);
if($twitter){
@vilmosioo
vilmosioo / Github_API.php
Created February 14, 2013 16:04
Github API helper class. Uses the github api to retrieve public gists, repos or commits for a specific user
<?php
/*
* Github API helper class
*
* Uses the github api to retrieve public gists, repos or commits for a specific user
*/
class Github_API{
static function get_data($url){
$base = "https://api.github.com/";
$response = wp_remote_get($base . $url, array( 'sslverify' => false ));