从插件序列化数组中获取键的值

时间:2013-08-29 作者:Harish Chouhan

我想了解WordPress上托管的插件的详细信息。通过调用API来组织存储库http://api.wordpress.org/plugins/info/1.0/

我已经将我使用的代码和输出粘贴到下面。我不擅长PHP,只是想知道如何从数组中检索和输出一条信息,比如下载计数。

$payload = array(
  \'action\' => \'plugin_information\',
  \'request\' => serialize(
    (object)array(
        \'slug\' => \'i-recommend-this\',
        \'fields\' => array(
            \'downloaded\' => true,
            \'description\' => false

            )
     )
   )
);
$body = wp_remote_post( \'http://api.wordpress.org/plugins/info/1.0/\', array( \'body\' => $payload) );

var_dump( unserialize($body[\'body\']) );
这将以以下方式输出数据:

object(stdClass)[320]
  public \'name\' => string \'I Recommend This\' (length=16)
  public \'slug\' => string \'i-recommend-this\' (length=16)
  public \'version\' => string \'2.4.2\' (length=5)
  public \'author\' => string \'<a href="http://www.harishchouhan.com">Harish Chouhan</a>\' (length=57)
  public \'author_profile\' => string \'http://profiles.wordpress.org/hchouhan\' (length=38)
  public \'contributors\' => 
    array
      \'hchouhan\' => string \'http://profiles.wordpress.org/hchouhan\' (length=38)
      \'dreamsonline\' => string \'http://profiles.wordpress.org/dreamsonline\' (length=42)
      \'dreamsmedia\' => string \'http://profiles.wordpress.org/dreamsmedia\' (length=41)
      \'Benoit "LeBen" Burgener\' => string \'\' (length=0)
  public \'requires\' => string \'3.3\' (length=3)
  public \'tested\' => string \'3.5.2\' (length=5)
  public \'compatibility\' => 
    array
      \'3.6\' => 
        array
          \'2.4.0\' => 
            array
              ...
          \'2.4.2\' => 
            array
              ...
  public \'rating\' => float 92.4
  public \'num_ratings\' => int 13
  public \'downloaded\' => int 7673
  public \'last_updated\' => string \'2013-08-25\' (length=10)
  public \'added\' => string \'2012-04-25\' (length=10)
  public \'homepage\' => string \'http://www.harishchouhan.com/personal-projects/i-recommend-this/\' (length=64)
  public \'sections\' => 
    array
  \'description\' => string \'<p>This plugin allows your visitors to simply like/recommend your posts instead of comment on it.</p>

<h4>This plugin includes</h4>

<ul>
<li>A counter to display the number of "like" and to vote.</li>
<li>A widget and a function to display the X most liked posts.</li>
<li>A preference pane with some options.</li>
<li>Saves Cookie as well as users IP address to disable voting on the same post again</li>
</ul>

<h4>Advanced Options</h4>

<ul>
<li>Hide count if count is zero</li>
<li>Set a default messages w\'... (length=1694)
      \'installation\' => string \'<ol>
<li>Upload the directory <code>/i-recommend-this/</code> to the <code>/wp-content/plugins/</code> directory</li>
<li>Activate the plugin through the \'Plugins\' menu in WordPress</li>
<li>Click on the Settings link below the plugin name on the plugins page</li>
</ol>

<p>To display the recomment/like link other than at the bottom of individual post, you would have to add below code in your template</p>

<pre><code>&#60;?php if( function_exists(&#039;dot_irecommendthis&#039;) ) dot_irecommendthis(); ?&#62\'... (length=586)
      \'changelog\' => string \'<p>= 2.4.2
* Bug fix. Thanks to @mmaxim</p>

<p>= 2.4.1
* Fixed undefined index error.</p>

<p>= 2.4.0
* Added filter dot_irt_before_count to be able to allow custom content or icons before the count.</p>

<p>= 2.3.0
* Added option to hide count if count is zero
* Added option to disable saving of IP address in the database</p>

<p>= 2.2.0
* Added option to customize the link title. You can now remove the word recomment and add anything you like. Ideas suggested by Krystina Montemurro.</p>

<p>= 2.1.5
* Sup\'... (length=2216)
      \'faq\' => string \'<p>Take a look at the <a href="http://www.dreamsonline.net/wordpress-plugins/i-recommend-this/">official "I Recommend This" FAQ</a>.</p>

<p>You can also visit the <a href="http://www.dreamsonline.net/wordpress-plugins/i-recommend-this/">support center</a> and start a discussion if needed.</p>\' (length=294)
  public \'download_link\' => string \'http://downloads.wordpress.org/plugin/i-recommend-this.2.4.2.zip\' (length=64)
  public \'tags\' => 
    array
      \'dribbble-like\' => string \'dribbble like\' (length=13)
      \'heart\' => string \'heart\' (length=5)
      \'like\' => string \'Like\' (length=4)
      \'love\' => string \'love\' (length=4)
      \'post\' => string \'Post\' (length=4)
      \'rate\' => string \'rate\' (length=4)
      \'rating\' => string \'rating\' (length=6)
      \'recommend\' => string \'recommend\' (length=9)

2 个回复
SO网友:Harish Chouhan

Ok终于找到了一种方法:

$payload = array( \'action\' => \'plugin_information\', \'request\' => serialize( (object)array( \'slug\' => \'i-recommend-this\', \'fields\' => array( \'downloaded\' => true, \'description\' => false ) ) ) );

$body = wp_remote_post( \'http://api.wordpress.org/plugins/info/1.0/\', array( \'body\' => $payload) );

$body = unserialize($body[\'body\']);

echo \'<p>Downloaded: \' . print_r( $body->downloaded, true ) . \'</p>\';

SO网友:Harish Chouhan

刚刚找到plugin_api并使用它,可以通过以下代码实现上述功能:

/** If plugins_api isn\'t available, load the file that holds the function */ if ( ! function_exists( \'plugins_api\' ) ) require_once( ABSPATH . \'wp-admin/includes/plugin-install.php\' );

/** Prepare our query */ $call_api = plugins_api( \'plugin_information\', array( \'slug\' => \'i-recommend-this\' ) );

/** Display the results */ if ( is_wp_error( $call_api ) ) echo \'

\' . print_r( $call_api->get_error_message(), true ) . \'
\'; else //echo \'
\' . print_r( $call_api, true ) . \'
\'; echo \'

\' . print_r( $call_api->downloaded, true ) . \'

\';

结束

相关推荐

W3TC激活时临时API出现问题

我正在Wordpress 3.5.2上使用W3 Total Cache 0.9.2.11。我对瞬态API和w3tc的对象缓存设置有问题。当我使用memcached激活“对象缓存”(其他设置为默认设置)时,瞬态API停止工作。其行为是:当我在set\\u transient()之后的过期时间内调用get\\u transient()时,我得到null。当“对象缓存”被激活时,行为是一致的,当它被停用时,它会完美地工作。文档中没有提到set_transient 调用可能失败。我尝试了调试模式w3tc对象缓存。