Skip to content

Commit

Permalink
Separate the wordpress functionality into its own file, and included …
Browse files Browse the repository at this point in the history
…it via the 'files' autoload.
  • Loading branch information
jpederson committed May 20, 2019
1 parent e77411d commit 9bc9f14
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 111 deletions.
5 changes: 2 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@
"j7mbo/twitter-api-php":"*"
},
"autoload": {
"classmap": [
"twitterAggregator.php"
]
"classmap": ["twitterAggregator.php"],
"files": ["wordpress.php"]
}
}
108 changes: 0 additions & 108 deletions twitterAggregator.php
Original file line number Diff line number Diff line change
Expand Up @@ -328,111 +328,3 @@ public function error( $message ) {
}
}


/************************************
wordpress functionality
***********************************/

// check if we're inside wordpress
if ( function_exists( 'add_shortcode' ) ) {

// twitter aggregator options for the wordpress settings page and the shortcode
$ta_options = array(
'ta_consumer_key',
'ta_consumer_secret',
'ta_oauth_access_token',
'ta_oauth_access_token_secret',
'ta_usenames'
);


/************************************
shortcode [twitter-aggregator]
***********************************/
function twitter_aggregator_shortcode( $atts ) {

// default parameters
$a = shortcode_atts( array(
'consumer_key' => get_option( "ta_consumer_key", "" ),
'consumer_secret' => get_option( "ta_consumer_secret", "" ),
'oauth_access_token' => get_option( "ta_oauth_access_token", "" ),
'oauth_access_token_secret' => get_option( "ta_oauth_access_token_secret", "" ),
'usernames' => get_option( "ta_usenames", "reuters,ap,propublica" ),
'update_interval' => 10,
'styles' => true,
'count' => 10,
'exclude_replies' => true,
'include_rts' => true
), $atts );


// generate an aggregator object
$ta = new twitterAggregator( $a );

if ( $a['styles'] ) {
// display the widget
return $ta->widget();
} else {
// display the widget
return $ta->widget_unstyled();
}
}

// register our function as a shortcode
add_shortcode( 'twitter-aggregator', 'twitter_aggregator_shortcode' );



/************************************
settings interface
***********************************/

// register the settings for the first time.
function ta_register_settings() {
global $ta_options;

// loop through the options and add them.
foreach ( $ta_options as $opt ) {
add_option( $opt, '');
register_setting( 'ta_options_group', $opt );
}
}
add_action( 'admin_init', 'ta_register_settings' );


// register the options page in the admin menu
function ta_register_options_page() {
add_options_page('Twitter Aggregator Settings', 'Twitter Aggregator', 'manage_options', 'ta', 'ta_options_page');
}
add_action('admin_menu', 'ta_register_options_page');


// the actual page output function
function ta_options_page() {
global $ta_options;

?>
<h2>Twitter Aggregator Settings</h2>
<form method="post" action="options.php">
<?php

// settings field group
settings_fields( 'ta_options_group' );

// loop through the options and output fields.
foreach ( $ta_options as $opt ) {
?>
<p><label for="<?php print $opt ?>"><?php print ucwords( str_replace( "ta_", "", str_replace( "_", " ", $opt ) ) ); ?></label><br>
<input type="text" id="<?php print $opt; ?>" name="<?php print $opt; ?>" value="<?php echo get_option( $opt ); ?>" /></p>
<?php
}

// add the submit button.
submit_button();

?>
</form>
<?php
}

}
111 changes: 111 additions & 0 deletions wordpress.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
<?php


/************************************
wordpress functionality
***********************************/

// check if we're inside wordpress
if ( function_exists( 'add_shortcode' ) ) {

// twitter aggregator options for the wordpress settings page and the shortcode
$ta_options = array(
'ta_consumer_key',
'ta_consumer_secret',
'ta_oauth_access_token',
'ta_oauth_access_token_secret',
'ta_usenames'
);


/************************************
shortcode [twitter-aggregator]
***********************************/
function twitter_aggregator_shortcode( $atts ) {

// default parameters
$a = shortcode_atts( array(
'consumer_key' => get_option( "ta_consumer_key", "" ),
'consumer_secret' => get_option( "ta_consumer_secret", "" ),
'oauth_access_token' => get_option( "ta_oauth_access_token", "" ),
'oauth_access_token_secret' => get_option( "ta_oauth_access_token_secret", "" ),
'usernames' => get_option( "ta_usenames", "reuters,ap,propublica" ),
'update_interval' => 10,
'styles' => true,
'count' => 10,
'exclude_replies' => true,
'include_rts' => true
), $atts );


// generate an aggregator object
$ta = new twitterAggregator( $a );

if ( $a['styles'] ) {
// display the widget
return $ta->widget();
} else {
// display the widget
return $ta->widget_unstyled();
}
}

// register our function as a shortcode
add_shortcode( 'twitter-aggregator', 'twitter_aggregator_shortcode' );



/************************************
settings interface
***********************************/

// register the settings for the first time.
function ta_register_settings() {
global $ta_options;

// loop through the options and add them.
foreach ( $ta_options as $opt ) {
add_option( $opt, '');
register_setting( 'ta_options_group', $opt );
}
}
add_action( 'admin_init', 'ta_register_settings' );


// register the options page in the admin menu
function ta_register_options_page() {
add_options_page('Twitter Aggregator Settings', 'Twitter Aggregator', 'manage_options', 'ta', 'ta_options_page');
}
add_action('admin_menu', 'ta_register_options_page');


// the actual page output function
function ta_options_page() {
global $ta_options;

?>
<h2>Twitter Aggregator Settings</h2>
<form method="post" action="options.php">
<?php

// settings field group
settings_fields( 'ta_options_group' );

// loop through the options and output fields.
foreach ( $ta_options as $opt ) {
?>
<p><label for="<?php print $opt ?>"><?php print ucwords( str_replace( "ta_", "", str_replace( "_", " ", $opt ) ) ); ?></label><br>
<input type="text" id="<?php print $opt; ?>" name="<?php print $opt; ?>" value="<?php echo get_option( $opt ); ?>" /></p>
<?php
}

// add the submit button.
submit_button();

?>
</form>
<?php
}

}

0 comments on commit 9bc9f14

Please sign in to comment.