Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 128 additions & 2 deletions class.wp-wiki-tooltip-admin.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct( $name='' ) {
add_action( 'admin_init', array( $this, 'register_error_settings' ) );
add_action( 'admin_init', array( $this, 'register_design_settings' ) );
add_action( 'admin_init', array( $this, 'register_thumb_settings' ) );
add_action( 'admin_init', array( $this, 'register_tweaks_settings' ) );

$comm = new WP_Wiki_Tooltip_Comm();
add_action( 'wp_ajax_get_wiki_page', array( $comm, 'ajax_get_wiki_page' ) );
Expand Down Expand Up @@ -82,6 +83,15 @@ public function init() {
'wp-wiki-tooltip-settings-thumb',
array( $this, 'settings_page_thumb' )
);

add_submenu_page(
'wp-wiki-tooltip-settings',
_x( 'Wiki Tooltips Advanced Settings', 'page title', 'wp-wiki-tooltip' ),
_x( 'Advanced', 'menu title', 'wp-wiki-tooltip' ),
'manage_options',
'wp-wiki-tooltip-settings-tweaks',
array( $this, 'settings_page_tweaks' )
);

$this->log( 'all request params: ' . print_r( $_REQUEST, true ) );

Expand All @@ -90,7 +100,8 @@ public function init() {
$result = ( delete_option( 'wp-wiki-tooltip-settings-base' ) &&
delete_option( 'wp-wiki-tooltip-settings-error' ) &&
delete_option( 'wp-wiki-tooltip-settings-design' ) &&
delete_option( 'wp-wiki-tooltip-settings-thumb' ) ) ? 'true' : 'false';
delete_option( 'wp-wiki-tooltip-settings-thumb' ) &&
delete_option( 'wp-wiki-tooltip-settings-tweaks' ) ) ? 'true' : 'false';
header( 'Location: options-general.php?page=wp-wiki-tooltip-settings&settings-updated=reset-' . $result );
die();
}
Expand Down Expand Up @@ -361,6 +372,53 @@ public function register_thumb_settings() {
);
}

public function register_tweaks_settings() {
global $wp_wiki_tooltip_default_options;

add_settings_section(
'wp-wiki-tooltip-settings-tweaks',
_x( 'Advanced', 'settings section headline', 'wp-wiki-tooltip' ),
array( $this, 'print_tweaks_section_info' ),
'wp-wiki-tooltip-settings-tweaks'
);

add_settings_field(
'cache-hit-days',
_x( 'Cache existing articles', 'settings field label', 'wp-wiki-tooltip' ),
array( $this, 'print_cache_hit_days_field' ),
'wp-wiki-tooltip-settings-tweaks',
'wp-wiki-tooltip-settings-tweaks'
);

add_settings_field(
'cache-miss-days',
_x( 'Cache non-existing articles', 'settings field label', 'wp-wiki-tooltip' ),
array( $this, 'print_cache_miss_days_field' ),
'wp-wiki-tooltip-settings-tweaks',
'wp-wiki-tooltip-settings-tweaks'
);

add_settings_field(
'wiki_request_timeout',
_x( 'Wiki request timeout', 'settings field label', 'wp-wiki-tooltip' ),
array( $this, 'print_request_timeout_field' ),
'wp-wiki-tooltip-settings-tweaks',
'wp-wiki-tooltip-settings-tweaks'
);

register_setting(
'wp-wiki-tooltip-settings-tweaks',
'wp-wiki-tooltip-settings-tweaks',
array(
'type' => 'array',
'description' => _x( 'Wiki Tooltips Advanced Settings', 'register setting description', 'wp-wiki-tooltip' ),
'sanitize_callback' => array( $this, 'sanitize_tweaks_settings' ),
'show_in_rest' => false,
'default' => $wp_wiki_tooltip_default_options
)
);
}

/********************************************************
* Sections
*******************************************************/
Expand All @@ -387,6 +445,10 @@ public function print_thumb_section_info() {
echo '&nbsp;<span class="bold-teletyper">[wiki thumbnail="on"]WordPress[/wiki]</span>&nbsp;' . __( 'or', 'wp-wiki-tooltip' ) . '&nbsp;<span class="bold-teletyper">[wiki thumbnail="off" title="WordPress"]a nice blogging software[/wiki]</span></p>';
}

public function print_tweaks_section_info() {
echo '<p>' . __( 'Some advanced settings to ensure smooth operation.' , 'wp-wiki-tooltip' ) . '</p>';
}

/********************************************************
* Base Settings Fields
*******************************************************/
Expand Down Expand Up @@ -653,6 +715,30 @@ public function print_thumb_style_field( $args ) {
echo '<p class="description">' . __( 'All entered CSS settings will be put into the CSS class of the thumbnail in the tooltip.', 'wp-wiki-tooltip' ) . '</p>';
}

public function print_cache_hit_days_field( $args ) {
printf(
'<p><label><input type="text" id="cache-hit-days" name="wp-wiki-tooltip-settings-tweaks[cache-hit-days]" value="%s" class="small-text" style="text-align:right;" /> ' . __( 'days', 'wp-wiki-tooltip' ) . '</label></p>',
isset( $this->options_tweaks['cache-hit-days'] ) ? esc_attr( $this->options_tweaks[ 'cache-hit-days' ] ) : $args[ 'cache-hit-days' ]
);
echo '<p class="description">' . __( 'When article exists, how many days it should be stored in cache. Zero turns off this cache.', 'wp-wiki-tooltip' ) . '</p>';
}

public function print_cache_miss_days_field( $args ) {
printf(
'<p><label><input type="text" id="cache-miss-days" name="wp-wiki-tooltip-settings-tweaks[cache-miss-days]" value="%s" class="small-text" style="text-align:right;" /> ' . __( 'days', 'wp-wiki-tooltip' ) . '</label></p>',
isset( $this->options_tweaks['cache-miss-days'] ) ? esc_attr( $this->options_tweaks[ 'cache-miss-days' ] ) : $args[ 'cache-miss-days' ]
);
echo '<p class="description">' . __( 'When article doesn\'t exist, how many days it should be stored in cache. Zero turns off this cache.', 'wp-wiki-tooltip' ) . '</p>';
}

public function print_request_timeout_field( $args ) {
printf(
'<p><label><input type="text" id="wiki_request_timeout" name="wp-wiki-tooltip-settings-tweaks[wiki_request_timeout]" value="%s" class="small-text" style="text-align:right;" /> ' . __( 'seconds', 'wp-wiki-tooltip' ) . '</label></p>',
isset( $this->options_tweaks['wiki_request_timeout'] ) ? esc_attr( $this->options_tweaks[ 'wiki_request_timeout' ] ) : $args[ 'wiki_request_timeout' ]
);
echo '<p class="description">' . __( 'Timeout value (in seconds) when asking Wiki for info about article.', 'wp-wiki-tooltip' ) . '</p>';
}

public function sanitize_base_settings( $input ) {
global $wp_wiki_tooltip_default_options;
$this->log( 'Input for BASE => <' . print_r( $input, true ) . '>' );
Expand Down Expand Up @@ -801,13 +887,42 @@ public function sanitize_thumb_settings( $input ) {
return $input;
}

public function sanitize_tweaks_settings( $input ) {
global $wp_wiki_tooltip_default_options;
$this->log( 'Input for TWEAKS => <' . print_r( $input, true ) . '>' );

if( ! isset( $input[ 'nonce' ] ) || ! wp_verify_nonce( $input[ 'nonce' ], 'wp-wiki-tooltip-settings-tweaks-submit' ) ) {
$this->sanitize_stop();
}

// check cache-hit-days
$input[ 'cache-hit-days' ] = ( int ) $input[ 'cache-hit-days' ];
if( 0 > $input[ 'cache-hit-days' ] ) {
$input[ 'cache-hit-days' ] = $wp_wiki_tooltip_default_options[ 'tweaks' ][ 'cache-hit-days' ];
}

// check cache-miss-days
$input[ 'cache-miss-days' ] = ( int ) $input[ 'cache-miss-days' ];
if( 0 > $input[ 'cache-miss-days' ] ) {
$input[ 'cache-miss-days' ] = $wp_wiki_tooltip_default_options[ 'tweaks' ][ 'cache-miss-days' ];
}

// check timeout, it shouldn't be less than default
$input[ 'wiki_request_timeout' ] = ( int ) $input[ 'wiki_request_timeout' ];
if( $input[ 'wiki_request_timeout' ] < $wp_wiki_tooltip_default_options[ 'tweaks' ][ 'wiki_request_timeout' ] ) {
$input[ 'wiki_request_timeout' ] = $wp_wiki_tooltip_default_options[ 'tweaks' ][ 'wiki_request_timeout' ];
}

return $input;
}

public function sanitize_stop() {
wp_die( _x( 'Sorry, but this request seems to be invalid!', 'nonce check invalid', 'wp-wiki-tooltip' ) );
}

public function settings_page( $active_tab = 'base' ) {

if( isset( $_GET[ 'tab' ] ) && in_array( $_GET[ 'tab' ], array( 'base', 'error', 'design', 'thumb' ) ) ) {
if( isset( $_GET[ 'tab' ] ) && in_array( $_GET[ 'tab' ], array( 'base', 'error', 'design', 'thumb', 'tweaks' ) ) ) {
$active_tab = $_GET[ 'tab' ];
}

Expand All @@ -821,6 +936,7 @@ public function settings_page( $active_tab = 'base' ) {
<a href="?page=wp-wiki-tooltip-settings&tab=error" class="nav-tab <?php echo $active_tab == 'error' ? 'nav-tab-active' : ''; ?>"><?php _ex( 'Error Handling', 'settings tab title', 'wp-wiki-tooltip' ); ?></a>
<a href="?page=wp-wiki-tooltip-settings&tab=design" class="nav-tab <?php echo $active_tab == 'design' ? 'nav-tab-active' : ''; ?>"><?php _ex( 'Design', 'settings tab title', 'wp-wiki-tooltip' ); ?></a>
<a href="?page=wp-wiki-tooltip-settings&tab=thumb" class="nav-tab <?php echo $active_tab == 'thumb' ? 'nav-tab-active' : ''; ?>"><?php _ex( 'Thumbnail', 'settings tab title', 'wp-wiki-tooltip' ); ?></a>
<a href="?page=wp-wiki-tooltip-settings&tab=tweaks" class="nav-tab <?php echo $active_tab == 'tweaks' ? 'nav-tab-active' : ''; ?>"><?php _ex( 'Advanced', 'settings tab title', 'wp-wiki-tooltip' ); ?></a>
</h2>

<form method="post" action="options.php">
Expand All @@ -844,6 +960,12 @@ public function settings_page( $active_tab = 'base' ) {
settings_fields('wp-wiki-tooltip-settings-thumb' );
do_settings_sections('wp-wiki-tooltip-settings-thumb' );

} elseif( $active_tab == 'tweaks' ) {

wp_nonce_field('wp-wiki-tooltip-settings-tweaks-submit', 'wp-wiki-tooltip-settings-tweaks[nonce]' );
settings_fields('wp-wiki-tooltip-settings-tweaks' );
do_settings_sections('wp-wiki-tooltip-settings-tweaks' );

} else {

wp_nonce_field('wp-wiki-tooltip-settings-base-submit', 'wp-wiki-tooltip-settings-base[nonce]' );
Expand Down Expand Up @@ -875,4 +997,8 @@ public function settings_page_design() {
public function settings_page_thumb() {
$this->settings_page( 'thumb' );
}

public function settings_page_tweaks() {
$this->settings_page( 'tweaks' );
}
}
8 changes: 8 additions & 0 deletions class.wp-wiki-tooltip-base.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ public function load_all_options() {
$wp_wiki_tooltip_default_options,
array( 'thumb-enable', 'thumb-align', 'thumb-width', 'thumb-style' )
);

$this->options_tweaks = $this->load_single_option(
'tweaks',
$old_options,
get_option( 'wp-wiki-tooltip-settings-tweaks' ),
$wp_wiki_tooltip_default_options,
array( 'cache-hit-days', 'cache-miss-days', 'wiki_request_timeout' )
);
}

public static function log( $msg = '' ) {
Expand Down
32 changes: 27 additions & 5 deletions class.wp-wiki-tooltip-comm.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,28 @@ class WP_Wiki_Tooltip_Comm extends WP_Wiki_Tooltip_Base {
'format' => 'json',
'siprop' => 'general'
);

private $timeout_value = -1;

private function get_url_with_timeout($url)
{
if( $this->timeout_value == -1 )
{
$option=get_option('wp-wiki-tooltip-settings-tweaks');
if( is_array( $option ) && isset( $option[ 'wiki_request_timeout' ])) {
$this->timeout_value = $option[ 'wiki_request_timeout' ];
}
else {
$this->timeout_value = 5;
}
}

$args=array(
'timeout' => $this->timeout_value
);

return wp_remote_get( $url, $args );
}

public function ajax_get_wiki_page() {

Expand Down Expand Up @@ -90,7 +112,7 @@ public function ajax_get_wiki_page() {
}

$this->page_query_args[ 'pageid' ] = $wiki_id;
$response = wp_remote_get( $wiki_url . '?' . http_build_query( $this->page_query_args ) );
$response = $this->get_url_with_timeout( $wiki_url . '?' . http_build_query( $this->page_query_args ) );

if ( is_array( $response ) && ! is_wp_error( $response ) && ! $section_error ) {
$wiki_data = json_decode( $response['body'], true );
Expand Down Expand Up @@ -125,7 +147,7 @@ public function ajax_get_wiki_page() {
if( $thumb_enable == 'on' ) {
$this->image_query_args[ 'pageids' ] = $wiki_id;
$this->image_query_args[ 'pithumbsize' ] = $thumb_width;
$response = wp_remote_get($wiki_url . '?' . http_build_query( $this->image_query_args ) );
$response = $this->get_url_with_timeout($wiki_url . '?' . http_build_query( $this->image_query_args ) );
if( is_array( $response ) && ! is_wp_error( $response ) ) {
$image_data = json_decode( $response[ 'body' ], true );
if( isset( $image_data[ 'query' ][ 'pages' ][ $wiki_id ][ 'thumbnail' ] ) ) {
Expand All @@ -152,7 +174,7 @@ public function ajax_test_wiki_url() {
$wiki_urls = array( $wurl, $wurl . '/api.php', $wurl . '/w/api.php' );

foreach( $wiki_urls as $wurl ) {
$response = wp_remote_get( $wurl . '?' . http_build_query( $this->test_query_args ) );
$response = $this->get_url_with_timeout( $wurl . '?' . http_build_query( $this->test_query_args ) );

if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$wiki_data = json_decode( $response[ 'body' ], true );
Expand All @@ -175,7 +197,7 @@ public function ajax_test_wiki_url() {

private function get_section_id_by_name( $wiki_url = '', $page_id = -1, $section_title = '' ) {
$this->sections_query_args[ 'pageid' ] = $page_id;
$response = wp_remote_get( $wiki_url . '?' . http_build_query( $this->sections_query_args ) );
$response = $this->get_url_with_timeout( $wiki_url . '?' . http_build_query( $this->sections_query_args ) );

if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$wiki_data = json_decode( $response[ 'body' ], true );
Expand All @@ -192,7 +214,7 @@ private function get_section_id_by_name( $wiki_url = '', $page_id = -1, $section

public function get_wiki_page_info( $title = '', $wiki_url = '' ) {
$this->info_query_args[ 'titles' ] = $title;
$response = wp_remote_get( $wiki_url . '?' . http_build_query( $this->info_query_args ) );
$response = $this->get_url_with_timeout( $wiki_url . '?' . http_build_query( $this->info_query_args ) );

if ( is_array( $response ) && ! is_wp_error( $response ) ) {
$wiki_data = json_decode( $response['body'], true );
Expand Down
18 changes: 16 additions & 2 deletions class.wp-wiki-tooltip.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ public function init() {
}

public function add_wiki_container() {

echo sprintf(
'<div id="wiki-container"
data-wp_ajax_url="%s"
Expand Down Expand Up @@ -115,13 +116,26 @@ public function do_wiki_shortcode( $atts, $content ) {
$num = $this->shortcode_count++;

$trans_wiki_key = urlencode( $wiki_base_id . "-" . $wiki_url . '-' . $title . '-' . $this->version );
if( ( $trans_wiki_data = get_transient( $trans_wiki_key ) ) === false ) {
if( ( $trans_wiki_data = get_transient( $trans_wiki_key ) ) === false || // if there is no transient
( $this->options_tweaks[ 'cache-miss-days' ] == 0 && $trans_wiki_data[ 'wiki-id' ] == '-1') // if there is an "empty" transient and caching of misses is disabled
) {

$comm = new WP_Wiki_Tooltip_Comm();
$trans_wiki_data = $comm->get_wiki_page_info( $title, $wiki_url );
$trans_wiki_data[ 'wiki-base-url' ] = $wiki_url;

set_transient( $trans_wiki_key, $trans_wiki_data, WEEK_IN_SECONDS );
if( $trans_wiki_data[ 'wiki-id' ] == -1) {
// if there is a miss, set transient only when and cache-miss-days > 0
if( $this->options_tweaks[ 'cache-miss-days' ] > 0 ) {
set_transient( $trans_wiki_key, $trans_wiki_data, $this->options_tweaks[ 'cache-miss-days' ] * DAY_IN_SECONDS );
}
}
else {
// if there is a hit, set transient only when and cache-hit-days > 0
if( $this->options_tweaks[ 'cache-hit-days' ] > 0 ) {
set_transient( $trans_wiki_key, $trans_wiki_data, $this->options_tweaks[ 'cache-hit-days' ] * DAY_IN_SECONDS );
}
}
}

// $output = 'data-wiki_num="' . $num . '" data-wiki_id="' . $trans_wiki_data[ 'wiki-id' ] . '" data-wiki_title="' . $trans_wiki_data[ 'wiki-title' ] . '" data-wiki_section="' . $params[ 'section' ] . '" data-wiki_base_url="' . $trans_wiki_data[ 'wiki-base-url' ] . '" data-wiki_url="' . $trans_wiki_data[ 'wiki-url' ] . '" data-wiki_thumbnail="' . $params[ 'thumbnail' ] . '"';
Expand Down
18 changes: 10 additions & 8 deletions config.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@
]
],

'cache' => [ // NOT USED: cache settings - default "1 week"

'count' => 1, // how many

'unit' => 'week' // what
],

'a-target-range' => [ '_blank', '_self' ],

'a-target' => '_blank', // where to open links to wiki pages
Expand Down Expand Up @@ -84,5 +77,14 @@
'thumb-width' => '200', // standard width of the thumbnails

'thumb-style' => '', // stylesheets for thumbnail images
]
],

'tweaks' => [

'cache-hit-days' => 7, // cache TTL when article exists

'cache-miss-days' => 7, // cache TTL when article doesn't exist

'wiki_request_timeout' => 5, // timeout to use when askong Wiki API
]
];